When a class dynamically allocates itself at constructor, why does stack overflow happen instead of...
Am I allowed to determine tenets of my contract as a warlock?
Fastest way from 8 to 7
What's the difference between DHCP and NAT? Are they mutually exclusive?
Do Veracrypt encrypted volumes have any kind of brute force protection?
How to soundproof the Wood Shop?
Was the Lonely Mountain, where Smaug lived, a volcano?
How can religions without a hell discourage evil-doing?
Why is it bad to use your whole foot in rock climbing
Boss making me feel guilty for leaving the company at the end of my internship
Is the first of the 10 Commandments considered a mitzvah?
Approach sick days in feedback meeting
Must CPU have a GPU if motherboard provides display port (when no separate video card)?
How can calculate the turn-off time of an LDO?
What did the 8086 (and 8088) do upon encountering an illegal instruction?
Can I use 220 V outlets on a 15 ampere breaker and wire it up as 110 V?
Are athlete's college degrees discounted by employers and graduate school admissions?
Must I use my personal social media account for work?
Why is my Taiyaki (Cake that looks like a fish) too hard and dry?
Is it good practice to create tables dynamically?
Why does there seem to be an extreme lack of public trashcans in Taiwan?
What does BREAD stand for while drafting?
What's the relation between у.е. to USD?
A life of PhD: is it feasible?
Does the UK delegate some immigration control to the Republic of Ireland?
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
10 hours ago
Because you don't do a bad allocation you just run out of stack memory because of the recursive constructor calls.
– Timo
10 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 10 hours ago
ownfosownfos
504
504
1
It's important to realize thatnew Overflow();causesOverflow::Overflow()to be called.
– François Andrieux
10 hours ago
Because you don't do a bad allocation you just run out of stack memory because of the recursive constructor calls.
– Timo
10 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
10 hours ago
Because you don't do a bad allocation you just run out of stack memory because of the recursive constructor calls.
– Timo
10 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
10 hours ago
It's important to realize that
new Overflow(); causes Overflow::Overflow() to be called.– François Andrieux
10 hours ago
Because you don't do a bad allocation you just run out of stack memory because of the recursive constructor calls.
– Timo
10 hours ago
Because you don't do a bad allocation you just run out of stack memory because of the recursive constructor calls.
– Timo
10 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 10 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 10 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
10 hours ago
Because you don't do a bad allocation you just run out of stack memory because of the recursive constructor calls.
– Timo
10 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