Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8258763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:39:25+00:00 2026-06-08T02:39:25+00:00

struct MemBlock { char mem[1024]; MemBlock operator*(const MemBlock &b) const { return MemBlock(); }

  • 0
struct MemBlock {

    char mem[1024];

    MemBlock operator*(const MemBlock &b) const {

        return MemBlock();
    }

} global;

void foo(int step = 0) {

    if (step == 10000)
    {
        global = global * MemBlock();
    }
    else foo(step + 1);
}

int main() {

    foo();
    return 0;
}

Program received signal SIGSEGV, Segmentation fault.
0x08048510 in foo (step=4000) at t.cpp:12
12 void foo(int step = 0) {

It seems that the MemBlock() instance costs a lot of stack memory though it hasn’t been called yet (check gdb info).

And when I use global = global * global instead, the program exits normally.

Can anybody explain the inner mechanism?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:39:26+00:00Added an answer on June 8, 2026 at 2:39 am

    The compiler is reserving the stack space for the MemBlock instance on each call to foo, regardless of the control flow within foo. This is a common optimisation to prevent having to repeatedly adjust the stack pointer within the function. Instead, the compiler calculates the maximum stack space required and on entry to the function adjusts the stack pointer by that amount.

    As you’ve observed, this results in losing stack space reserved for objects you don’t actually use. The answer is to not do that; if you’re only using some large-footprint objects within certain branches then separate those branches out into their own function.

    Incidentally, this is why archaic versions of C required all function-scope variables to be declared at the top of the function; so that the compiler can easily work out how much stack space the function requires.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

struct X{}; template<class T> decltype(X() == int()) f(T const&){ return true; } int main(void)
struct G{ G&operator()(const int**a) { v<<a; std::copy(v.begin(),v.end(),std::ostream_iterator<int>(std::cout, )); return *this; } friend std::vector<int>&operator<<(std::vector<int>&v,const int**
struct Foo { int data; Foo() = default; Foo(const Foo& arg) = default; };
struct A { int &r; A (int &i) : r(i) {} void foo ()
struct T { void eat(std::string const& segment) { buffer << segment; std::string sentence; while
struct person { int age; char name[100]; struct person *next; }; void delfirst(struct person
struct dataStruct { const char* s; int num; }; struct Final_struct { int n;
struct X { X():mem(42){} void f(int param = mem) //ERROR { //do something }
struct Foo{ int _a; this(int a){ _a = a; } int opCall(int b){ return
struct that { that &frob() { return *this; } that frob() const { return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.