I am following the book "C Primer Plus" and encounter a problem to understand the regions of memory. In the book, it states:
Typically, a program uses different regions of memory for static objects, automatic objects, and dynamically allocated objects. Listing 12.15 illustrates this point.
// where.c -- where's the memory?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int static_store = 30;
const char * pcg = "String Literal";
int main(void)
{
int auto_store = 40;
char auto_string[] = "Auto char Array";
int *pi;
char *pcl;
pi = (int *) malloc(sizeof(int));
*pi = 35;
pcl = (char *) malloc(strlen("Dynamic String") + 1);
strcpy(pcl, "Dynamic String");
printf("static_store: %d at %p\n", static_store, &static_store);
printf(" auto_store: %d at %p\n", auto_store, &auto_store);
printf(" *pi: %d at %p\n", *pi, pi);
printf(" %s at %p\n", pcg, pcg);
printf(" %s at %p\n", auto_string, auto_string);
printf(" %s at %p\n", pcl, pcl);
printf(" %s at %p\n", "Quoted String", "Quoted String");
free(pi);
free(pcl);
return 0;
}
Run the code and get:
static_store: 30 at 0x10a621040
auto_store: 40 at 0x7ffee55df768
*pi: 35 at 0x7fbf1d402ac0
String Literal at 0x10a620f00
Auto char Array at 0x7ffee55df770
Dynamic String at 0x7fbf1d402ad0
Quoted String at 0x10a620f9b
the book's conclusion:
As you can see, static data, including string literals occupies one region, automatic data a second region, and dynamically allocated data a third region (often called a memory heap or free store).
I could figure out they are of different address. How could I assure that they are of different regions?