0

Inside a C function, if I declare something like char *s = "Hello"; Where will the memory for the "Hello" string be allocated? Will it be on the stack or the heap?

If it is on the heap, will there be any problem if I return the address of that location (which is 's') from the function??

Thanks.

4
Contributors
6
Replies
8
Views
7 Years
Discussion Span
Last Post by dumbncool
0

Thanks for the answer. I have read the link which you gave and am still confused. I was thinking any memory which my program needs has to be allocated from the stack or the heap. But you said Neither.

Also in that link, the author talks about char *p = "world"; p is a pointer which points to the memory location where "world" is stored.
But he doesn't answer exactly where is this string "world" stored.

0

It's stored in the same place as a global variable would be. The "program memory" that is neither "stack" nor "heap". The stuff with static storage duration.

Edited by Dave Sinkula: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.