6

For the C statement given below i would like to know where the memmory allocation will take place.

char* ptr="Hello";//ptr is a automatic variable

then the pointer variable ptr will be allocated on the stack,but where will this string "Hello" be allocated. Is it on Stack or on Heap? And what about memory allocation for initialization statement like char ptr[]="Hello";

prajul
  • 1,216
  • 2
  • 15
  • 27

2 Answers2

12

The standard doesn't say (it doesn't know about "stack", "heap" etc). But in practice the answer is: Neither. The string literal will be stored in the data section, usually in a read-only page.

As a side note, as Als mentions in the comments, it's undefined behavior to attempt to modify a string literal.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • 3
    Apt to mention that an attempt to modify the string literal will result in an Undefined Behavior. And hence the declaration should ideally be `const char *ptr = "Hello"`; – Alok Save Nov 03 '11 at 19:04
  • 2
    @Als I do not see how that follows. It may be the programmer's intention to later make `ptr` point to another memory zone, and then to use it to modify that zone. The declaration should be `const char *` if an only if the programmer wants a pointer to `const char`. – Pascal Cuoq Nov 03 '11 at 19:14
  • @PascalCuoq: Please lookup some previous Questions, there are a zillion questions here which address this already.I don't see it to be benificial to replicate all of it over here again. – Alok Save Nov 03 '11 at 19:16
  • @Als I am sorry, but I have no idea either what you mean nor what I am condescendingly asked to search for. You don't have to repeat, a link will suffice. – Pascal Cuoq Nov 03 '11 at 19:20
  • @Als I searched and found this question: http://stackoverflow.com/questions/2245664/string-literals-in-c It says I can declare `ptr` as `char*`, assign it a string literal, then make it point somewhere else and use `ptr` to modify that somewhere. Is it the search you meant? – Pascal Cuoq Nov 03 '11 at 19:37
  • @PascalCuoq: Ah okay,I was under the impression that this is tagged C++.In fact C++ Standard deprecates usage of string literals without the `const`, C doesn't. – Alok Save Nov 03 '11 at 20:06
3

With static strings like in your example, the string is not really allocated. Space for it is made in the executable itself, and the above assignment just sets "ptr" to the address of that space.

I'm not sure if this is implementation dependent or not, but the string is usually in protected memory so you cannot change it ... only point to it.

In UNIX, you can see the static strings in an executable by using the "strings" command on an executable.

Ron
  • 1,305
  • 2
  • 14
  • 22
  • Oops ... @cricutar beat me to the answer :-) – Ron Nov 03 '11 at 19:05
  • So array initialization like char ptr[]="Hello"; will be allocated on the stack?? – prajul Nov 03 '11 at 19:13
  • Yes, if this is the definition of an automatic (stack local) variable (not of a static or global one). – Basile Starynkevitch Nov 03 '11 at 19:17
  • @prajul ... memory for the pointer variable "ptr" will be allocated on the stack, and will be initialized to point to the data for "Hello". The data for "Hello" will actually be in the executable's data space and is not allocated at runtime but is a part of the executable when the executable is loaded into memory. – Ron Nov 03 '11 at 22:38