Go Down

Topic: static const memory usage (Read 216 times) previous topic - next topic

DannySwarzman

If the static keyword is used for a constant inside a function, does it have any influence on the compilation?

Suppose a function contains:

static const uint8_t UNO = 1;
static const uint8_t VARIOS[3]{1,2,3};

How much memory RAM do these take? Is the answer the same for AVR and SAMD21?


J-M-L

#1
Aug 25, 2019, 09:44 pm Last Edit: Aug 25, 2019, 09:47 pm by J-M-L
Static means the memory is not allocated on the heap stack and goes away when the function terminates. So the variable will exist for as long as your program runs. The scope is limited to the function though

This
Code: [Select]
static const uint8_t UNO = 1;will allocate one byte globally.

This line:
Code: [Select]
static const uint8_t VARIOS[3]{1,2,3}; allocates 3 bytes but does not do what you think...
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

TheMemberFormerlyKnownAsAWOL

Static means the memory is not allocated on the heap and goes away when the function terminates. So the variable will exist for as long as your program runs.

This line:
Code: [Select]
static const uint8_t VARIOS[3]{1,2,3};
 does not do what you think...
Static means the memory is not allocated on the stack

J-M-L

Indeed - exactly what I meant :)
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

TheMemberFormerlyKnownAsAWOL

Well, I suppose it isn't allocated on the heap either....:D

larryd

#5
Aug 25, 2019, 09:49 pm Last Edit: Aug 25, 2019, 09:52 pm by larryd
That's a heaping stack of information.





No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

david_2018

Curious, since both statements are static and constant, would the compiler potentially optimize them away, depending on their use?

larryd

static const uint8_t UNO = 1;

The above combination makes zero sense.  static const


No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

J-M-L

#8
Aug 25, 2019, 10:01 pm Last Edit: Aug 25, 2019, 10:03 pm by J-M-L
Yes I never wanted to mention the heap. I meant just to mention the stack where function's local variables are allocated.

(With static, the variable goes in the bss segment I would say)

static const uint8_t UNO = 1;
The above combination makes zero sense.  static const
Good point - i focused on static, did not even look at const ... will likely be optimized away indeed
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

DannySwarzman

Then this, inside a function:

static const uint8_t VARIOS[3]{1,2,3};

takes global space.

This, inside a function:

const uint8_t VARIOS[3]{1,2,3};

takes stack space.

And, these two, inside a function, take the same amount of stack space and no global space:

const uint8_t VARIOS[3]{1,2,3};
uint8_t VARIOS[3]{1,2,3};

Is that right?




J-M-L

Add an = before your initialization array
Code: [Select]
const uint8_t VARIOS[] = {1,2,3};
If the compilers is smart and depending if you use literals or constant indexes, the array might be optimized out too.
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

TheMemberFormerlyKnownAsAWOL

Add an = before your initialization array

No longer necessary

J-M-L

#12
Aug 25, 2019, 10:34 pm Last Edit: Aug 25, 2019, 10:38 pm by J-M-L
I learnt something then! Thx

EDIT: it does not seem to be in the spec. Is that a GCC thingy?
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

gfvalvo

static const uint8_t UNO = 1;

The above combination makes zero sense.  static const
I disagree.

When that line is outside of any function, the 'static' means that the variable UNO is global only to the current file. It's out of scope for functions in any other file, unlike a regular global. The 'const' means the programmer has promised the compiler that he/she will not try to change its value.

When used inside a function, UNO is local to the function, but not stored on the stack (as already mentioned). These means only one instance of it exists which may or may not be the behavior you want if the function is called recursively. The 'const' means the programmer has promised the compiler that he/she will not try to change its value.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

westfw

"static const" is supposed to be a good replacement for #define, at least at global scope.
Whether it will actually result in allocated memory depends on exactly how it is used.
On a samd board, if it does allocate memory, then it will end up in flash memory, but on a avr it will be in RAM (if allocated.)

Go Up