Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you give an example of stack overflow in C++?

Can you give an example of stack overflow in C++? Other than the recursive case:

void foo() { foo(); }
like image 556
Nadir SOUALEM Avatar asked Nov 01 '09 15:11

Nadir SOUALEM


People also ask

What is stack overflow in C example?

It is used to store local variables which is used inside the function. Parameters are passed through this function and their return addresses. If a program consumes more memory space, then stack overflow will occur as stack size is limited in computer memory.

Does C have stack overflow?

On a C implementation with 8 byte double-precision floats, the declared array consumes 8 megabytes of data; if this is more memory than is available on the stack (as set by thread creation parameters or operating system limits), a stack overflow will occur.

How do I get a stack overflow?

In software, a stack overflow occurs if the call stack pointer exceeds the stack bound. The call stack may consist of a limited amount of address space, often determined at the start of the program. Translation: There is a limited amount of memory (the call stack) allocated to a program.

What causes a stack overflow error C++?

Stack Overflow is a fatal error which is most often found in programs containing recursive functions. It can also be caused by pushing too many local variables into the stack or manual allocation of stack memory.


1 Answers

The typical case that does not involve infinite recursion is declaring an automatic variable on the stack that is too large. For example:

int foo()
{
    int array[1000000];

}
like image 128
Tall Jeff Avatar answered Oct 14 '22 04:10

Tall Jeff