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...
};
}
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...
};
}
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, 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");
}