Question

#include <stdio.h>  
int main()
{
  int i = 10;
  return 0;
}

In the above program, where exactly the value 10 is stored ?

I understand the variable i is stored in the stack. stack is populated during run time. From "where exactly" 10 is coming from.

Était-ce utile?

La solution

10 is a constant, so the compiler will use the number 10 directly in the executable part of your program as part of the CPU instructions.

Here's the assembly produced on my system with gcc:

movl    $10, -4(%rbp)

(The 4 is because an int is 4 bytes long)

Note that all of these things are part of the implementation, but the above happens in practice. The language itself doesn't specify these details.

Autres conseils

10 is a "literal", which will be generated by the compiler during compilation. And then it will be assigned to your variable on the stack. Like

mov eax, 10;
mov [0x12345678], eax;

This is pseude-code, though, but will assign your variable i (address is here 0x12345678) the value 10, previously stored in eax.

The "stack" is an area of the memory set aside by the operating system for your program, separate from the "heap" and the global variables and the executable code.

When a function is called, there is code to push the arguments onto the stack, then space is set aside for the local variables. When the function returns this space and all arguments are "popped" off the stack, so the memory can be reused by the next function.

This is a very basic and rough description, and a lot of the details differs between systems and compilers.

There will be an explicit machine code instruction that sets i.

Something like:

 MOV AL, 10

After compiling and linking your executable contains multiple segments. Two type of these segments are:

  • text segments - containing the actual code
  • data segments - containing static data.

(there are other types as well)

The value 10 is either stored in the text segment (as an instruction to set 10 to a specific address or register), or stored as data in the data segment (which is retrieved by the code and stored at the specific address/register).

The compiler decides what is best (most efficient for the given compilation flags). But I suppose it is 'stored' in a text segment as the value 10 is quite simple to 'create in code' (as shown by some of the other answers).

More complex data (structs, strings, etc.) are typically stored in a data segment.

The value 10 is stored in a physical source file which contains your source code. During execution, that value is transferred to a variable named i, which has automatic storage duration, and is of the type int. That is all that matters. Any further questions aren't productive in the realm of general purpose programming languages such as C.

Notice how most answers mention compilers? What if an interpreter is used to translate C source code directly into behaviour? Are these answers still valid?

I rather not "give a man a fish" if you will... You can actually check it out yourself:

Take your code and create an object file out of it:

> gcc -c file.c -o file.o

Then do an object dump on the generated file:

> objdump -d file.o

Disassembly of section .text:

0000000000000000 <main>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   48 83 ec 10             sub    $0x10,%rsp
   8:   c7 45 fc 0f 00 00 00    movl   $0xa,-0x4(%rbp) // Right here you can see
                                                       // the raw value 0xa (10)
                                                       // being set, so it's in the
                                                       // .text section   
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top