Program received signal SIGSEGV, Segmentation fault.
0xff2209e8 in _write () from /usr/lib/libc.so.1
(gdb) up
#1 0xff212bf4 in _xflsbuf () from /usr/lib/libc.so.1
(gdb) up
#2 0xff21000c in _flsbuf () from /usr/lib/libc.so.1
(gdb) up
#3 0x00000037 in ?? ()
(gdb) up
#4 0x00000037 in ?? ()
(gdb) up
Previous frame identical to this frame (corrupt stack?)
(gdb) up
Initial frame selected; you cannot go up.
(gdb)

Any help?

Recommended Answers

All 8 Replies

It is actually crazy. I have a call to a recursive function process() which recursively calls itself for 22460 times, but after taht it executes exactly the first line inside process which is cout<<"hi"; after which it says seg. fault and shows the above error. If I type another cout<"hi"; again after that, it executes the first cout and gives a seg fault. I mean it exactly executes the first line in the function and gives a seg. fault immediatley. I wonder what's happening

what's happening is called stack overflow. Each recursive call required a little more stack space (or a lot more depending on how you wrote the function). Try to figure out how much stack space the function needs, add about 8 bytes for return address then multiply that by 22460. That could be a huge amount of memory.

How to figure out how much stack space the function needs

How to add 8 bytes for return address then multiply that by 22460

I am new to C++ programming and dont have much idea. Can you give me an example or something

google for getrusage, here is one link.

It's just simple maths

void process ( int a, int b ) {
  char var[10];
  process( 0, 0 );
}

You have a char array, so that's 10 bytes.
Two integers - say 4 bytes each (total 8).
Each call has a fixed overhead of several bytes (say saving the return address of the call), another 8.

So that's 26 bytes in this example.
26 * 22460 = 583960
which is way over 1/2MB.

Consider that many systems limit the stack space to a few MB in total anyway, then you don't need too many more local variables before you're in deep trouble.

The first thing you should do is look for a non-recursive solution (there is always a non-recursive solution).

I actaully tried calling non recursively and it works. I tried calling the function non-recursively more than 4 or 5 times and it still works. But for recursive function as I mentioned it loops for around 22460 times after which it seg. faults. By anyway can I find which line wrong memory allocation is taking place?

You can also find out the limits using getrlimit. So check if you're going over it or not.

It's just simple maths

void process ( int a, int b ) {
  char var[10];
  process( 0, 0 );
}

You have a char array, so that's 10 bytes.
Two integers - say 4 bytes each (total 8).
Each call has a fixed overhead of several bytes (say saving the return address of the call), another 8.

So that's 26 bytes in this example.
26 * 22460 = 583960...

we need to also take into acount
a. padding that would be added to align data (eg. in the above example, 2 bytes for the char array in a 32 bit architecture).
b. many compilers would also insert canaries on the stack to enable stack smashing protection. microsoft c++ 8, icc 9, gcc on FreeBSD, NetBsd, OpenBSD and SystemX all implement canaries by default. these canaries would be there in every stack frame (see http://en.wikipedia.org/wiki/Stack-smashing_protection#Implementations )

commented: nice +20
commented: hi +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.