This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

init of large arrays

Hi all,

what would be the best way to init a huge array, which will never change his values? Will the array affect the stack if I use const aray[][] or should I use static const array[][]?

What would be the best way if the array is not const during the whole program?

best regards
Hans

Parents
  • I am not sure if I understand the above statement.

    If the static keyword is not used, then the compiler is free to allocate space for the variable in whatever way it allocates space for non-persistent variables (e.g. on the stack, using memory overlaying, in registers, using static allocation, whatever). This means that if there's a stack, then the variable can end up there, or not, depending on the compiler.

    If the static keyword is used, the compiler must allocate space in the way it does for persistent variables. This excludes putting it on the stack.

    (And yes, I've just recently seen that the compiler I use did put a large const array on the stack, ironically by copying values stored elsewhere in memory to the stack. It only stopped doing that after I made the array static const.)

Reply
  • I am not sure if I understand the above statement.

    If the static keyword is not used, then the compiler is free to allocate space for the variable in whatever way it allocates space for non-persistent variables (e.g. on the stack, using memory overlaying, in registers, using static allocation, whatever). This means that if there's a stack, then the variable can end up there, or not, depending on the compiler.

    If the static keyword is used, the compiler must allocate space in the way it does for persistent variables. This excludes putting it on the stack.

    (And yes, I've just recently seen that the compiler I use did put a large const array on the stack, ironically by copying values stored elsewhere in memory to the stack. It only stopped doing that after I made the array static const.)

Children
No data