Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find out a variables' name size in memory in C++?

I think the title is self-explanatory, but let me repeat so that no misunderstandings are made.

I would like to know whether there is a way of finding out how much space a variables' name takes up in the memory using C++.

Again, I am not talking about the size of a variables' value, but about its' names' size.

Thanks in advance.

like image 641
Mechanic45 Avatar asked Jan 29 '26 16:01

Mechanic45


2 Answers

Variable names are just to help you, the programmer, remember what the variable is for, and to help the compiler associate different uses of the same variable. With the exception of debugging symbols, they make no appearance in the compiled code.

like image 100
Lawrence Aiello Avatar answered Jan 31 '26 06:01

Lawrence Aiello


Every element in the memory consist of two parts - address and value@address. That's what the processor/controller/everything else see.

You can still argue and say:

int a = 5; # How am I not consuming space to write the name 'a' here?
int *p = a; # How am I not consuming space to write *p here. ?

It is all about the benefit of high-level language. Under the name, you are actually claiming stack/heap memory addresses and putting values to the memory under corresponding address. Because you are using high level language, you're using names to remember where is your data in a "human" way as opposed to the processor who remembers in a "machine" way. Under the bonnet, it's all addresses using 1s and 0s and values @ those addresses build with 1s and 0s.

like image 28
ha9u63ar Avatar answered Jan 31 '26 06:01

ha9u63ar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!