if this was declared within a function, would it be declared on the stack? (it being const is what makes me wonder)
void someFunction()
{
const unsigned int actions[8] =
{ e1,
e2,
etc...
};
}
Yes, they're on the stack. You can see this by looking at this code snippet: it will have to print the destruction message 5 times.
struct A { ~A(){ printf( "A destructed\n" ); } };
int main() {
{
const A anarray [5] = {A()} ;
}
printf( "inner scope closed\n");
}
As I understand it: yes. I've been told that you need to qualify constants with static
to put them in the data segment, e.g.
void someFunction()
{
static const unsigned int actions[8] =
{
e1,
e2,
etc...
};
}
If you don't want your array to be created on stack, declare it as static. Being const may allow the compiler to optimize whole array away. But if it will be created, it will be on stack AFAIK.
Yes, non-static variables are always created on the stack.
anarray
, all elements will have to be destroyed by the time the program exits. You should add scope blocks and a print to be sure:struct A { ~A(){ printf( "A destructed\n" ); } }; int main() { { const A anarray[5]; } printf( "main exiting\n" ); };
– arolson101 Sep 08 '09 at 14:50