Muktadir Rahman
Muktadir Rahman

Reputation: 161

Where machine instructions of a program stored during runtime?

So far as I know, whenever we run any program, the machine instructions of the program is loaded in RAM. Again, there are two regions of memory: stack and heap.

My question is: Which region of memory the machine instruction stored in? stack or heap?

I learnt that the following program gives a runtime error though there is no variable declared inside the function. The reason behind this is the overflow of stack. Then should I assume that the machines instructions of the function is stored in stack?

int func()
    {
            return func();
    }

Upvotes: 8

Views: 10031

Answers (7)

DevSolar
DevSolar

Reputation: 70253

Neither, as it is not dynamically allocated the way stack and heap are.

The executable loader loads the executable (.text) and any static data it contains, like the initial values of global variables (.data / .rodata), into an unused RAM area. It then sets up any zero-initialized memory the executable asked for (.bss).

Only then is the stack set up for main(). Stack memory is allocated on the stack if you enter another function, holding the return address, function arguments, and any locally declared variables as well as any memory allocated via alloca().[1] Memory is released when you return from the function.

Heap memory is allocated by malloc(), calloc(), or realloc(). It gets released when you free() or realloc() it.

The RAM used for the executable and its static data does not get released until the process terminates.

Thus, stack and heap are, basically, under control of the application. The memory of the executable itself is under control of the executable loader / the operating system. In appropriately equipped operating systems, you don't even have write access to that memory.


Regarding your edited question, no. (Bad style, editing a question to give it a completely new angle.)

The executable code remains where it was loaded. Calling a function does not place machine instructions on the stack. The only thing your func() (a function taking no arguments) places on the stack is the return address, a pointer that indicates where execution should continue once the current function returns.

Since none of the calls ever returns, your program keeps adding return addresses on the stack, until that cannot hold any more. This has nothing to do with machine code instructions at all.


[1]: Note that none of this is actually part and parcel of the C language standard, but implementation-defined, and may differ -- I presented a simplified version of affairs. For example, function parameters might be passed in CPU registers instead of on the stack.

Upvotes: 15

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

Neither heap nor stack.

Usually, executable instructions are present in Code segment.

Quoting the wikipedia article

In computing, a code segment, also known as a text segment or simply as text, is a portion of an object file or the corresponding section of the program's virtual address space that contains executable instructions.

and

when the loader places a program into memory so that it may be executed, various memory regions are allocated (in particular, as pages)

At runtime, the code segment of an object file is loaded into a corresponding code segment in memory. In particular, it has nothing to do with stack or heap.


EDIT:

In your code snippet above, what you're experiencing is called infinite recursion.

Even if you function does not need any space in stack for local variable, it still needs to push the return address of the outer function before calling the inner function, thus claiming the stack space, only never to return [pop the addresses out of stack] [like at a point of no return], thereby running out of stack space, causing stack overflow.

Upvotes: 1

John Bode
John Bode

Reputation: 123448

There are multiple memory segments in addition to the stack and heap. Here's an example of how a program may be laid out in memory:

              +------------------------+
high address  | Command line arguments |   
              | and environment vars   |  
              +------------------------+
              |         stack          |
              | - - - - - - - - - - -  |
              |           |            |
              |           V            |
              |                        |
              |           ^            |
              |           |            |
              | - - - - - - - - - - -  |
              |          heap          |
              +------------------------+
              |    global and read-    |
              |       only data        |
              +------------------------+
              |     program text       |
 low address  |    (machine code)      |
              +------------------------+   

Details will vary from platform to platform, but this layout is pretty common for x86-based systems. The machine code occupies its own memory segment (labeled .text in ELF format), global read-only data will be stored in another segment (.rdata or .rodata), uninitialized globals in yet another segment (.bss), etc. Some segments are read-only, some are writable.

Upvotes: 1

Lundin
Lundin

Reputation: 213678

the machine instructions of the program is loaded in RAM

Correct for hosted, "PC-like" systems. On embedded systems, code is most often executed directly from flash memory

Again, there is two regions of memory: stack and heap.

No, that's some over-simplification that way too many, way too bad programming teachers teach.

There are lots of other regions too: .data and .bss where all variables with static storage go, .rodata where constants go, etc etc.

The segment where the program code is stored is usually called .text.

Upvotes: 3

ouah
ouah

Reputation: 145829

They are usually on a section called .text.

On Linux you can list the sections of an ELF object or executable with size command from core-utils, for example on a tst ELF executable:

$ size -Ax tst | grep "^\.text"
.text                0x1e8   0x4003b0
$

Upvotes: 1

Matteo Italia
Matteo Italia

Reputation: 126787

Again, there is two regions of memory: stack and heap.

It's not that simple. Normally on mainstream operating system there's more stuff going on:

  • there is one stack per running thread;
  • there are as many heaps as you decide to allocate (actually, as far as the memory manager is concerned you ask some memory pages to "enabled" in your virtual address space, the "heap" thing derives from the fact that normally one uses some kind of heap manager code to efficiently distribute these memory portions between allocations);
  • there can be memory mapped files and shared memory;
  • most importantly, the executable file (and the dynamic libraries) are mapped in the memory of the process, normally with the code zone (the so called "text" segment) mapped in read-only mode, and other zones (typically pertaining to initialized global and static variables and stuff fixed by the loader) in copy-on-write.

So, the code is stored in the relevant section of the executable, which is mapped in memory.

Upvotes: 1

Mark Shevchenko
Mark Shevchenko

Reputation: 8197

Neither one nor the other.

The image of your program contains the code and the static data (f.e. all you string constants, static arrays and structures, and so on). They will be loaded into different segments of the RAM.

Stack and heap are dynamic structures to store data, they will be created at the start of your program. Stack is hardware supported solutions, while heap is a standard library supported solution.

So, your code will be located in the code segment, your static data and heap will be located in the data segment, and the stack will be located in the stack segment.

Upvotes: 4

Related Questions