When a class dynamically allocates itself at constructor, why does stack overflow happen instead of std::bad_alloc?Why should C++ programmers minimize use of 'new'?C++ std::bad_alloc from stack allocation?How Stack overflow exception occurs when property sets value to ItselfC++ copy constructor for class with dynamically allocated arrayCopy constructor throws a std::bad_alloc, when it is calledWhy does the C++ compiler create an object heap instead of pushing the class object in stack?Dynamically allocated memory storage clarificationWhy does a stack overflow occur at varying stack usage each run instead of a fixed amount?std::bad_alloc thrown on prime number taskC++ zero initialization - Why is `b` in this program uninitialized, but `a` is initialized?

If the pressure inside and outside a balloon balance, then why does air leave when it pops?

How do I change my LaTex document to follow some Word requirements?

If absolute velocity does not exist, how can we say a rocket accelerates in empty space?

Which are the methodologies for interpreting Vedas?

Approach sick days in feedback meeting

In American Politics, why is the Justice Department under the President?

Realistic, logical way for men with medieval-era weaponry to compete with much larger and physically stronger foes

How to represent jealousy in a cute way?

Undocumented incompatibility between changes and siunitx?

Is it a good security practice to force employees hide their employer to avoid being targeted?

The best in flight meal option for those suffering from reflux

How to soundproof the Wood Shop?

Can I attach a DC blower to intake manifold of my 150CC Yamaha FZS FI engine?

Is it possible to have battery technology that can't be duplicated?

Why is it bad to use your whole foot in rock climbing

Was planting UN flag on Moon ever discussed?

Forgot passport for Alaska cruise (Anchorage to Vancouver)

Placement of positioning lights on A320 winglets

About the paper by Buekenhout, Delandtsheer, Doyen, Kleidman, Liebeck and Saxl

Is it advisable to add a location heads-up when a scene changes in a novel?

As easy as Three, Two, One... How fast can you go from Five to Four?

Can an open source licence be revoked if it violates employer's IP?

What do I need to do, tax-wise, for a sudden windfall?

Can I get a photo of an Ancient Arrow?



When a class dynamically allocates itself at constructor, why does stack overflow happen instead of std::bad_alloc?


Why should C++ programmers minimize use of 'new'?C++ std::bad_alloc from stack allocation?How Stack overflow exception occurs when property sets value to ItselfC++ copy constructor for class with dynamically allocated arrayCopy constructor throws a std::bad_alloc, when it is calledWhy does the C++ compiler create an object heap instead of pushing the class object in stack?Dynamically allocated memory storage clarificationWhy does a stack overflow occur at varying stack usage each run instead of a fixed amount?std::bad_alloc thrown on prime number taskC++ zero initialization - Why is `b` in this program uninitialized, but `a` is initialized?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








7















I made a class that recursively creates itself using new (just for fun!), expecting that this will throw std::bad_alloc due to infinite dynamic allocation (heap overflow). But stack overflow happened instead of std::bad_alloc. Why does this happen?



class Overflow

private:
Overflow* overflow;

public:
Overflow()

overflow = new Overflow();

;

int main()

Overflow overflow_happens; // stack overflow happens instead of std::bad_alloc exeption










share|improve this question



















  • 1





    It's important to realize that new Overflow(); causes Overflow::Overflow() to be called.

    – François Andrieux
    9 hours ago












  • Because you don't do a bad allocation you just run out of stack memory because of the recursive constructor calls.

    – Timo
    9 hours ago







  • 1





    That is implementation dependent race condition. Like if you take poison and also get shot there's a high chance you'll die but whether from poisoning or bleeding is a race condition.

    – Tanveer Badar
    9 hours ago






  • 2





    @TanveerBadar While there is a "race" between running out of stack space and heap space, the term race condition has a strict definition in c++. So it's not accurate to say there is a race condition.

    – François Andrieux
    9 hours ago






  • 1





    What happens if you overflow = new Overflow[1000]? or 1000000, or 1000000000?

    – Caleth
    8 hours ago


















7















I made a class that recursively creates itself using new (just for fun!), expecting that this will throw std::bad_alloc due to infinite dynamic allocation (heap overflow). But stack overflow happened instead of std::bad_alloc. Why does this happen?



class Overflow

private:
Overflow* overflow;

public:
Overflow()

overflow = new Overflow();

;

int main()

Overflow overflow_happens; // stack overflow happens instead of std::bad_alloc exeption










share|improve this question



















  • 1





    It's important to realize that new Overflow(); causes Overflow::Overflow() to be called.

    – François Andrieux
    9 hours ago












  • Because you don't do a bad allocation you just run out of stack memory because of the recursive constructor calls.

    – Timo
    9 hours ago







  • 1





    That is implementation dependent race condition. Like if you take poison and also get shot there's a high chance you'll die but whether from poisoning or bleeding is a race condition.

    – Tanveer Badar
    9 hours ago






  • 2





    @TanveerBadar While there is a "race" between running out of stack space and heap space, the term race condition has a strict definition in c++. So it's not accurate to say there is a race condition.

    – François Andrieux
    9 hours ago






  • 1





    What happens if you overflow = new Overflow[1000]? or 1000000, or 1000000000?

    – Caleth
    8 hours ago














7












7








7








I made a class that recursively creates itself using new (just for fun!), expecting that this will throw std::bad_alloc due to infinite dynamic allocation (heap overflow). But stack overflow happened instead of std::bad_alloc. Why does this happen?



class Overflow

private:
Overflow* overflow;

public:
Overflow()

overflow = new Overflow();

;

int main()

Overflow overflow_happens; // stack overflow happens instead of std::bad_alloc exeption










share|improve this question
















I made a class that recursively creates itself using new (just for fun!), expecting that this will throw std::bad_alloc due to infinite dynamic allocation (heap overflow). But stack overflow happened instead of std::bad_alloc. Why does this happen?



class Overflow

private:
Overflow* overflow;

public:
Overflow()

overflow = new Overflow();

;

int main()

Overflow overflow_happens; // stack overflow happens instead of std::bad_alloc exeption







c++ stack-overflow bad-alloc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 9 hours ago









NathanOliver

105k18150231




105k18150231










asked 9 hours ago









ownfosownfos

504




504







  • 1





    It's important to realize that new Overflow(); causes Overflow::Overflow() to be called.

    – François Andrieux
    9 hours ago












  • Because you don't do a bad allocation you just run out of stack memory because of the recursive constructor calls.

    – Timo
    9 hours ago







  • 1





    That is implementation dependent race condition. Like if you take poison and also get shot there's a high chance you'll die but whether from poisoning or bleeding is a race condition.

    – Tanveer Badar
    9 hours ago






  • 2





    @TanveerBadar While there is a "race" between running out of stack space and heap space, the term race condition has a strict definition in c++. So it's not accurate to say there is a race condition.

    – François Andrieux
    9 hours ago






  • 1





    What happens if you overflow = new Overflow[1000]? or 1000000, or 1000000000?

    – Caleth
    8 hours ago













  • 1





    It's important to realize that new Overflow(); causes Overflow::Overflow() to be called.

    – François Andrieux
    9 hours ago












  • Because you don't do a bad allocation you just run out of stack memory because of the recursive constructor calls.

    – Timo
    9 hours ago







  • 1





    That is implementation dependent race condition. Like if you take poison and also get shot there's a high chance you'll die but whether from poisoning or bleeding is a race condition.

    – Tanveer Badar
    9 hours ago






  • 2





    @TanveerBadar While there is a "race" between running out of stack space and heap space, the term race condition has a strict definition in c++. So it's not accurate to say there is a race condition.

    – François Andrieux
    9 hours ago






  • 1





    What happens if you overflow = new Overflow[1000]? or 1000000, or 1000000000?

    – Caleth
    8 hours ago








1




1





It's important to realize that new Overflow(); causes Overflow::Overflow() to be called.

– François Andrieux
9 hours ago






It's important to realize that new Overflow(); causes Overflow::Overflow() to be called.

– François Andrieux
9 hours ago














Because you don't do a bad allocation you just run out of stack memory because of the recursive constructor calls.

– Timo
9 hours ago






Because you don't do a bad allocation you just run out of stack memory because of the recursive constructor calls.

– Timo
9 hours ago





1




1





That is implementation dependent race condition. Like if you take poison and also get shot there's a high chance you'll die but whether from poisoning or bleeding is a race condition.

– Tanveer Badar
9 hours ago





That is implementation dependent race condition. Like if you take poison and also get shot there's a high chance you'll die but whether from poisoning or bleeding is a race condition.

– Tanveer Badar
9 hours ago




2




2





@TanveerBadar While there is a "race" between running out of stack space and heap space, the term race condition has a strict definition in c++. So it's not accurate to say there is a race condition.

– François Andrieux
9 hours ago





@TanveerBadar While there is a "race" between running out of stack space and heap space, the term race condition has a strict definition in c++. So it's not accurate to say there is a race condition.

– François Andrieux
9 hours ago




1




1





What happens if you overflow = new Overflow[1000]? or 1000000, or 1000000000?

– Caleth
8 hours ago






What happens if you overflow = new Overflow[1000]? or 1000000, or 1000000000?

– Caleth
8 hours ago













2 Answers
2






active

oldest

votes


















15














The stack overflow is happening because you have infinite recursion. Calling Overflow() causes you to call Overflow() again and again and again. Those function calls need to go on the stack. Since your stack is smaller than your heap you'll run out of stack space for all of the those constructor calls before you run out of memory for all of the objects you are creating.






share|improve this answer






























    4














    Because you are recursively calling the constructor, a method, repeatedly. The method calls get pushed to the call stack. Since the stack size is much smaller than the available heap, the call stack overflows before the heap runs out.






    share|improve this answer


















    • 1





      I like the answer, though I think this is not quite accurate: "Since the stack size is much smaller than the available heap...." because calling the constructor once does not require the same amount of heap as it requires stack,

      – formerlyknownas_463035818
      9 hours ago











    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fstackoverflow.com%2fquestions%2f56545469%2fwhen-a-class-dynamically-allocates-itself-at-constructor-why-does-stack-overflo%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    15














    The stack overflow is happening because you have infinite recursion. Calling Overflow() causes you to call Overflow() again and again and again. Those function calls need to go on the stack. Since your stack is smaller than your heap you'll run out of stack space for all of the those constructor calls before you run out of memory for all of the objects you are creating.






    share|improve this answer



























      15














      The stack overflow is happening because you have infinite recursion. Calling Overflow() causes you to call Overflow() again and again and again. Those function calls need to go on the stack. Since your stack is smaller than your heap you'll run out of stack space for all of the those constructor calls before you run out of memory for all of the objects you are creating.






      share|improve this answer

























        15












        15








        15







        The stack overflow is happening because you have infinite recursion. Calling Overflow() causes you to call Overflow() again and again and again. Those function calls need to go on the stack. Since your stack is smaller than your heap you'll run out of stack space for all of the those constructor calls before you run out of memory for all of the objects you are creating.






        share|improve this answer













        The stack overflow is happening because you have infinite recursion. Calling Overflow() causes you to call Overflow() again and again and again. Those function calls need to go on the stack. Since your stack is smaller than your heap you'll run out of stack space for all of the those constructor calls before you run out of memory for all of the objects you are creating.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 9 hours ago









        NathanOliverNathanOliver

        105k18150231




        105k18150231























            4














            Because you are recursively calling the constructor, a method, repeatedly. The method calls get pushed to the call stack. Since the stack size is much smaller than the available heap, the call stack overflows before the heap runs out.






            share|improve this answer


















            • 1





              I like the answer, though I think this is not quite accurate: "Since the stack size is much smaller than the available heap...." because calling the constructor once does not require the same amount of heap as it requires stack,

              – formerlyknownas_463035818
              9 hours ago















            4














            Because you are recursively calling the constructor, a method, repeatedly. The method calls get pushed to the call stack. Since the stack size is much smaller than the available heap, the call stack overflows before the heap runs out.






            share|improve this answer


















            • 1





              I like the answer, though I think this is not quite accurate: "Since the stack size is much smaller than the available heap...." because calling the constructor once does not require the same amount of heap as it requires stack,

              – formerlyknownas_463035818
              9 hours ago













            4












            4








            4







            Because you are recursively calling the constructor, a method, repeatedly. The method calls get pushed to the call stack. Since the stack size is much smaller than the available heap, the call stack overflows before the heap runs out.






            share|improve this answer













            Because you are recursively calling the constructor, a method, repeatedly. The method calls get pushed to the call stack. Since the stack size is much smaller than the available heap, the call stack overflows before the heap runs out.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 9 hours ago









            Avin KavishAvin Kavish

            2,101214




            2,101214







            • 1





              I like the answer, though I think this is not quite accurate: "Since the stack size is much smaller than the available heap...." because calling the constructor once does not require the same amount of heap as it requires stack,

              – formerlyknownas_463035818
              9 hours ago












            • 1





              I like the answer, though I think this is not quite accurate: "Since the stack size is much smaller than the available heap...." because calling the constructor once does not require the same amount of heap as it requires stack,

              – formerlyknownas_463035818
              9 hours ago







            1




            1





            I like the answer, though I think this is not quite accurate: "Since the stack size is much smaller than the available heap...." because calling the constructor once does not require the same amount of heap as it requires stack,

            – formerlyknownas_463035818
            9 hours ago





            I like the answer, though I think this is not quite accurate: "Since the stack size is much smaller than the available heap...." because calling the constructor once does not require the same amount of heap as it requires stack,

            – formerlyknownas_463035818
            9 hours ago

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • 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%2fstackoverflow.com%2fquestions%2f56545469%2fwhen-a-class-dynamically-allocates-itself-at-constructor-why-does-stack-overflo%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

            Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

            Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

            François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480