7

e.g.

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

and

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

out of (1) and (2) which will acquire more memory space or equal space would be aquire?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Guri
  • 261
  • 2
  • 3
  • 10
  • 12
    such long names as in (2) will take more memory in the programmer's head :) – Chubsdad Sep 23 '10 at 11:44
  • The title says "in any programming language" but the tags say "c++" (before Martin added the "language-agnostic" tag). So which one is it? – sepp2k Sep 23 '10 at 11:45
  • All you are considering that integer I am talking about but I am saying any where in the memory a and a_long_variable_name_used_instead_of_small_one take some space or not – Guri Sep 23 '10 at 11:46
  • 6
    Even in languages where it does count, if you're even remotely suggesting using less descriptive names to "save memory", don't do it! Write clear code first! – AshleysBrain Sep 23 '10 at 11:49

10 Answers10

19

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.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
  • for C++, this depends if you compile with symbols or not. For debugging core dumps, I appreciate my symbols to be shipped in the binary / library, in this case however long names may affect the size (even though ONE long name wouldn't really...) – Matthieu M. Sep 23 '10 at 13:47
  • 3
    yes, it makes the file size bigger, bit it's not going to take any more memory unless you run it under a debugger – Ferruccio Sep 23 '10 at 14:03
9

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.

Nick Meyer
  • 39,212
  • 14
  • 67
  • 75
  • 7
    Since he has tagged the question as language-agnostic (among others), how about languages that support reflection at runtime? – Björn Pollex Sep 23 '10 at 11:39
  • 1
    Or languages that use a simple map from variable name to value to lookup variables (I'm sure there are interpreted languages that do that) – sepp2k Sep 23 '10 at 11:42
  • @sepp2k: For example, global variables in Lua are looked up using a hash map. – Puppy Sep 23 '10 at 11:48
  • 2
    Notice that, instead, class/struct names may matter if you enable RTTI, since their names in this case must be stored in the executable. EDIT: just to make it clear, I'm talking about C++. – Matteo Italia Sep 23 '10 at 11:48
  • 1
    @Space, @sepp2k: originally it was just tagged as C++. – Nick Meyer Sep 23 '10 at 13:59
6

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).

Martin Törnwall
  • 9,299
  • 2
  • 28
  • 35
6

In most interpreted languages, the name would be stored in a table somewhere in memory, thus taking up different amounts of space.

troelskn
  • 115,121
  • 27
  • 131
  • 155
2

If my understanding is correct, they'll take up the same amount of memory. I believe (and am ready to get shot down in flames) that in C++ the names are symbolic to help the user and the compiler will just create a block of memory sufficient to hold the type you're declaring, in this case an int. So, they should both occupy the same memory size, ie, the memory required to hold an address.

Rob Lowther
  • 361
  • 1
  • 6
2

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 in the assembler output. So, the length of the variable name does not matter at runtime in terms of memory.


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

A relevant quote from Hacker News,

Code should be written so as to completely describe the program's functionality to human readers, and only incidentally to be interpreted by computers. We have a hard time remembering short names for a long time, and we have a hard time looking at long names over and over again in a row. Additionally, short names carry a higher likelihood of collisions (since the search space is smaller), but are easier to "hold onto" for short periods of reading.

Thus, our conventions for naming things should take into consideration the limitations of the human brain. The length of a variable's name should be proportional to the distance between its definition and its use, and inversely proportional to its frequency of use.

Community
  • 1
  • 1
Lazer
  • 90,700
  • 113
  • 281
  • 364
1

In modern compilers the name of a variable does not impact the amount of space that is required to hold it in C++.

wheaties
  • 35,646
  • 15
  • 94
  • 131
1

Field names (instance variable names) in Java use memory, but only once per field. This is for reflection to work. The same goes for other languages that are based on the JVM, and I guess for DotNet.

Thomas Mueller
  • 48,905
  • 14
  • 116
  • 132
0

No. Both will occupy equal space.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
liaK
  • 11,422
  • 11
  • 48
  • 73
0

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.

Adam
  • 327
  • 2
  • 4