Division of Mathematics and Computer Science Maryville College
Pointers and Addresses Memory is organized into a big array. Every data item occupies one or more cells. A pointer stores an address. A pointer is itself a variable.
Pointer Syntax Declaration type *name int *ptr; Declares a pointer to a variable of given type. Address Of &variable &x Returns the address of the variable. Dereference *name *ptr Gets to the value at the address stored in the pointer. Size Information sizeof Gets the size, in bytes, of its argument. sizeof(type) sizeof expression
Pointers and Function Arguments Pointers are the only mechanism to share variables with a function. In C, all function calls are pass-by-value. The value of a pointer is a memory location, and so they are copied but provide a common reference.
scanf(fmt,...) char c; int x; double d; scanf("%c%d%x", &c, &x, &d); This is the formatted input method in stdio.h Uses the same formatters as printf You do not use normal strings, just simple format place holders. Parameters are passed in as pointers.
Pointers and Arrays Array Names Synonymous with the first address in the array. Is not a variable! Suppose we have an integer pointer pa and an int array ar[10] We can assign the pointer: pa = &ar[0] Alternatively: pa = ar The following are equivalent operations: ar[i] pa[i] *(ar + i) *(pa + i)
Character Pointers and Functions String literals have type const char* Strings are terminated by null characters. "This is a string" is really: "This is a string\0" When passing strings into functions, use type char* The null terminator is there so that functions can find the end of the string. Arrays of strings are arrays of pointers! char *[] or char**.
Address Arithmetic Every pointer has an associated type. Adding or subtracting from a pointer moves the address by the specified amount. int *pa; pa++; /* The next int */ pa ; /* The previous int */ pa+=2; /* Go forward 2 ints */ You can also add and subtract pointers: n = qa-pa; /* number of ints */
Alloc Example /* size of available space */ #define ALLOCSIZE 10000 /* storage for alloc */ static unsigned char allocbuf[allocsize]; /* next free position */ static void *allocp = allocbuf;
Alloc Example - alloc(int n) /* return pointer to n characters */ void *alloc(int n) { if((void*)allocbuf + ALLOCSIZE - allocp >= n) { /* it fits */ allocp += n; return allocp - n; /* old p */ } else { /* not enough room */ return 0; } }
Alloc Example - afree(void *p) /* free storage pointed to by p */ void afree(void *p) { if(p >= (void*)allocbuf && p < (void*)allocbuf + ALLOCSIZE) { allocp=p; } }
Command-Line Arguments C provides a mechanism to send parameters into main: int main(int argc, char **argv) argc is the number of arguments argv is an array of character pointers. The array itself is terminated by NULL. argv[0] is the name of the program as typed at the shell.
Pointers to Functions Functions are not variables, but they do reside in memory. A function pointer contains the address of the beginning of a function. Suppose we had a comparison function which takes two pointers: int compare(int *x, int *y); We could build a pointer to this function: int (*fp)(int*, int*); fp = compare; We can call the function from this pointer (*fp)(&x, &y); Could we use this function pointer to reference compare? Why or why not? int(*gp)(void*, void*);
malloc and free The standard library (in #include <stdlib.h>) has an allocation function. void *malloc(size_t n); Allocates n bytes and returns a pointer to the first byte. Example: Create an array of 10 integers. int *pa = malloc(10 * sizeof(int)); void free(void *p); Deallocates a pointer allocated by malloc. Example: Destroy our array. free(pa); Typically, malloc and free are implemented as a heap.
The Unix Memory Model A process begins with a read-only program text segment. Following the program text segment is the writable data segment. The program break is the address of the end of the data segment. The program break can be advanced upwards using the brk syscall. (sbrk is a more convenient wrapper.) malloc s heap lives in the data segment. The stack is at the highest address and grows downwards.