Whether variable name in any programming language takes memory space

后端 未结 10 1509
时光说笑
时光说笑 2021-01-05 17:44

e.g.

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

and

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


        
相关标签:
10条回答
  • 2021-01-05 18:09

    Both occupy the same amount of memory. 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.

    0 讨论(0)
  • 2021-01-05 18:09

    The name you give to a variable in C/C++ will not affect the size of the resulting executable code. When you declare a variable like that, the compiler reserves memory space (in the case of an int on x86/x64, four bytes) to store the value. To access or alter the value it will then use the address rather than the variable name (which is lost in the compilation process).

    0 讨论(0)
  • 2021-01-05 18:13

    compilers are there for a reason... They optimize code to use a little space as possible and run as fast as possible especially modern ones.

    0 讨论(0)
  • 2021-01-05 18:18

    In C++ and most statically compiled languages, variable names may take up more space during the compilation process but by run time the names will have been discarded and thus take up no space at all.

    In interpreted languages and compiled languages which provide run time introspection/reflection the name may take up more space.

    Also, language implementation will affect how much space variable names take up. The implementer may have decided to use a fixed-length buffer for each variable name, in which case each name takes up the same space regardless of length. Or they may have dynamically allocated space based on the length.

    0 讨论(0)
提交回复
热议问题