1

I have some very basic questions regarding pointers and memory allocation.

  1. In below code where does pointer c actually point to? In other words where does the string "xyz" get stored in memory (stack/heap etc.)?

  2. What will happen to memory location allocated for a as I am not using it anymore?

  3. Code seems to work well if I un-comment the commented section. What's happening with the memory in that scenario?

     #include <stdio.h>
    
     main()
     {
         char *c;
         //c = (char *)malloc(2);
         c = "a"; 
         c = "xyz" ; 
         printf("%s",c);
         return 0;
     }
    

Output:

xyz

Edit:

After reading few of the answer and first comment another question came up in my mind:

  1. In below case, where do the strings get stored? Can I alter them later on?

    char *c[] = {"a","xyz"};
    
Yun
  • 3,056
  • 6
  • 9
  • 28
Vagish
  • 2,520
  • 19
  • 32
  • Referring 1: http://stackoverflow.com/q/2589949/694576 – alk Jun 09 '14 at 06:43
  • @alk the accepted answer in the question you refereed suggest that use array for mutable string,If I am correct then in that case the string is stored in read/write section(stack/heap) instead of read only section? – Vagish Jun 09 '14 at 07:08
  • You might like to closley read the answer again. However all answers together make clear that where to store literals is highly implementation dependant. – alk Jun 09 '14 at 07:27

4 Answers4

5
  1. The specific details are implementation dependent, but in most common implementations, literal strings like "a" and "xyz" are stored in the text section of the program, like the machine code that implements the program. Assigning a = "xyz"; sets a to point to that location in memory.

  2. The memory for "a" is unaffected. However, an optimizing compiler may notice that c was never used between that assignment and being reassigned, so it could simply ignore the first assignment, and never allocate any space for "a" at all.

  3. The memory you allocated with malloc() stays allocated until the program ends. Allocating memory without freeing it is called a memory leak, and you should try to avoid it.

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

1.In below code where does pointer 'c' is actually pointing to?In other words where does the string "xyz" get stored in memory(stack/heap etc.)?

will place xyz the read-only parts of the memory and making c a pointer to that, a variable of type pointer-to-char, called c , which is initialized with the location of the first character in that unnamed, read-only array.you created automatic storage (often called the stack). Here you just point to memory in text section.

2.What will happen to memory location allocated for"a"' as i am not using it anymore?

As per your code you not allocated any memory just you assign string literals to that.If you allocate then need to free it.

3.Code seems to work well if I un-comment the commented section.Whats happening with memory in that scenario?

If you un comment the allocation statement to allocate memory then also it point to you literal string but you need to free that memory.

Now for case

char *c[] = {"a","xyz"};

when you declare and initialize words, it'll be allocated on the stack as any other automatic array and its items will be assigned to pointers to the beginning of each string constant.Also to alter this string may illegal.

Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
0

"xyz" and "a" are string literals which is mostly available in string table.

"xyz" is printed because it is recently assigned to that pointer.

Jeyaram
  • 9,158
  • 7
  • 41
  • 63
0
  1. To a place in the heap/stack in the READ-ONLY part of the memory which the string is in. When assigning a string literal directly into a pointer, the program searches for that string in the memory, if it exists through the short search he's doing, it'll point to it, if it doesn't - it will create it. Either way it's read only so it'll be the same as a const char* so you can't change it (of course you can somehow manipulate it, maybe by another pointer or so).
  2. Nothing, it'll stay unaffected.
  3. What's happening is that malloc returns a pointer and you just ignore it, you go to another address containing a and it will not have the same influence as strcpy(c, "a"); as you ignore the allocated memory and its pointer - you do not free it. Generally, nothing's happen if you don't free the memory (I mean, at the end of the program it is freed automatically by the OS) but it WILL take memory within the program so if i'd allocate, let's say 1000000 bytes (assuming it succeeded), allocating more heap memory would be a problem :P
  4. about the other question... You can't alter them through that pointer, try and it will throw an interrupt in the middle of the program and it'll probably stop responding.
Zach P
  • 1,731
  • 11
  • 16