M
Martin Dickopp
Rolf Magnus said:Martin said:void f1(int n) { vector<int> x(n); /* C++ */ }
void f2(int n) { int x[n]; /* C99 only */ }
void f3(int n) { int* x = new int[n]; /* C++ */ delete [] x; }
void f4(int n) { int* x = (int*)malloc(n*sizeof(int));
/*...*/ free(x); }
In all of these cases it makes sense for the compiler to allocate x
on the stack instead of the heap. However, AFAIK, only in case of f2
the compiler is required to do so.
No, neither in C nor in C++ a heap and/or stack is even required to
exist. How/where the implementation allocates memory is unspecified.
I find it quite strange that C++ doesn't require a stack to exist, but
OTOH dictates that when an exception occurs, "stack unwinding" is done.
This term doesn't have a meaning if there is no stack, does it?
How the C++ standard uses the term "stack unwinding" is defined in 15.2#3:
| The process of calling destructors for automatic objects constructed on
| the path from a try block to a throw-expression is called "stack
| unwinding."
Except for the STL class stack, I don't see the term "stack" used in any
other sense in the standard. Therefore, you are correct that a stack in
the above sense is required to exist, but there is no requirement that a
stack to allocate memory for objects with automatic storage duration from
must exist.
Martin