#include #include #include // where are arrays placed? // on the stack. // only malloc allocates memory on the heap void test1( int N ) { int a[4]; // should be on stack int b[N]; // this is on the stack // scary example int M; int d[M]; // yikes!!! How big is this? M doesn't have a value. int *c = (int *)malloc( sizeof(int)*4 ); // should be on heap printf( "address of a is %p\n", a ); printf( "address of b is %p\n", b ); printf( "address of d is %p\n", b ); printf( "address of c is %p and it points to %p\n", &c, c ); } // Is freed memory reused? void test2() {; int *a; int i; for (i = 0; i < 4; i++) { a = (int *)malloc( sizeof(int) * 12 ); printf( "a points to address %p\n", a ); free( a ); } } // What about chunks of different sizes? void test3() {; int *a; int i; // try this with chunks of the same size and different size for (i = 0; i < 4; i++) { a = (int *)malloc( sizeof(int) * 10 * (i+1) ); printf( "a points to address %p\n", a ); free( a ); } } // Is memory allocated in order? NO!!! void test4() {; int *a; int i; // try this with chunks of the same size and different size for (i = 0; i < 4; i++) { a = (int *)malloc( sizeof(int) * 10 * (i+1) ); printf( "a points to address %p\n", a ); } a = (int *)malloc( sizeof(int) * 10 ); printf( "a points to address %p\n", a ); a = (int *)malloc( sizeof(int) * 1 ); printf( "a points to address %p\n", a ); } // If I free a big chunk of memory, will it be reused for allocating // new little chunks? void test5() { int *a = malloc( sizeof(int)*10 ); printf( "a points to address %p\n", a ); int *b = malloc( sizeof(int)*20 ); printf( "b points to address %p\n", b ); free( a ); a = malloc( sizeof(int)*12 ); printf( "a points to address %p\n", a ); free( a ); int i; int *c[12]; for (int i = 0; i<12; i++) { c[i] = malloc( sizeof(int) ); } for (int i = 0; i<12; i++) { printf( "c %d is %p\n", i, c[i] ); free(c[i]); } a = malloc( sizeof(int)*12 ); printf( "a points to address %p\n", a ); } // can I force it to coallesce? void test6() { unsigned long long i; int *a; int *lastused = NULL; for (i = 64;; i*=2) { a = malloc( sizeof(int)*i); if (a == lastused) { printf( "Using same address as last used, i=%llu\n", i ); return; } lastused = a; printf( "a points to %p\n", a ); free( a ); } } // Can I detect which way the stack grows and where the heap and static space are, relative to the stack? // On Stephanie's laptop, using clang, she finds that // The heap is at the highest addresses // The stack is at lower addresses and grows down // The static area is at the lowest addresses. void test7_helper( ) { int a = 5; printf( "a in test7_helper is at %p\n", &a ); int *b = (int *)malloc( sizeof(int) ); printf( "b in test7_helper is at %p\n", &b ); printf( "b in test7_helper points to %p\n", b ); } void test7() { static int my_static = 5; int a = 5; printf( "my_static is at %p\n", &my_static ); printf( "a in test7 is at %p\n", &a ); test7_helper(); } int main(int argc, char *argv[]) { // test1(4); // test2(); test3(); // test4(); // test5(); // test6(); // test7(); }