CSC195, Class 19: Pointers Overview: * Pointers * Pointers in C * Basic operations * Memory allocation Notes: * Help me get rid of my M&M's * Sam out of town on Friday. Mr. Gum replaces Sam. * Any volunteers to get data for the EBoard? Thanks Reese. * Exam coming soon. * Graded homework coming later. * Comments about class format? The EBoards? The labs? The Web site? + How do I get you to talk more? ---------------------------------------- Almost every value you use in a program gets stored somewhere in memory. int x; If it's somewhere in memory, it has an address that the computer knows. "pointer" is another name for "a data type that stores addresses" Addresses are useful because: * Languages that include pass-by-reference need addresses. * You can do interesting things by pointing to different areas of memory, rather than moving stuff. * Needed for dynamic data structures * They give you a clue as to what's really happening in the machine Pointers are a powerful tool * And therefore a dangerous one C tries to reveal pointers and to give you appropriate mechanisms for dealing with them. Declaring pointers: int *p; /* p contains a memory address. That memory address contains an integer. */ int *p,q; /* q is an integer. */ typedef int *pointer_to_integer pointer_to_integer ip; int **h; /* h contains a memory address. That memory address contains a memory address. That memory address contains an integer. */ Dereferencing pointers *p /* The thing that p points to */ *p = 5; /* Put 5 in the memory location stored in p. */ p = 5; /* Make p point to memory location 5. */ /* Splint will hate you. */ p = (int *) 5; i = *p; Suppose int *p,*q; p = q; /* p and q now point to the same thing. */ Next key operation: Obtaining addresses int i; &i; /* Address of i. */ p = &i; /* Make p point at i. */ q = &i; /* Make q point at i. */ *p = 5; i = 1; printf("%d", *p); /* Prints 1 if p still happens to point to i. */ Back to DL's question q = &p; /* is legal, but a bad idea */ Reese's question: Can't we make p point somewhere else first? We need a "I want to point somewhere that's safe to point" operation. p = (int *) malloc(amount-of-memory); If malloc returns NULL ((void *) 0), that much memory wasn't available. To free memory you've allocated with malloc free(p); Here's some code guaranteed to crash your system eventually p = (int *) malloc(amount-of-memory); p = &i; One of the most common programming bugs: Forgetting to free memory. p = (int *) malloc(amount-of-memory); ... q = p; ... free(p); p = &i; ... r = malloc(sizeof(int)); *q = 5; *r = 1; printf("%d", *q); Good programmers eventually write their own memory allocators for special purposes. Note that we can think of computers as having three "areas" of memory. stack: Where local variables and parameters go. heap: Where longer-lived allocated stuff goes. static: Where unchanging stuff goes. also "text" +----+ |TEXT| address 0, beginning of memory +----+ |TEXT| +----+ |TEXT| +----+ | | +----+ | | +----+ |HEAP| +----+ | | +----+ ... +----+ |STAC| address X, end of memory +----+