By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,400 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

[Noob]String memory allocation

P: n/a
DJP
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? 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?

Hope to hear from someone out there.

Thanks.

DJP

Nov 15 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
2006-11-15 <11*********************@m7g2000cwm.googlegroups.c om>,
DJP wrote:
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)
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]
Hope to hear from someone out there.
Nov 15 '06 #2

P: n/a
DJP wrote:
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?
These are not portable terms. The only really important thing is
storage duration. Where things are stored is implementation specific.

In the first case, the address of the first character of an anonymous
array is stored in a pointer. That array itself has static storage
duration, which means that it is around for the entire program. It is
undefined behavior to modify a string literal, and it would be better
to make str1 a const pointer.

The second form depends on how it's declared. If it is outside of any
function, or is declared with the "static" keyword, then it will also
have static storage duration. Otherwise, it has automatic, which means
it ceases to exist when the program leaves the enclosing scope.
Also, unless explicitly malloc'ing a string
buffer are we not required to free it?
Not usually.
Or are there other
circumstances in which we are required to free?
Unless it's a libary function that returns allocated memory.
Hope to hear from someone out there.


Brian
Nov 15 '06 #3

P: n/a
DJP
(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)
Thanks for your response. Also, can you elaborate a little bit about
the "static nonwritable area of storage" and the "static writable
storage" that you mention? Thanks.

Nov 15 '06 #4

P: n/a
2006-11-15 <11**********************@i42g2000cwa.googlegroups .com>,
DJP wrote:
>(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)

Thanks for your response. Also, can you elaborate a little bit about
the "static nonwritable area of storage" and the "static writable
storage" that you mention? Thanks.
It means that it doesn't go away until you exit the program and you
cannot*, or, for writable, can, write to it. [OT] On some systems, these will
be loaded into memory from disk when the program is started in different
'segments' or 'sections', and the nonwritable areas will be marked as
such in the memory protection [/OT]

*Note that your implementation won't necessarily stop you from trying.
Instead, bad things might happen later on at an unrelated point in the
program, or when you try to port it to another system.
Nov 15 '06 #5

P: n/a
Default User wrote:
DJP wrote:
>Also, unless explicitly malloc'ing a string
buffer are we not required to free it?

Not usually.
>Or are there other
circumstances in which we are required to free?

Unless it's a libary function that returns allocated memory.
The only standard library functions that return allocated memory are
malloc, calloc and realloc.

Any other C function in a library may of course return a pointer that
must be freed[1], but that pointer must have been returned by a malloc,
calloc or realloc call within the library[2].

[1] Or a pointer to somewhere within a memory block that must be freed.
It may not be a pointer to the first byte of that memory block.

[2] Or within another library or other code that is linked into the
final program.

--
Simon.
Nov 15 '06 #6

P: n/a
Simon Biber wrote:
Default User wrote:
DJP wrote:
Also, unless explicitly malloc'ing a string
buffer are we not required to free it?
Not usually.
Or are there other
circumstances in which we are required to free?
Unless it's a libary function that returns allocated memory.

The only standard library functions that return allocated memory are
malloc, calloc and realloc.
I don't see the word the word "standard" in my sentence above.
Any other C function in a library may of course return a pointer that
must be freed[1], but that pointer must have been returned by a
malloc, calloc or realloc call within the library[2].
True, but as library routines are often opaque to the user, a new user
won't necessarily know that. From their point of view, some library
routine *cough* strdup() *cough* could return allocated memory, they
will need to check.

Brian
Nov 15 '06 #7

P: n/a
DJP wrote:
(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)

Thanks for your response. Also, can you elaborate a little bit about
the "static nonwritable area of storage" and the "static writable
storage" that you mention? Thanks.
Besides the information you received elsewhere, you may want to review
the FAQ on the subject:
<http://c-faq.com/decl/strlitinit.html>
In fact, reviewing all the string, array, and pointer entries would be
a good idea.

Brian
Nov 15 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.