Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a stack overflow in C++ and Unix?

Tags:

c++

unix

I'm wondering how to get an stack overflow error with a simple example, such as:

int recursSum (int n)
{
   return (n==1)? 1:n+recursSum(n-1);
}

I ask that stupid question because I only have some Segmentation fault, even with an empty function calling itself…

Am I missing something or is there any protection or something that prevents me for doing this?

like image 298
cglacet Avatar asked Jan 29 '26 07:01

cglacet


1 Answers

A segmentation fault means that the memory protection kicked in and prevented you from accessing memory you did not have available. This can occur for a variety of reasons, but one reason indicated is stack overflow (overflowing the stack into some other segment of memory).

like image 113
Tevo D Avatar answered Jan 30 '26 21:01

Tevo D