/* * A simple example to help understand process layout in memory and stack */ #include #include #include #include /* BIG is 64KB */ #define BIG (1 << 16) char big_array[BIG]; char buffer[BIG] = "I'm a string"; int z = 101; /* * The recursive sumup function */ static int sumup(int x) { int rval; if (x == 0) { return (0); } rval = sumup(x - 1); return (x + rval); } /* * The swap function */ static void swap(int *x, int *y) { int t; t = *x; *x = *y; *y = t; } /* * We will force execution of the foo function on returning from swap */ int foo(void) { (void) printf("You called foo()\n"); return (0); } /* * Do a few bits and pieces */ int main(int argc, char **argv) { char local[128]; int a, b; char *ptr; /* Check usage */ if (argc != 1) { (void) fprintf(stderr, "Usage: %s\n", argv[0]); exit(EXIT_FAILURE); } /* Copy a string */ (void) strcpy(local, "I am a C program"); /* Allocate some memory */ ptr = (char *)malloc(BIG * sizeof (*ptr)); /* Check allocation succeeded */ if (ptr == NULL) { perror("malloc()"); exit(EXIT_FAILURE); } /* Copy another string */ (void) strcpy(ptr, "I'm going on the heap"); /* Free memory */ free(ptr); /* Call some functions */ a = 5; b = sumup(a); swap(&a, &b); return (0); }