3

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...
     };
 }
Suma
  • 33,181
  • 16
  • 123
  • 191
Dynite
  • 2,313
  • 5
  • 30
  • 38

4 Answers4

5

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
  • 40,723
  • 12
  • 105
  • 192
  • 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 Aug 26 '09 at 11:46
  • 2
    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 Aug 26 '09 at 11:51
  • 3
    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 Aug 26 '09 at 12:03
  • 1
    This is a bad example. Regardless of the storage location of 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
  • @arolson: Thanks, you're right. BTW, you can put code in comments (and in other text) by surrounding it with backquotes. – xtofl Sep 08 '09 at 15:07
  • 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 Sep 09 '09 at 01:26
  • @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 Sep 09 '09 at 06:37
4

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
  • 7,438
  • 1
  • 28
  • 41
2

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 for Ukraine
  • 12,283
  • 6
  • 56
  • 83
0

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

StackedCrooked
  • 34,653
  • 44
  • 154
  • 278
  • Are you not implying that it being static would take up stack space? – Dynite Aug 26 '09 at 12:12
  • @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 Aug 26 '09 at 14:16
  • @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 Aug 26 '09 at 20:35