Segmentation fault when initializing a std::vector<double>

64 views Asked by At

What are the possible causes of a segmentation fault at the following line?

std::vector<double> d(8);
1

There are 1 answers

1
Employed Russian On BEST ANSWER

What are the possible causes of a segmentation fault at the following line?

The line itself is exceedingly unlikely to cause a segmentation fault. The only way that could happen is if you've exhausted stack. Do (gdb) x/i $pc. Is the crashing instruction a PUSH or a CALL? If so, stack exhaustion is probably the cause.

If the code is compiled with optimization, and the crashing instruction is MOVDQA or similar, then stack mis-alignment is likely to blame: some code in the stack is not aligning stack on 16-byte boundary, as required by the current ABI. Are you linking in some really old archive libraries? Are you building some code with -mpreferred-stack-boundary=2 or 1? Bad idea!

If (much more likely) the crash doesn't really happen at that line, but rather in some code called from that line, and especially if the called code is inside malloc, then previous heap corruption is most likely the cause.

To find it, use Valgrind or Address Sanitizer.