Where does main function get allocated?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








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.










share|improve this question



















  • 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

















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.










share|improve this question



















  • 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













0












0








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.










share|improve this question
















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.







process c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 17 at 14:27









ctrl-alt-delor

12.5k52662




12.5k52662










asked Mar 16 at 13:59









gnisgnis

136




136







  • 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 - 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




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





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




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






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






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





@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





@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 Answer
1






active

oldest

votes


















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).






share|improve this answer























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f506680%2fwhere-does-main-function-get-allocated%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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).






    share|improve this answer



























      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).






      share|improve this answer

























        1












        1








        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).






        share|improve this answer













        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).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 17 at 14:40









        ctrl-alt-delorctrl-alt-delor

        12.5k52662




        12.5k52662



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f506680%2fwhere-does-main-function-get-allocated%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay