4
#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.

kumar
  • 2,530
  • 6
  • 33
  • 57
  • I suggest searching through [n1570.pdf](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) for this term "stack" which you are so fond of. Once you've done that, consider searching for "automatic storage duration", and see if that's what you mean... – autistic Mar 26 '13 at 13:30
  • 3
    @modifiablelvalue Ok, So, You meant to say - You don't know the answer. – kumar Mar 26 '13 at 13:35
  • No. I meant to say "the answer is very vague, because the question is very broad" and "your logic is very narrow; There is no stack in C". Before I get into the "*what about the callstack*?" debate, who here knows anything about CPS? – autistic Mar 26 '13 at 13:55

7 Answers7

5

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.

teppic
  • 8,039
  • 2
  • 24
  • 37
  • But 10 should be part of any one this ? Is there any other portion of program which is not part of the below segments?? :1. Text segment 2. Initialized data segment 3. Uninitialized data segment 4. Stack 5. Heap – kumar Mar 26 '13 at 13:27
  • @kumar - text. This means the executable instructions, not data. – teppic Mar 26 '13 at 13:29
  • Thanks teppic. I am getting the answers as If I am chatting.Its quite fast. – kumar Mar 26 '13 at 13:36
  • @kumar - if you're using gcc try compiling like this `gcc -S foo.c` to look at the .s file, and you'll see all the sections and how simple integers are put in directly in the CPU code. – teppic Mar 26 '13 at 13:39
  • @modifiablelvalue - the question is explicitly about segments and stacks, it's quite clearly regarding compiled code. – teppic Mar 26 '13 at 14:01
  • @teppic I would suggest that the "C" tag is more significant than the "segments" tag. – autistic Mar 26 '13 at 14:11
4

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.

bash.d
  • 13,029
  • 3
  • 29
  • 42
  • 10 a literal is generated by the compiler during compilation..OK, So is it portion of the code, Is it in "Text segment"?? My understanding is anyportion of code should be present in any of the five segments 1. Text segment 2. Initialized data segment 3. Uninitialized data segment 4. Stack 5. Heap. Please correct, If I am wrong. – kumar Mar 26 '13 at 13:30
  • Yes, it will be put into the `.text`-segment – bash.d Mar 26 '13 at 13:34
2

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

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

Something like:

 MOV AL, 10
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
1

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.

Veger
  • 37,240
  • 11
  • 105
  • 116
1

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?

autistic
  • 1
  • 3
  • 35
  • 80
1

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   
Mike
  • 47,263
  • 29
  • 113
  • 177