[Noob]String memory allocation

D

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
 
R

Random832

2006-11-15 said:
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]
 
D

Default User

DJP said:
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
 
D

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.
 
R

Random832

2006-11-15 said:
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.
 
S

Simon Biber

Default said:
Not usually.


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.
 
D

Default User

Simon said:
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
 
D

Default User

DJP said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top