Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many const variables can I declare before running out of memory?

I am writing code that has a lot of const variables (mostly ints and enums) and I am wondering, is there a maximum number of variables I can declare? My idea is that these const variables are allocated on the stack, which means I can declare about 1MB/4bytes = 250000 variables (which is plenty), assuming the stack has a size of 1MB. Am I correct?

A quick example of what i mean:

Test.cpp:

const unsigned int VECTOR_ID = 4;
const unsigned int MATRIX_ID = 3;

int main()
{
  std::cout << VECTOR_ID << " : " << MATRIX_ID << std::endl;
  return 0;
}
like image 908
ThatGuyAgain Avatar asked Feb 08 '20 18:02

ThatGuyAgain


People also ask

Does using const save memory?

If the compiler optimizes it as a compile-time constant then yes it could save a few bytes of memory since it might not need space for the variable. That requires you to initialize the variable on definition, and that you never ever anywhere takes the address of the variable.

Do constants need memory?

A constant is a named memory location which temporarily stores data that remains the same throughout the execution of the program. The type of a variable indicates what kind of value it will store. The name of a variable is known as its identifier.

Can you declare a constant without initializing it?

An initializer for a constant is required. You must specify its value in the same declaration. (This makes sense, given that it can't be changed later.) The const declaration creates a read-only reference to a value.

Where are const variables stored in memory?

As per the memory layout of C program ,constant variables are stored in the Initialized data segment of the RAM. But as per some of the Microcontroller memory layout ,const variables are stored in FLASH Memory.


1 Answers

Note that constants which are known at compile time may not correspond to any objects; when you compile with optimization enabled the constants will be compiled directly into the machine instructions as immediate values. Here is a simple example. This means that there is no limit to the number of constant variables as such. It also means that constants which are not used at all will likely disappear completely. (And if they are used in any non-trivial way the size of the code will exceed the size of the data.)

Even if indeed your constant variables become objects, e.g. because their address is taken, they will be "compiled into your program" and be a part of the executable.

A program's size, and the size of its segments, is limited by the format of the executable, the system resources, and potentially by the build tools. An Intel page seems to indicate that static data (where global constants could end up) is limited to 2 GB under Windows even on 64 bit architectures (which is still three orders of magnitudes larger than your use case):

Note that the limit on static and stack data is the same in both 32-bit and 64-bit variants. This is due to the format of the Windows Portable Executable (PE) file type, which is used to describe EXEs and DLLs as laid out by the linker. It has 32-bit fields for image section offsets and lengths and was not extended for 64-bit variants of Windows. As on 32-bit Windows, static data and stack share the same first 2GB of address space.

A quick search seemed to indicate that this limit is not present in a modern Linux.

like image 105
Peter - Reinstate Monica Avatar answered Oct 17 '22 01:10

Peter - Reinstate Monica