Unformatted text preview:

1CMSC 212 – S05 (lect 11)Announcementsz Midterm #1– Re-grade requests due by Thursday at 1:45 PMz Program #1B– Grading of Coding - returned in class– Re-grade requests due by next Tuesday at 1:45 PMz Program #2– Due in one weekz Reading– Chapter 14 (Today)– Chapter 12, 10.2 (Tuesday)2CMSC 212 – S05 (lect 11)Other Helpful Memory Routinesz 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 memoryz #include <alloca.h>z void *alloca(size_t size);– allocate memory on the stack– memory is freed when the function returns3CMSC 212 – S05 (lect 11)More Dynamic Memory Examplestypedef struct {int count;char *name;float price;} item;item *inventory;inventory = (item *) calloc(sizeof(item), 20);for (i=0; i < 20; i++) {inventory[i].name = strdup(newName);}4CMSC 212 – S05 (lect 11)Steps in compiling a programz Converting .c to .o files– Pre-processor–Compiler• Scanner/Parser• Type Checker•Optimizer• Code Generator– Assembler• Converts assembly code to machine codez Converting .o's to an executable–Linker• resolves symbols – function use and definitions– global variable declarations and use5CMSC 212 – S05 (lect 11)Pre-processorz Makes a pass over the program before the compilerz Handles– #include statements• puts it all into one file–Macros– Pre-processor Symbols6CMSC 212 – S05 (lect 11)Pre-defined Symbolsz __FILE__– the name of the source file being compiledz __LINE__– line number of the current line in the filez __DATE__– Date the file was compiledz __TIME__– Time the file was compiledz __STDC__– 1 if the compiler conforms to ANSI C7CMSC 212 – S05 (lect 11)#definez #define name stuffz Symbolic constants– Primary Use– #define MAX_SIZE_TABLE 1024z Macros– #define name(paramter-list) stuff–Example:• #define SUM(a,b) a + bz Macros are a single line– Can define a "continuation" by ending line with \– #define macro1 a very long macro \– that runs onto another line8CMSC 212 – S05 (lect 11)Macro Substitutionz Items are textually replacedz 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 + 39CMSC 212 – S05 (lect 11)Solution to Macro Expressionsz Use parenthesis– #define SQUARE(x) ((x) * (x))– #define DOUBLE(x) ((x) + (x))z A Common Macro:– #define MAX(a, b) ( (a) > (b) ? (a) : (b))10CMSC 212 – S05 (lect 11)#define substitution1. Check macro invocation arguments for macros• If present, replace with macro values2. Replace macros invocations by defined macro3. Scan for any defined macros used in program• If found, repeat steps 1 & 2String literal are not scanned for macro replacement11CMSC 212 – S05 (lect 11)Macros vs. Functionsz 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 compilez Example: macro being passed a type as a paramz #define MALLOC(n, type) \– ((type *) malloc( (n) * sizeof( type )))12CMSC 212 – S05 (lect 11)Other Issues with Macrosz Side Effects of Macro Parameters– Replace each use of parameter– Consider:• #define DOUBLE(x) ((x) + (x))• y = DOUBLE(++j);• y = ((++j) + (++j))z Can be hard to tell macros from functions– Solution, use all upper case for macro namesz #undef name– un-define a macroz Compiler Command Line Definition– gcc -DFOO=3– defines FOO as though #define FOO 3 appeared in program13CMSC 212 – S05 (lect 11)Conditional Compilation z #if constant-expressions– statementsz #elif constat-expressions– more statementsz #else– yet more statementsz #endifz defined(macro-name)– constant expression, 1 if macro-name is definedz Useful if code must be different on different systemsz Tends to make code hard to read14CMSC 212 – S05 (lect 11)Conditional Compilation Examplez #if defined(i386_platform)– something specific to i386z #elsif defined(sparc)– some specific to sparcz #else– #errorz #endif15CMSC 212 – S05 (lect 11)File Inclusionz #include "foo.h"– Works as we have seen earlierz What if foo.h includes another file– Pre-processor can handle thisz What if "foo.h" and "bar.h" each include #stuff.h– This is fine, until one program includes both foo.h and bar.hz Solution (inside stuff.h):#ifndef STUFF_H#define STUFF_H….#endif16CMSC 212 – S05 (lect 11)Include File Searchz #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 compiledz #include "anotherFile.h"– Search in the current directory, then the list of include directoriesz Can use command line flags to change locations– gcc -I<some directory>– looks in <some directory> for header


View Full Document

UMD CMSC 212 - Lecture 11

Download Lecture 11
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 11 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 11 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?