cpointersmemory-management

Does multiple pointer allocation may cause an overflow?


I'm trying to understand memory management with C. So far I have a clear overview, but there's a question that is not that obvious for me.

If I declare a char pointer and assign it some string, then the pointer contains the address to that string (that is allocated somwhere).

char *pchar1 = "Hello World OLD";
unsigned long x = (unsigned long)pchar1; //Note that I'm storing the value of this addr for further usage
printf("pchar1 content: %s\n", pchar1); // Hello World OLD
printf("Adress of pchar1: %lu\n\n", pchar1); //<Some Random Addr>

If I assign another string to the pointer, then it aims to another address.

pchar1 = "Hello World NEW";
printf("pchar1 content: %s\n", pchar1); // Hello World NEW
printf("Adress of pchar1: %lu\n\n", pchar1); // <A different addrs than the fisrt one>

If I try to read the old memory location I found that there's still my old string

printf("Old addr stores in 'x': %lu\n", x); // Hello World OLD
printf("Reading old pchar1 mem addr (from x): %s\n\n", (char *)x); // <The First Address>

My question is, as memory used is not being freed and If I don't store the value for old memory addr then I lost track of it, may something like this cause an overflow?

while(1){
        pchar1 = "Hello World OLD"; //Stores this string in a different addres
        pchar1 = "Hello World NEW"; //Stores this string in a different addres
    }

The following this the whole code:

#include <stdio.h>
#include <stdlib.h>


int main(int argc, char *argv[]){
    char *pchar1 = "Hello World OLD";
    unsigned long x = (unsigned long)pchar1;

    

    printf("pchar1 content: %s\n", pchar1);
    printf("Adress of pchar1: %lu\n\n", pchar1);
   
    pchar1 = "Hello World NEW";
    printf("pchar1 content: %s\n", pchar1);
    printf("Adress of pchar1: %lu\n\n", pchar1);

    printf("Old addr stores in 'x': %lu\n", x);
    printf("Reading old pchar1 mem addr (from x): %s\n\n", (char *)x);

    //Overflow?
    while(1){
        pchar1 = "Hello World OLD"; //Stores this string in a different addres
        pchar1 = "Hello World NEW"; //Stores this string in a different addres
    }

    printf("\nSuccess!...\n");
    return 0;
}

Thanks in advance!


Solution

  • String constants are typically stored in a static read-only section of your program's memory. Such constants are available for the full lifetime of the program, whether you keep a pointer to them or not.

    Also, if a string constant appears more than once in a program, it's not uncommon for each of those instances to refer to the same piece of memory.

    In this particular case:

    while(1){
        pchar1 = "Hello World OLD"; //Stores this string in a different addres
        pchar1 = "Hello World NEW"; //Stores this string in a different addres
    }
    

    You have two string constants stored in that read-only memory segment. On each iteration of the loop pchar1 is modified to point to one of those string constants. So it's not creating more memory that's being used.

    If on the other hand you have something like this:

    void foo(void)
    {
        char s[] = "Some very very very long string";
        foo();
    }
    

    And you call foo, there will be an array on the stack which stores the string "Some very very very long string" once for each iteration of the function, and eventually you will run out of stack space, and the larger s is the quicker that will happen.