Reckoner
Reckoner

Reputation: 1041

How do 'C' statements execute inside memory

Suppose I have this piece of C code here:

int main() {           
    int a = 10;
    printf("test1");
    printf("test2");
    someFunc();
    a = 20;
    printf("%d",a);
} 

Well I think that all these statements are stored on the stack at a time and then popped one by one to get executed. Am I correct? If not, please correct me.

Upvotes: 1

Views: 129

Answers (3)

Lundin
Lundin

Reputation: 214395

The C standard doesn't specify where things go in memory.

However, computers work like this in general:

  • Your program consists of various memory sections/segments, usually they are called .data, .bss, .rodata, .stack, .text and there is possibly also a .heap. The .text section is for storing the actual program code, the rest of the sections are for storing variables. More info on Wikipedia.
  • The C code is translated to machine code (assembler) by the compiler. All code is stored inside the .text section, which is read-only memory.
  • Modern computers can "mark" memory as either code or data, and will be capable of generating hardware exceptions if you try to execute code in the data section, or treat the code section as data. This way the processor can assist in catching bugs like dangling pointers or run-away code.
  • In theory you could execute code from any memory section, even on the stack, but because of the previously mentioned feature, this is not typically done. Most often, it wouldn't make any sense to do so anyhow.

So for your specific code snippet, the only thing that is stored on the stack is the variable a. Or more likely, it is stored in a CPU register, for performance reasons.

The string literals "test1", "test2" and "%d" will be stored in the .rodata section.

The literal 20 could either be stored in the .rodata section, or more likely, merged into the code and therefore stored in .text together with the rest of the code.

The program counter determines which part of the code that is currently executed. The stack is not involved in that what-so-ever, it is only for storing data.

Upvotes: 2

Magisch
Magisch

Reputation: 7352

Whats worth noting is that the C standard does not mandate implementation. So as long as the output is correct according to the standard, the compiler is free to implement this as it choses.

What most likely happens for you is that the compiler translates this bit of C into assembler code which is then executed top to bottom.

a = 20;

Is most likely optimised out unless you use it somewhere else. Good compilers also throw a warning for you like:

Warning: Unused Variable a

Upvotes: 1

Bathsheba
Bathsheba

Reputation: 234785

Not really no. The C standard doesn't mention a stack so your notions are ill-conceived.

A C compiler (or interpreter for that matter) can do anything it likes so long as it follows the C standard.

In your case, that could mean, among other things, (i) the removal of a altogether, since it's only used to output 20 at the end of the function, and (ii) someFunc() could be removed if there are no side effects in doing so.

What normally happens is that your code is converted to machine code suitable for the target architecture. These machine code instructions do tend to follow the code quite faithfully (C in this sense is quite "low level") although modern compilers will optimise aggressively.

Upvotes: 6

Related Questions