Does the variable name in any programming language determine the memory space

eg.

int a=3;//-----------------------(1) 

and

 int a_long_variable_name_used_instead_of_small_one=3;//-------------(2) 

from (1) and (2), which will receive more memory space or equal space, will be available?

+6
c ++ language-agnostic programming-languages
source share
10 answers

Both occupy the same amount of memory. Variable names are intended only 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 cannot be seen in compiled code.

+8
source share

In C ++ and most statically compiled languages, variable names may take up more space during the compilation process, but the names will be discarded by the start time and therefore do not take up space at all.

In interpreted languages โ€‹โ€‹and compiled languages โ€‹โ€‹that provide introspection / reflection of runtime, the name may take up more space.

In addition, the language implementation affects the number of spatial variable names. Perhaps the developer decided to use a fixed-length buffer for each variable name, in which case each name occupies the same space regardless of length. Or they can have dynamically distributed space along the length.

+18
source share

In most interpreted languages, the name will be stored in a table somewhere in memory, thereby occupying a different space.

+6
source share

The name you pass to the variable in C / C ++ will not affect the size of the resulting executable code. When you declare such a variable, the compiler reserves memory space (in the case of int on x86 / x64, four bytes) to store the value. To access or change a value, it will use the address, not the name of the variable (which is lost during compilation).

+5
source share

If my understanding is correct, they will take the same amount of memory. I believe (and I'm ready to shoot in flames) that in C ++ the names are symbolic to help the user, and the compiler will simply create a memory block sufficient to hold the type that you declare, in this case int. Thus, they should occupy the same amount of memory, i.e. The memory required to store the address.

+2
source share

For C ++,

 $ cat name.cpp int main() { int a = 74678; int bcdefghijklmnopqrstuvwxyz = 5664; } $ g++ -S name.cpp $ cat name.s .file "name.cpp" .text .align 2 .globl main .type main, @function main: .LFB2: pushl %ebp .LCFI0: movl %esp, %ebp .LCFI1: subl $8, %esp .LCFI2: andl $-16, %esp movl $0, %eax addl $15, %eax addl $15, %eax shrl $4, %eax sall $4, %eax subl %eax, %esp movl $74678, -4(%ebp) movl $5664, -8(%ebp) movl $0, %eax leave ret .LFE2: .size main, .-main .section .note.GNU-stack,"",@progbits .ident "GCC: (GNU) 3.4.6 20060404 (Red Hat 3.4.6-11.0.1)" $ 

As you can see, neither a nor bcdefghijklmnopqrstuvwxyz reflect assembly output. Thus, the length of the variable name does not matter at runtime in terms of memory.


But variable names are huge contributions to program design. Some programmers even rely on good naming conventions instead of comments to explain the design of their program.

Corresponding quote from Hacker News ,

The code should be written in such a way as to fully describe the functionality of the program for readers for humans and only be randomly interpreted on computers. It is not easy for us to remember short names for a long time, and it is difficult for us to look at long names again and again in a row. In addition, short names have a higher chance of collisions (since the search space is smaller), but they are easier to keep for short reading periods.

Therefore, our conventions for naming things must take into account the limitations of the human brain. The length of the variable name must be proportional to the distance between its definition and its use and inversely proportional to the frequency of use.

+2
source share

In modern compilers, the variable name does not affect the amount of space required to store it in C ++.

+1
source share

Field names (instance variable names) in Java use memory, but only once for each field. This is to reflect the work. The same goes for other JVM-based languages, and I think for DotNet.

+1
source share

No .. Both will occupy an equal space.

0
source share

there are compilers ... They optimize the code to maximize the use of minimal space and work as quickly as possible, especially modern ones.

0
source share

All Articles