/* Based on Scott Anderson's code * modified by Mark Sheldon to conform to current course coding standards. */ #include #include #define ONE_KILOBYTE 0x1000 unsigned int prev_i_addr = 0; unsigned int prev_p_addr = 0; void show_addr(int depth) { int i; int stack_array[ONE_KILOBYTE]; int* dynamic_array; void* p; unsigned int i_addr; unsigned int p_addr; p = malloc(ONE_KILOBYTE * sizeof(int)); if (p == NULL) { fprintf(stderr, "Couldn't allocate memory.\n"); exit(1); } dynamic_array = (int *) p; /* check that we can really access these elements */ stack_array[0xFFF] = 1; dynamic_array[0xFFF] = 2; i_addr = (unsigned int) &i; p_addr = (unsigned int) p; printf("at depth %d, i is at %x and p is at %x; p %s i\n ", depth, i_addr, p_addr, p_addr < i_addr ? "<" : ">"); printf(" sa[-1]=%d da[-1]=%d, heap diff = %d, stack diff = %d\n\n", stack_array[-1], dynamic_array[-1], p_addr - prev_p_addr, prev_i_addr - i_addr); prev_p_addr = p_addr; prev_i_addr = i_addr; if (depth < 9) show_addr(depth + 1); /* Oh, let's free things before we return */ free(p); } int main( int argc, char* argv[] ) { show_addr(0); return 0; }