Question

Possible Duplicate:
Whether variable name in any programming language takes memory space

I was just reading about memory allocation, and can't help wonder this question:

Do both

int x = 4;

and

int this_is_really_really_long_name_for_an_integer_variable = 4;

occupy same amount of memory (the total memory occupied by the variable. not just sizeof(int))

I understand that this question is related to 'programming languages and compiler construction'. But, I haven't got to study it :(

Was it helpful?

Solution

In general they occupy the same amount of space, i.e. sizeof(int). However, one could argue that when building an object file with additional symbols for debugging the ratio is different. The amount of data which the variable stores does not change but the debugging symbols occupy more space in case of the longer variable name. Consider a following example.

$ cat short.c && gcc -c short.c && wc -c short.o
int x = 0;
927 short.o
$ cat long.c && gcc -c long.c && wc -c long.o
int this_is_really_really_long_name_for_an_integer_variable = 0;
981 long.o

The difference in size is exactly the difference of lengths of variables' names.

From a run-time efficiency and memory usage point of view it does not matter, though.

OTHER TIPS

In C? Yes, these variables will occupy the same amount of space. Variable name is used only by compiler at compile-time.

But there are some languages that store variable names in run-time.

The length of the variable name has no bearing on the amount of storage reserved for it; in most cases, the variable name isn't preserved in the generated machine code.

32 bits, since compiler will no store your name. It will handle it as an address only. int container only occupied 32 bits.

Variable names are only used for address binding at compile time.
variables names are stored in symbol table in lexical processing which is one phase of compiler process once address binding is done then there is no use of variable name, & your length of variable name does not matter. it only takes 32 bits

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top