0

Consider the following function:

void Foo()
{
   std::string s = "Hello";
}

In the above case where is the string allocated? Some pages (like this) says it's on the heap and Microsoft says it depends on the size. And how can I make it for sure on the heap and on the stack (if I want to avoid using new())?

Consider another function:

void Foo2()
{
   char *c = "Hello";
}

In this case I supposed it is on the stack, but as soon as I checked its assembly via MS Visual C++ I think it is on the heap. Is it true?

And where is char c[] = "Hello"?

I'm using VC12, but I would like to know what about g++ too.

It's important me because of the exception handling. Because in case of an unwinding stack I really lose data if it's created on the heap and in other hand there is no problem, but the stack can be too big.

Niall
  • 30,036
  • 10
  • 99
  • 142
  • possible duplicate of [C String literals: Where do they go?](http://stackoverflow.com/questions/2589949/c-string-literals-where-do-they-go) – Marco A. Sep 15 '14 at 12:38
  • The 3 ways are correct in regard of exception (except the missing `const` in the second one). – Jarod42 Sep 15 '14 at 12:38
  • 2
    In `Foo2` the string is *not* on the stack, only the variable `c` is on the stack. The actual string data is in some read-only segment allocated by the program loader. The last case though, `char c[] = "hello";`, here the whole array is on the stack. – Some programmer dude Sep 15 '14 at 12:40
  • But it should also be noted that the C++ specification doesn't mention anything about "stack" or "heap", those are just implementation details left to the compiler, run time system, operating system and hardware. – Some programmer dude Sep 15 '14 at 12:41
  • 4
    Also, you should not really need to care where strings are stored, in C++ just use `std::string` and everything will work out in the end. – Some programmer dude Sep 15 '14 at 12:42
  • "It's important me because of the exception handling" - you shouldn't need to worry about that. Like any properly designed type, `std::string` will correctly release its memory on destruction during stack unwinding, whether or not it allocated that memory on the heap. – Mike Seymour Sep 15 '14 at 12:53
  • I'm sorry but could I get some vote up? :-) –  Sep 15 '14 at 12:58
  • Now I see the standards says nothing about this. Thanks. The two answers below are very similar and therefore I could accept the first one. –  Sep 15 '14 at 13:00

2 Answers2

0
void Foo()
{
   std::string s = "Hello";
}

Implementation dependant.
As you said, MSVC's standard library will allocate to heap for large strings and local storage (stack) for smaller strings.
How it is stored will vary based on the platform/version of the standard library that you use. The storage is not defined in the C++ spec, just the functionality.

void Foo2()
{
   char *c = "Hello";
}

Pointer c is on the stack, but the actual data is read-only, and not in the stack or on the heap.
It is actually stored in a read-only data block in memory.

void Foo3()
{
   char c[] = "Hello";
}

Array c is stored on the stack. When it is initialised, it is created using the string "Hello" from the same source as in Foo2(). However as c is on the stack, *c points to data that can be modified.

Baldrickk
  • 4,291
  • 1
  • 15
  • 27
0

std::string manages itself. In the code:

void foo() {
    std::string s = "Hello";
}

s is a stack variable. How the string "Hello" is stored is up to std::string You don't need to worry about it - unless your particular STL implementation is buggy, everything will work out.

In:

void bar() {
    char* c = "Hello";
}

You've declared a local pointer that points to the literal string "Hello". It's up to the compiler where this goes, but it's likely in a read only segment of memory. It's worth noting that, in this case, the string "Hello" is a constant - in C++98, trying to modify this is technically deprecated (and I believe an outright error in C++11).

In:

void baz() {
    char c[] = "Hello";
}

The entire string "Hello"; is stored in a local 6 element character array (i.e. on your stack).

The solution with std::string is preferable for most cases.

xen-0
  • 709
  • 4
  • 12