tags:

views:

208

answers:

4

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...
  };
 }
+4  A: 

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...
         };
}
Kim Gräsman
+2  A: 

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.

Tadeusz Kopec
+5  A: 

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");
}
xtofl
Out of curiosity -- does anyone know if this behavior is defined? That is, if the storage of constants is spelled out in the standard? Thanks.
Kim Gräsman
More or less. The standard doesn't talk about a "stack" at all. But it does say that variables default to *automatic storage duration*, that is, they are destroyed when they go out of scope. And the way this is implemented is with a stack.So no, you are not guaranteed that it it allocated on the (or *a*) stack, but you are guaranteed that it behaves as if it does
jalf
const-ness doesn't affect that. `static` would have given it static storage duration. Const just specifies that it may not be modified, it doesn't affect lifetime.
jalf
Mkes sense, thanks!
Kim Gräsman
This is a bad example. Regardless of the storage location of <code>anarray</code>, all elements will have to be destroyed by the time the program exits. You should add scope blocks and a print to be sure:<code>struct A { ~A(){ printf( "A destructed\n" ); } };int main() { { const A anarray[5]; } printf( "main exiting\n" );};</code>
arolson101
@arolson: Thanks, you're right. BTW, you can put code in comments (and in other text) by surrounding it with backquotes.
xtofl
I think that this is a bad answer because it doesn't address the constness of array in the question. AFAIK, C/C++ is allowed to define any const array that never has its address taken in the const link section at an absolute address or even remove it altogether and use its parts as register absolutes.
Zan Lynx
@Zan: afaik, that's up to the implementation; the locally-scoped variable should still go out of scope, and all of it's elements should be destructed.
xtofl
A: 

Yes, non-static variables are always created on the stack.

StackedCrooked
Are you not implying that it being static would take up stack space?
Dynite
@Dynite, a colleage of mine mentioned that once before. However, I am not certain, so I'll just remove that part of my post.
StackedCrooked
@Dynite, I was talking BS. Correct information can be found here: http://stackoverflow.com/questions/93039/where-are-static-variables-stored-in-c-c
StackedCrooked