2

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 :(

Community
  • 1
  • 1
Prasanth
  • 5,230
  • 2
  • 29
  • 61

5 Answers5

7

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.

aod
  • 93
  • 6
7

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.

Jan
  • 11,636
  • 38
  • 47
  • Thanks! I was expecting this kind of answer. Now, correlating this to what others said, I think gcc indeed stores the name of variable in the source code, but replaces all next occurrences with a symbol. This concept sounds strikingly like symbol table in assembly. Am I right? – Prasanth Nov 27 '12 at 11:50
  • 1
    +1 for mentioning that the symbol table contains the variable name. The name is only used by linker and debugger though, and can be removed with `strip`, in many cases. – ams Nov 27 '12 at 11:51
  • @Prasanth: The compiled program itself does not need the symbol name. The only time it is needed at run-time is if you export the variable from a shared library. – ams Nov 27 '12 at 11:53
  • Also, C is compiled *to* assembler. It's no coincidence that the symbol tables look the same. – ams Nov 27 '12 at 11:56
3

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.

John Bode
  • 119,563
  • 19
  • 122
  • 198
2

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

1

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

Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70