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";

2
  • duplicate stackoverflow.com/questions/7943628/… Commented Nov 3, 2011 at 19:26
  • @Mr.32 True, although that question is 3/5 voted as a duplicate of a question it is not a duplicate of. Commented Nov 3, 2011 at 19:43

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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";
@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.
@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.
@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.
@Als I searched and found this question: 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?
|
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.

4 Comments

Oops ... @cricutar beat me to the answer :-)
So array initialization like char ptr[]="Hello"; will be allocated on the stack??
Yes, if this is the definition of an automatic (stack local) variable (not of a static or global one).
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.