Global / local const / static where are they?

A theoretical question...
Finally having some working GUI, I created a very simple icon, which is used with winit for creating an icon. The 'image' is scaled up to 256 x 256 using 3 Rgba colors.

// declare const or static, local or global?
static LEMMIX_ICON: [u8; 256] =
[
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,
    0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,
    0,0,0,0,2,0,1,2,2,1,0,2,0,0,0,0,
    0,0,0,0,0,2,0,2,2,0,2,0,0,0,0,0,
    0,0,0,0,0,0,2,3,3,2,0,0,0,0,0,0,
    0,0,0,0,0,0,0,3,3,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,3,3,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,3,3,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,3,3,0,0,0,0,0,0,0,
    0,0,0,0,0,0,2,2,2,2,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
];

During the creation / decoding of the image I use this encoded array.
Should I declare this array as global const or static?
Should I declare this array as local const or static (inside my function, the only place where it is used)?
And does it matter?
And where does the data reside in the application in all 4 cases?

Notionally, the definition of a const is "copy-pasted" everywhere you use the const. So notionally at least, a new instance of each const "lives" every place it gets used. That said, it is possible for some consts to be promoted to a static. But even if this happens, it could happen multiple times in the same binary, resulting in multiple statics.

A static lives in some global memory region of your program. There's always only one of each static.

All of that is true whether the const/static is local to some non-global scope or not. The non-global scope impacts how accessible the items are to parts of your code, not where they "live".

7 Likes

Aha, yes I understand. So logically - because the static is only used once inside my decode function - it could live as static inside that function, I presume.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.