473,320 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

[Noob]String memory allocation

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
7 1887
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Olumide | last post by:
In the following C++ class, MyClass{ // Members } From which I create two objects (using the default constructor), (1) MyClass *object1 = new MyClass(); // Stack (2) MyClass object2 =...
8
by: Ivan Shevanski | last post by:
Alright heres another noob question for everyone. Alright, say I have a menu like this. print "1. . .Start" print "2. . .End" choice1 = raw_input("> ") and then I had this to determine what...
2
by: Donald | last post by:
I am trying print the length of a managed array, but I cannot figure out how to correctly cast the result so String::Format will use it. I wrote a simple test to duplicate the problem (below) --...
9
by: pointer noob | last post by:
Hi, I am trying to write a bit of code to iterate through memory addresses and if the address is divisable by 2 then write one byte, if not write a different byte. I am having trouble with the...
10
by: stylecomputers | last post by:
Hey guys, I am absolutely new to Linux programming, with no w######s programming experience except a small amount of C++ console apps. Reasonably new to Linux, BSD etc, got good sound networking...
2
by: jaime_v | last post by:
This is for all those STL-heads out there, this mught be trivial for you but I am a noob with the STL. I am having to implement a system in which I wil have many (thousands) of containers, I have...
4
by: jobs | last post by:
Three noob questions. Please help. 1. Is there a way to have arguments on subroutine that are optional? 2. Say I need to convert datetime to string..in this format : 2006-09-07...
12
by: Simon | last post by:
Hi all, I'm having a baffling problem with a windows service that I'm working on. Basically, I am using a typed dataset to insert a large number of rows into an SQL Server 2005 database. But...
11
by: Bryan Parkoff | last post by:
I want to know how much static memory is limited before execution program starts. I would write a large array. The large array has 65,536 elements. The data size is double word. The static...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.