I've got a C++ unit test that produces useful output to stderr, and mostly noise (unless I'm debugging) to stdout, so I'd like to redirect the stdout to /dev/null.
Curiously enough, doing this seems to cause a segmentation fault.
Is there any reason why code might seg fault with "> /dev/null" and run fine otherwise?
The output is produced entirely by printf
s, if that has any bearing.
It is difficult for me to post the offending code because it is research being submitted for publication. I'm hoping there is an "obvious" possible cause based on this description.
post mortem
The segfault was being caused by code like this:
ArrayElt* array = AllocateArrayOfSize(array_size);
int index = GetIndex(..) % array_size;
ArrayElt elt = array[index];
For the umpteenth time, I forgot that x % y
remains negative when x
is negative in C/C++.
Ok, so why was it only happening when I redirected to /dev/null
? My guess is that the invalid memory address I was accessing was in an output buffer for stdout - and this buffer isn't allocated when it isn't needed.
Thanks for the good answers!