2006-11-15 <1163550441.082592.27940@m7g2000cwm.googlegroups.c om>,
DJP wrote:
Quote:
Hi,
>
I had sort of a noob question on memory allocation for strings.
>
char *str1 = "Hello World!";
char str2[] = "Hello World!";
>
In the above bit are both str1 & str2 stack allocated or heap allocated
or otherwise?
Well - the off-topic answer is, it depends on whether this is inside
a function or not. They will never be "heap allocated" in the sense you
find on typical implementations, only malloc (and functions that call
malloc, some of which can be surprising) does that.
(mostly back to on-topic) In either case, the first one is in a static
nonwritable area of storage. Outside a function, the latter is in static
writable storage, but inside a function, it is in "automatic" storage
(that is, what you might call the stack)
Quote:
Also, unless explicitly malloc'ing a string buffer are we
not required to free it? Or are there other circumstances in which we
are required to free?
You are never required to, nor permitted to, free() or realloc()
any pointer whose value did not originate from a malloc(), calloc(), or
realloc() call that you yourself made. [OT] or certain non-standard
functions which are documented to call malloc [/OT]
Quote:
Hope to hear from someone out there.