7

Possible Duplicate:
Is a string literal in c++ created in static memory?
C++ string literal data type storage

In this code:

const char * str = "hello world";

If I understand correctly a pointer is 4 or 8 bytes, which I guess would be allocated on the stack. But where is the memory for the "hello world" allocated and stored?
Or what does str point to exactly?

Community
  • 1
  • 1
Josh
  • 6,046
  • 11
  • 52
  • 83

3 Answers3

16

It's not allocated. It's generally stored in your program's code segment or on the stack That's up to the compiler. Either way, it points to a null-terminated array of characters.

paddy
  • 60,864
  • 6
  • 61
  • 103
  • 6
    Or in the data segment, or in a "readonly data" segment, or some other place that the compiler/linker decides is suitable. – Mats Petersson Jan 22 '13 at 22:05
  • 1
    Depending on your definition of memory allocation, it actually is allocated. You seem to miss the difference between dynamic (variables declared `auto` or space obtained via `malloc`) and static allocation. – fuz Jan 22 '13 at 22:09
  • @FUZxxl Yes that's a good point. But normally when we use the term 'allocated' in the context of strings in C, we are talking about dynamic allocation. I was on that buzz. Thanks for your comment. +1 – paddy Jan 22 '13 at 22:11
  • That's why I said *depending on your definition*. Thanks for your clarification. – fuz Jan 22 '13 at 22:12
6

C has no stack or heap. C says that "hello world" is a string literal and that string literals have static storage duration.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • AKA implementation defined? – jn1kk Jan 22 '13 at 21:54
  • 2
    @jsn - not even as strong a requirement as "implementation defined." It can put them anywhere it likes and it doesn't even have to tell you. – Carl Norum Jan 22 '13 at 21:55
  • @jsn implementation-defined requires the implementation to document somewhere how it is implemented. So to the question, is the *where* implementation-defined the answer is clearly no, it isn't. – ouah Jan 22 '13 at 21:59
  • 3
    Umm... C has both a stack and a heap (well, most implementations do). – James Curran Jan 22 '13 at 22:00
  • 8
    @JamesCurran: it has recently become *very* fashionable to respond to questions about stack and heap by saying that C (or C++) doesn't have them. Which of course is true in that the standard mandates no such data structures for automatic variables and allocations from the free store. Presumably if the questioner had asked where string literals are stored in a "typical" implementation, or some such, then a different way would be found to ignore the intended question. – Steve Jessop Jan 22 '13 at 22:02
  • @Steve, true enough... though I think it's odd that when discussing C#/.NET, there is very direct use of the terms "stack" and "heap" even though the heap probably isn't a heap, and the stack may not be a stack. – James Curran Jan 22 '13 at 22:10
  • 3
    @JamesCurran: That's because C# is a single-vendor product, and MS can essentially say "our implementation *defines* the language" (like Perl and PHP). By contrast, the C standard attempts to capture the high-level structure of compilers spanning decades of time and hundreds of implementations, and is worked out carefully by a relatively large group of people. – Kerrek SB Jan 22 '13 at 22:14
  • @KerrekSB : You're missing the point. I don't think those terms are used in the C# specification either. They are being applied to .NET data structures due to their rough similarity to C implementations from 30 years ago --- people are so used to using those terms, they don't realize they probably don't apply anymore (since many don't understand the specific types of data structures that "heap" and "stack" refer to) – James Curran Jan 22 '13 at 22:43
  • @JamesCurran: Are you sure (I don't know). But C# is MS response to Java (and modeled heavily on that design) which does explicitly have both a stack and a heap. – Martin York Jan 23 '13 at 03:53
  • @SteveJessop: A recent development? – Martin York Jan 23 '13 at 03:57
  • @LokiAstari: people have always corrected the terminology. What I've seen a few times recently is people not moving past the terminology to the actual question, that is to say talking only about the standard and never about any implementations. Of course it's up to the implementation, but just because the questioner doesn't specify a particular implementation IMO doesn't mean the question is *solely* about the standard. – Steve Jessop Jan 23 '13 at 07:01
  • @SteveJessop and the other downvoters. (1/2) *fashionable?* As I don't think I'm a newcomer regarding C let me answer this. *C has no stack* is not my answer, it's the preamble. C object allocation is not explained using stack and heap notions. My answer is that the string literal has *static storage duration*. Static storage means permanent storage which should answer OP question about string literals and the stack or at least point him to the right direction. – ouah Jan 23 '13 at 09:54
  • @SteveJessop (2/2) Storage duration is a basic but yet important concept in C. You cannot go into implementations before talking about storage duration, it's a prerequisite. Notice that none of the other answers mentions storage duration. Of course I could then have explained in my answer what is static storage duration and give some information on how is static storage duration implemented by gcc on x86 but I let this for a different question. – ouah Jan 23 '13 at 09:55
  • @ouah: I'm not one of the downvoters. I rarely downvote unless an answer is actually false or misleading. I'm not a *fan* of this answer, but it's neither of those things. – Steve Jessop Jan 23 '13 at 12:31
  • @LokiAstari: I'll grant the at the JVM spec does use the terms "Stack" and "Heap" (which surprised me). But I'm pretty sure they are using "heap" wrong. A "heap" is a specific type of data structure, which don't lend itself well to automatic garbage collection. – James Curran Jan 24 '13 at 14:25
  • @JamesCurran: I think you will find the term "heap" like "static" is overloaded. In this context they are not referring to the heap data structure but a generic area of memory used for dynamic memory allocation. See definition here: http://www.memorymanagement.org/glossary/h.html Or the better description of Stack and Heap http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap. But I have no idea if MS duplicated this idea for C#. This may be a good question for SO. – Martin York Jan 24 '13 at 16:29
  • @SteveJessop While it's become fashionable (and irritating), the truth is people comment either way. If you say "stack" --comment will be "no stack in C standard". If you say "C standard defines no such thing", comment will be "most implementations do. Can you link to one which doesn't?". I wish people leave these nitpicking habits and spend their time on better things :) I am sure the *comments* have attracted downvotes for a technically correct answer. Sometimes, you don't have to downvote. Just comment as if the answer is wrong, that's enough :D – P.P Feb 18 '13 at 08:46
6

Essentailly, that is compiled as if you had written:

const static char helloworld[12] 
             = {'h', 'e', 'l', 'l', 'o',' ','w', 'o', 'r', 'l', 'd', '\0'};

const char * str = helloworld;

The array would normally be placed in some read-only section of memory, probably near the executable code.

Depending on where it's defined, str will be in the stack or global memory space.

James Curran
  • 101,701
  • 37
  • 181
  • 258