Stack Overflow: Duplicate temporary allocation in stack space?

Viewed 217
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 Answers
Related