Difference between a bus error and a segmentation fault? Can it happen that a program gives a seg fault and stops for the first time and for the second time it may give a bus error and exit ?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
I assume you're talking about the
SIGSEGV
andSIGBUS
signals defined by Posix.SIGSEGV
occurs when the program references an invalid address.SIGBUS
is an implementation-defined hardware fault. The default action for these two signals is to terminate the program.The program can catch these signals, and even ignore them.
Interpreting your question (possibly incorrectly) as meaning "I am intermittently getting a SIGSEGV or a SIGBUS, why isn't it consistent?", it's worth noting that doing dodgy things with pointers is not guaranteed by the C or C++ standards to result in a segfault; it's just "undefined behaviour", which as a professor I had once put it means that it may instead cause crocodiles to emerge from the floorboards and eat you.
So your situation could be that you have two bugs, where the first to occur sometimes causes SIGSEGV, and the second (if the segfault didn't happen and the program is still running) causes a SIGBUS.
I recommend you step through with a debugger, and look out for crocodiles.
This would be a dup of What is a bus error?, if it weren't for the
part of the question. You should be able to answer this for yourself with the information found here.
Of course, taking the question literally...
Tada, a program that can exit with a segmentation fault on one run and exit with a bus error on another run.
SIGBUS
will also be raised if yoummap()
a file and attempt to access part of the mapped buffer that extends past the end of the file, as well as for error conditions such as out of space. If you register a signal handler usingsigaction()
and you setSA_SIGINFO
, it may be possible to have your program examine the faulting memory address and handle only memory mapped file errors.On most architectures I've used, the distinction is that:
For instance, a bus error might be caused when your program tries to do something that the hardware bus doesn't support. On SPARCs, for instance, trying to read a multi-byte value (such as an int, 32-bits) from an odd address generated a bus error.
Segmentation faults happen for instance when you do an access that violate the segmentation rules, i.e. trying to read or write memory that you don't own.