Does the variable make a constant, or the final saving of bytes or memory?

I am working with a program and I was trying to save bytes and storage space.

I have a lot of variables in my C program, but I was wondering if I could reduce the size of the program by making some of the variables that do not change throughout the program const

or final

.

So my questions are:

  • Is it possible to keep bytes when defining static variables like constant

    ?
  • If bytes are retained by doing this, why are they retained? How does the program store the variable differently if it is constant

    , and why does this path require less storage space?
  • If bytes are not stored by defining variables as constant

    , then why should a developer define a variable that way in the first place? Can't we just leave const

    just in case we need to change the variable later (especially if it doesn't)?
  • Are there only some IDEs / languages ​​that store bytes with variables constant

    ?

Thanks for any help, we are very grateful.

+3


source to share


4 answers


All your questions are highly compiler and environment dependent. A C compiler designed for an embedded environment might do a great job of conserving memory, while others might not.

Is there any byte preservation when defining constant constant variables?

Yes it is possible. But note that "const" is generally not meant to indicate how to store a variable - instead, its meaning is to help the programmer and compiler understand the source code better (when the compiler "understands better" it can produce better object code). Some compilers may use this information to store this variable in permanent memory, or delete it and turn it into literals in the object code. But in the context of your question, it might be #define

more appropriate.

If bytes are retained by doing this, why are they retained? How does a program store a variable differently if it is constant, and why does it require less storage space?

Variables declared in source code can move to different places in the object code and to different places when the object file is loaded into memory and executed. Note that, again, there are differences in different architectures - for example, in a small 8/16 bit MCU (processor for electronic devices), there is usually no "download" of the object file. This means that the value of the variable is stored somewhere - anyway. But at a low level, the compiler can use literals instead of addresses and this basically saves some memory. Suppose you specified the constant variable GAIN = 5 in the source code. When this variable is used in some formula, the compiler emits something like "LD R12, GAIN" (loads register R12 with the contents of the GAIN address, where the variable GAIN is stored).But the compiler can also emit "LD R12, # 5" (loads the value "5" into R12). In both cases an instruction is required, but in the second case there is no memory for the variables involved. This is a savings and can also be faster.



If bytes are not stored by defining variables as constant, then why should a developer define a variable this way in the first place? Can't we just leave const in case we need to change the variable later (especially if it doesn't)?

As stated earlier, the "const" keyword is intended to better define what operations will be performed on a variable. This is useful for programmers for clarity. It is helpful to make it clear that a variable is not intended to be modified, especially if the variable is a formal parameter. In some environments, there is actually some read-only memory that can be read and not written, and if a variable (perhaps a "system variable") is marked as "const" it is clear to the programmer -and- compiler that can warn if it comes across code trying to change this variable.

Are there only some IDEs / languages ​​that store bytes with constant variables?

Definitely yes. But don't talk about IDEs: they are only editors. And as far as languages ​​go, it's complicated: it depends entirely on implementation and optimization. Probably, this kind of economy is used only in compilers (not in interpreters) and depends heavily on optimization parameters / compiler capabilities.

+3


source


I am assuming you are working on a deeply embedded system (like cortex-M processors). For this, you know that SRAM is a meager resource whereas you have a lot of FLASH memory. Then, as much as possible, use the const keyword for any variable that doesn't change. Doing this would mean that the compiler will store the variable in flash and not in SRAM.

For example, to keep the text on your system, you can do this:



const char* const txtMenuRoot[] = { "Hello, this is the root menu", "Another text" };

      

Then not only the text is saved in FLASH, but also its pointer.

+5


source


Think of const

this way (there is no such thing as final

or in C constant

, so I'll just ignore it). If it is possible for the compiler to conserve memory, it will (especially when compiled optimized for size). const

provides the compiler with additional information about object properties. The compiler can make smarter decisions when it has more information, and this does not prevent the compiler from making the same decision as before it had this information.

It cannot hurt and can help, and it also helps code programmers to simplify reasoning. Both the compiler and the programmer help, no one was hurt. It's a win-win.

+2


source


Compilers can reduce memory used based on knowledge of the code, const help the compiler know the actual behavior of the code (if you trigger warnings you may have suggestions for where to put const).

But the structure can contain unused bytes due to the alignment restrictions used by hw, and compilers cannot change the internal order of the structure. This can only be done with a code change.

struct wide               struct compact
{                         {
    int_least32_t i1;             int_least32_t i1,
    int_least8_t b;                             i2;
    int_least32_t i2;             int_least8_t b;
}                         }

      

Due to alignment constraints, the structure may have white space between the 'b' and 'i2' members. This is not the case in struct compact, because items are listed from the widest, which may require large alignments to the smallest.

In the same cases, struct compact results in even faster code.

0


source







All Articles