Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirection of ./a.out is not capturing segmentation fault

Tags:

c++

c

linux

bash

shell

I run the command

./a.out < in &> output.txt
I want the errors also to be placed in output.txt.
The exit status of the command was 139 and on terminal its output was:
Segmentation fault (core dumped)

and the file output.txt was empty.

like image 283
chandan kharbanda Avatar asked May 30 '14 12:05

chandan kharbanda


People also ask

How do I find out what is causing my segmentation fault?

Check shell limits Usually it is the limit on stack size that causes this kind of problem. To check memory limits, use the ulimit command in bash or ksh , or the limit command in csh or tcsh . Try setting the stacksize higher, and then re-run your program to see if the segfault goes away.

How can I fix segmentation fault?

It can be resolved by having a base condition to return from the recursive function. A pointer must point to valid memory before accessing it.

How can segmentation fault be prevented?

Use a #define or the sizeof operator at all places where the array length is used. Improper handling of NULL terminated strings. Forgetting to allocate space for the terminating NULL character. Forgetting to set the terminating NULL character.

What are three kinds of pointers that can cause a segmentation fault?

Dereferencing or assigning to an uninitialized pointer (wild pointer, which points to a random memory address) Dereferencing or assigning to a freed pointer (dangling pointer, which points to memory that has been freed/deallocated/deleted) A buffer overflow. A stack overflow.


2 Answers

The message Segmentation fault (core dumped) is not coming from your program.

It's produced by shell as result of a signal received by it. It's not a part of stderr or stdout of your program.

So shell's message can be captured as:

{ ./a.out; } 2> out_err 
like image 112
P.P Avatar answered Oct 12 '22 22:10

P.P


If you want both the error messages from a.out and the string

Segmentation fault (core dumped)

to be appended to output.txt, then you have to redirect the shell's stderr as well. E.g.,

exec 2>> output.txt && ./a.out < in 2>&1 >> output.txt &

This is because the segfault message is coming from the shell itself.

like image 29
Roberto Reale Avatar answered Oct 12 '22 23:10

Roberto Reale