Docsity

Lecture Notes on Grading a Coding - Introduction to Low Level Program | CMSC 212, Study notes of Computer Science

Partial preview of the text

Download Lecture Notes on Grading a Coding - Introduction to Low Level Program | CMSC 212 and more Study notes Computer Science in PDF only on Docsity!

Announcements

z^

Midterm #1– Re-grade requests due by Thursday at 1:45 PM

z^

Program #1B– Grading of Coding - returned in class– Re-grade requests due by next Tuesday at 1:45 PM

z^

Program #2– Due in one week

z^

Reading– Chapter 14 (Today)– Chapter 12, 10.2 (Tuesday)

Other Helpful Memory Routines

z^

char *strdup(char *string);– not ansi, but supported by most compilers– allocate enough memory to hold string (and it's null)– copy string into the allocated memory

z^

#include <alloca.h>

z^

void *alloca(size_t size);– allocate memory on the

stack

  • memory is freed when the function returns

Steps in compiling a program

z^

Converting .c to .o files– Pre-processor– Compiler

  • Scanner/Parser• Type Checker• Optimizer• Code Generator
    • Assembler
      • Converts assembly code to machine code

z^

Converting .o's to an executable– Linker

  • resolves symbols
    • function use and definitions– global variable declarations and use

Pre-processor

z^

Makes a pass over the program before the compiler

z^

Handles– #include statements

  • puts it all into one file
    • Macros– Pre-processor Symbols

#define

z^

#define name stuff

z^

Symbolic constants– Primary Use– #define MAX_SIZE_TABLE 1024

z^

Macros– #define name(paramter-list) stuff– Example:

  • #define SUM(a,b) a + b

z^

Macros are a single line– Can define a "continuation" by ending line with \– #define macro1 a very long macro \–

that runs onto another line

Macro Substitution

z^

Items are textually replaced

z^

Consider:– #define SQUARE(x)

x * x

  • foo = SQUARE(a + 1)– This is replaced to:
    • foo = a + 1 * a + 1
      • #define DOUBLE(x)

x + x

  • foo = 10 * DOUBLE(3)– This is replaced to:
    • foo = 10 * 3 + 3

#define substitution

Check macro invocation arguments for macros•

If present, replace with macro values

Replace macros invocations by defined macro

Scan for any defined macros used in program•

If found, repeat steps 1 & 2

String literal are not scanned for macro replacement

Macros vs. Functions

z^

They sort of look alike, but macros– are textually replaced into program

  • can be faster than subroutines for short things
    • macros may not be recursive– do not check types of parameters
      • replaced code is type checked during compile

z^

Example: macro being passed a type as a param

z^

#define MALLOC(n, type) \– ((type *) malloc( (n) * sizeof( type )))

Conditional Compilation

z^

#if constant-expressions– statements

z^

#elif constat-expressions– more statements

z^

#else– yet more statements

z^

#endif

z^

defined(macro-name)– constant expression, 1 if macro-name is defined

z^

Useful if code must be different on different systems

z^

Tends to make code hard to read

Conditional Compilation Example

z^

#if defined(i386_platform)– something specific to i

z^

#elsif defined(sparc)– some specific to sparc

z^

#else– #error

z^

#endif

Include File Search

z^

#include <someFile.h>– Search a list of places for the file (a standard set is defined)– On Linux includes /usr/include– Generally used for file that are not part of the project being

compiled

z^

#include "anotherFile.h"– Search in the current directory, then the list of include

directories

z^

Can use command line flags to change locations– gcc -I– looks in for header files