0

When concerned with a memory layout for a process, all function calls within the process get a new stack-frame into the stack.

I'm not sure if this is true for main function as it seems like main does more things than serving as an entry-point function.

Does main function also gets its own stack-frame upon initialization of the a new process?

If this is implementation specific details, explanation for a typical Linux implementation would be useful.

  • 1
    Usually _start is the entry point rather than main - seehttps://stackoverflow.com/questions/29694564/what-is-the-use-of-start-in-c – Torin Mar 16 at 14:16
  • 1
    Subroutines share a stack (They have to): If they had different stack, then which stack would we activated when a subroutine returns? And main is just another subroutine, it is call by _start. – ctrl-alt-delor Mar 16 at 14:35
  • This is a general C/C++ language programming question rather than a Unix & Linux one, as it applies to, and could be asked in this very form about, C/C++ language programming on a lot of platforms. – JdeBP Mar 16 at 17:37
  • @ctrl-alt-delor If main is just another subroutine, can I also say that it follows the same step with non-main functions when it comes to memory stack allocation? – gnis Mar 17 at 14:16
  • @JdeBP I thought it was kernel that bootstraps things including memory layout upon process initialization, so UNIX & Linux was more adequate forum to ask about. C language itself doesn't have any authority over where and how it(the program itself) gets allocated/managed. – gnis Mar 17 at 14:18
1

Usually _start is the entry point rather than main.

main is just another subroutine, it is call by _start. It gets a stack-frame, on the stack, just like any other subroutine.

However the kernel does not do as much as you may think. There is code in the process run before main (it _start it is generated by the C compiler, but not part of the C program), that does a lot. There is even code run before exec (before the C program runs), such as setting up stdin, stdout and stderr.

The kernel may do some tricks with _start, it can not be run like other subroutines, but it will look like it is: The kernel will set up the process as if _entry had just been called (but it was not). If control passes back to _start then exit is called.

(I may have glossed over some detail, including _entry).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.