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;
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
|
show 7 more comments
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
1
It's important to realize thatnew Overflow();causesOverflow::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 youoverflow = new Overflow[1000]? or 1000000, or 1000000000?
– Caleth
8 hours ago
|
show 7 more comments
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
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
c++ stack-overflow bad-alloc
edited 9 hours ago
NathanOliver
105k18150231
105k18150231
asked 9 hours ago
ownfosownfos
504
504
1
It's important to realize thatnew Overflow();causesOverflow::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 youoverflow = new Overflow[1000]? or 1000000, or 1000000000?
– Caleth
8 hours ago
|
show 7 more comments
1
It's important to realize thatnew Overflow();causesOverflow::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 youoverflow = 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
|
show 7 more comments
2 Answers
2
active
oldest
votes
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.
add a comment |
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.
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered 9 hours ago
NathanOliverNathanOliver
105k18150231
105k18150231
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
It's important to realize that
new Overflow();causesOverflow::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