Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does a variable consume memory in addition to just its content (e.g. type, location)?

Tags:

c++

c

Quite likely this has been asked/answered before, but not sure how to phrase it best, a link to a previously answered question would be great.

If you define something like

char myChar = 'a';

I understand that this will take up one byte in memory (depending on implementation and assuming no unicode and so on, the actual number is unimportant).

But I would assume the compiler/computer would also need to keep a table of variable types, addresses (i.e. pointers), and possibly more. Otherwise it would have the memory reserved, but would not be able to do anything with it. So that's already at least a few more bytes of memory consumed per variable.

Is this a correct picture of what happens, or am I misunderstanding what happens when a program gets compiled/executed? And if the above is correct, is it more to do with compilation, or execution?

like image 472
funklute Avatar asked Dec 11 '22 08:12

funklute


2 Answers

The compiler will keep track of the properties of a variable - its name, lifetime, type, scope, etc. This information will exist in memory only during compilation. Once the program has been compiled and the program is executed, however, all that is left is the object itself. There is no type information at run-time (except if you use RTTI, then there will be some, but only because you required it for your program to function - such as is required for dynamic_casting).

Everything that happens in the code that accesses the object has been compiled into a form that treats it exactly as a single byte (because it's a char). The address that the object is located at can only be known at run-time anyway. However, variables with automatic storage duration (like local variables), are typically located simply by some fixed offset from the current stack frame. That offset is hard-baked into the executable.

like image 191
Joseph Mansfield Avatar answered Dec 28 '22 09:12

Joseph Mansfield


Wether a variable contains extra information depends on the type of the variable and your compiler options. If you use RTTI, extra information is stored. If you compile with debug information then there will also extra overhead be added. For native datatypes like your example of char there is usually no overhead, unless you have structs which also can cotnain padding bytes. If you define classes, there may be a virtual table associated with your class. However, if you dynamically allocate memory, then there usually will be some overhead along with your allocated memory.

Somtimes a variable may not even exist, because the optimizer realizes that there is no storage needed for it, and it can wrap it up in a register.

So in total, you can not rely on counting your used variables and sum their size up to calculate the amount of memory it requires because there is not neccessarily a 1:1: relation.

like image 40
Devolus Avatar answered Dec 28 '22 11:12

Devolus