debugger-lab/memmap.c
2026-10-20 23:17:20 -06:00

44 lines
1.6 KiB
C

/* Where a C program keeps its things. Build with -g -O0 and read the addresses.
*
* Every address printed here changes between runs, so read the pattern rather
* than the numbers: which addresses sit near each other, and which one belongs
* to the pointer instead of the text.
*/
#include <stdio.h>
int counter = 7; /* file scope, alive for the whole run */
static int calls; /* file scope, and invisible outside this file */
const char *label = "switchback"; /* the pointer is data; the text is read-only */
static void show(const char *what, const void *where) { printf("%-24s %p\n", what, where); }
static int add_one(int n) {
int local = n + 1; /* automatic: gone when this call returns */
calls = calls + 1;
printf(" inside add_one: local=%d at %p, calls=%d at %p\n", local, (void *)&local, calls,
(void *)&calls);
return local;
}
int main(void) {
int here = 41;
int row[4] = {1, 2, 3, 4};
char word[] = "quiet"; /* your own cells, filled at run time */
const char *literal = "quiet"; /* the pointer, not the letters */
int got;
show("global counter", (const void *)&counter);
show("static calls", (const void *)&calls);
show("pointer label", (const void *)&label);
show("text label points at", label);
show("local here", (const void *)&here);
show("local row", row);
show("local word", word);
show("pointer literal", (const void *)&literal);
show("text literal points at", literal);
got = add_one(here);
printf("\nadd_one(%d) returned %d, calls is now %d\n", here, got, calls);
return 0;
}