/* addressreport.c Jeff Ondich, 5/15/08 This program is for use in the takehome exam for CS332, Spring 2008. Its purpose is to report the addresses at which a variety of objects held in memory are stored. */ #include #include int f( int n ); int main() { char *literalPtr = "This is a string literal. Where is it stored?"; printf( "the address of the function 'main': 0x%lx\n", (long)main ); printf( "the address of the function 'f': 0x%lx\n", (long)f ); printf( "the address of the function 'printf': 0x%lx\n", (long)printf ); printf( "the address of main's local variable 'literalPtr': 0x%lx\n", (long)(&literalPtr) ); printf( "the address of the string literal to which 'literalPtr' points: 0x%lx\n", (long)literalPtr ); f( 5 ); return 0; } int f( int n ) { char unusedBigParameter[2000]; sprintf( unusedBigParameter, "some text for the unused array" ); char *message = (char *)malloc( 100 * sizeof(char) ); sprintf( message, "This message is from f(%d)\n", n ); if( n > 0 ) { printf( message ); printf( "address of f's parameter 'n': 0x%lx\n", (long)(&n) ); printf( "address of the f's variable 'message': 0x%lx\n", (long)(&message) ); printf( "address of memory allocated dynamically in f(%d): 0x%lx\n", n, (long)message ); f( n - 1 ); } free( message ); return 0; }