/** * Variables must be explicitly allocated and * explicitly freed. All dereference must be * explicit. * If you want to allocate memory inside a function and return * it to the calling function, then you need to allocate that memory * on the heap (malloc!). */ #include #include int* createThing( ) { int thing = 10; return &thing; // Don't do this!!! The memory that was allocated on the call stack will be gone. } int main (int argc, char* arvg[]) { int *a = (int *)malloc(sizeof(int)); *a = 4; printf( "a has a value that is an address: %p\n", a ); printf( "*a =%d\n", *a ); free(a); // printf( "a has a value that is an address: %p\n", a ); // Don't do this! You no longer have the right to look at that address. // printf( "*a =%d\n", *a ); a = createThing(); printf( "a has a value that is an address: %p\n", a ); printf( "*a =%d\n", *a ); // dereferencing a NULL pointer leads to a segmentation fault. int *b = NULL; printf( "*b = %d\n", *b ); return(0); }