8

Possible Duplicate:
In C++, why should new be used as little as possible?

Where are automatic variables allocated in C++? On the stack or the heap?

Also, I read in 7.9 — The stack and the heap that all memory allocated on the stack is known at compile time. Is it true? Does it mean that only static memory allocation happens on the stack?

Also, please mention links, references to a a complete explanatory text about memory allocation in C++.

Community
  • 1
  • 1
Nitin Garg
  • 2,019
  • 5
  • 25
  • 50
  • 2
    See [this question](http://stackoverflow.com/questions/6500313/in-c-why-should-new-be-used-as-little-as-possible) for a detailed explanation of C++ variable allocation. – André Caron Jul 06 '11 at 20:09
  • 6
    C++ is a *language*, not an implementation, so it doesn't have concepts like a "stack"; that stuff is left up to the compilers and specific platforms. It could be the case that every object with automatic storage duration is allocated on the heap. In practice, though, it's always the stack. But keep in mind if you really need to know this information, you'll be asking about a platform, not about C++. Let C++ be C++ the language and don't worry. Also, don't learn C++ from the internet, you should get a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – GManNickG Jul 06 '11 at 20:19
  • 1
    @GMan: Automatic variables aren't always on the stack in practice. See my answer. – Peter Alexander Jul 06 '11 at 20:38
  • @Peter: Yes, perhaps always was a bit strong, but certainly most of the time. Registers are a very scarce resource. – GManNickG Jul 06 '11 at 21:40

4 Answers4

19

C++ does not have the concept of a stack or heap, it is an implementation detail as far as the language is concerned.

That being said, every implementation I know of uses the stack to manage the lifetime of local variables. However, many local variables may end up living entirely within registers and never touch the stack, and some local variables may be optimised out completely. Just because you declare an automatic variable doesn't mean that it will be put on the stack.

e.g.

int main()
{
    int x = rand();
    int y = 2;
    cout << x << y << endl;
    return 0;
}

In this code, with optimisations on, the variable y will almost certainly be removed completely and variable x will probably be given its own register. It's unlikely that either of those variables will ever exist on the stack.

Peter Alexander
  • 50,304
  • 10
  • 114
  • 163
  • 2
    Well said. I hate when the "standards" police jump in and just stop there with a quote from the holy standard. Great examples. – Vinicius Kamakura Jul 06 '11 at 20:53
  • 1
    There is also the consideration that member variables might refer to dynamic memory even though the surrounding object has automatic storage duration. If `vector v;` is automatic, and thus likely on the "stack", `v[0]` is still almost certainly on the "heap". – Dennis Zickefoose Jul 06 '11 at 20:56
  • @Dennis: You are right in saying that the contents of a vector will (probably) be on the heap even when the vector itself is on the stack, but its member variables aren't declared with automatic storage (automatic storage class can only be used with local variables, including parameters) so it seems to be separate from the question being asked. – Peter Alexander Jul 06 '11 at 21:04
  • 1
    Yes, I agree. My comment was mostly meant to expand on the idea that "local variable ==> stack" isn't a particularly useful simplification, even if the language weren't silent on the idea of a stack actually existing. – Dennis Zickefoose Jul 06 '11 at 21:15
2

Automatic (local) variables are not allocated on the heap. They will either live on the stack or in a register.

And yes, all stack allocations are know at compile time.

The link you referenced is actually a pretty good resource for describing how c++ handles memory.

sshannin
  • 2,597
  • 1
  • 18
  • 25
2

Yes, auto vars are allocated, most commonly, on the stack (the C++ standard doesn't specify "stack" but it is safe enough to assume that).

You can use the stack to allocate dynamic memory with the alloca() function, but that is not very good practice, more on that in this question: Why is the use of alloca() not considered good practice?

Community
  • 1
  • 1
Vinicius Kamakura
  • 7,299
  • 1
  • 25
  • 42
0

In addition to locating some local variables in registers or on the stack, some compilers may choose to re-use specific registers or memory if it can be determined at compile time that the usable lifespan of an object is limited to regions of code.

For example,

{
int a;
// do something with a

int b;
// do something with b but nothing with a
}

The compiler will notice that a and b are the same size but a is not used anywhere during b's lifetime. The compiler will map both a and b to the same memory (stack or register), saving a bit of space.

jbruni
  • 1,238
  • 8
  • 12