Are constant arrays declared in a function stored on the stack?

if it was declared inside a function, would it be declared on the stack? (this is what makes me think)

void someFunction() { const unsigned int actions[8] = { e1, e2, etc... }; } 
+3
source share
4 answers

Yes, they are on the stack. You can see this by looking at this piece of code: it will have to print a message about the destruction 5 times.

 struct A { ~A(){ printf( "A destructed\n" ); } }; int main() { { const A anarray [5] = {A()} ; } printf( "inner scope closed\n"); } 
+5
source

As I understand it: yes. I was told that you need to qualify constants with static in order to put them in a data segment, for example.

 void someFunction() { static const unsigned int actions[8] = { e1, e2, etc... }; } 
+4
source

If you do not want your array to be created on the stack, declare it as static. Being const, the compiler can optimize the entire array. But if it is created, it will be pushed onto the AFAIK stack.

+2
source

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

0
source

Source: https://habr.com/ru/post/897146/


All Articles