Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a C++ compiler compile variable names? [closed]

I understand I did not make myself clear. My doubt, I think, could be summed up in this:

In an executable file(machine code) how are "variables" represented? Are they static memory addresses? Does the compiler gives each one a specific "name" (or just keeps the one you gave them)?

Expressed in code:

 int x=5;
 //Bunch of code
 cin>>y;
 cout<<x+1;

How does the program in each and every machine knows which address is going to hold the value 5, to hold the inputed value, to add 1 to the value it now holds and finally print that same value.

--João

like image 908
João Silva Avatar asked Feb 19 '12 18:02

João Silva


People also ask

How are variable names stored in C?

Variable names don't exist anymore after the compiler runs (barring special cases like exported globals in shared libraries or debug symbols). The entire act of compilation is intended to take those symbolic names and algorithms represented by your source code and turn them into native machine instructions.

How do compiler assign memory addresses to variables?

The compiler maps every variable name to a unique memory address and incorporates the address into the machine code. Together, the compiler and the operating system determine the location in memory of each variable.

Can we use in variable name in C?

Variable names in C are made up of letters (upper and lower case) and digits. The underscore character ("_") is also permitted. Names must not begin with a digit. Unlike some languages (such as Perl and some BASIC dialects), C does not use any special prefix characters on variable names.


2 Answers

It's implementation-specific.

Typically, the location of variables will be based on all sorts of factors and optimizations. They may not live in RAM at all, as they may be optimised to live entirely within registers, or optimised away entirely.

Variable names don't exist at run-time; they're discarded during compilation. However, the compiler may emit debug information that's stored in the application binary, to allow the developers to debug the application. This is usually removed in release versions, though.

I have no idea about the specifics of Gameshark. But in many cases, the location of a particular variable can be figured out by taking a look at the machine code for the application.

like image 158
Oliver Charlesworth Avatar answered Sep 18 '22 22:09

Oliver Charlesworth


Here is a simple program in C:

int main() {
    int a = 5;
    int b = 7;

    int c = a + b;

    return 0;
}

If you compile it with gcc -m32 -S -O0 -o main.s main.c under Linux, you'll get something like this

    .file   "main.c"
    .text
    .globl  main
    .type   main, @function
main:
.LFB0:
    /* %ebp is a Base Pointer Register */
    pushl   %ebp
    movl    %esp, %ebp

    /* Here we reserve space for our variables */
    subl    $16, %esp

    /* a's address is %ebp - 4 */
    movl    $5, -4(%ebp)

    /* b's address is %ebp - 8 */
    movl    $7, -8(%ebp)

    /* a + b */
    movl    -8(%ebp), %eax
    movl    -4(%ebp), %edx
    addl    %edx, %eax

    /* c's address is %ebp - 12 */
    movl    %eax, -12(%ebp)

    /* return 0 */
    movl    $0, %eax
    leave
    ret

As you can see, in this case, variables' addresses are calculated as offsets of a base pointer of a function. If you enable optimisations, variables' values may be stored in registers.

like image 30
kharvd Avatar answered Sep 22 '22 22:09

kharvd