Bus Error and segment Error

Source: Internet
Author: User
Tags dbx

I bought this "C expert programming" in the previous section, which is really good, especially for memory thinking ~ Currently, I only see this chapter), which describes bus errors and segment errors:
Bus Error (core dumped) Bus Error (information has been dumped)
Segmetation fault (core dumped) segment error (information has been dumped)
It is believed that as long as you use C and C ++ in UNIX, these two errors are common and very troublesome. Currently, I have encountered a program with a segment error and have not found the cause of the error. Let's take a look at the author's explanation:

Cause: When the hardware tells the operating system a faulty memory reference, the operating system sends a signal to the problematic process for communication. (A signal is an event notification or software interruption ). Common processes generally perform information dumping and terminate the "Bus Error" or "segment error" signal, commonly known as "program coredump ".
(Of course, you can set a signal processing program to modify the default response of the program ).

Bus Error:
Cause: It is almost always caused by unaligned reads or writes. It is called a bus error because the blocked component is the address bus when it accesses Non-Alignment memory.

Alignment data items can only be stored in the memory location where the address is an integer multiple of the data item size, which can accelerate memory access. For example, when accessing an 8-byte double data, the address can only be an integer multiple of 8, so the address for storing a double data can only be 24, but it cannot be stored in address 1006 because it cannot be divisible by 8. As long as this principle is ensured, it can ensure that an atomic item data does not span pages or cache block boundaries.

Small programs that cause bus errors:
Union
{
Char A [10];
Int I;
} U;

Int * P = (int *) & (U. A [1]);

* P = 17;/* Non-Aligned addresses in P will cause bus errors */
Because the combination of the array and INT ensures that A is aligned according to the four bytes of the int, "A + 1" is certainly not aligned by the int. (Memory alignment will be discussed later)

Segment error:
Cause:
(1) release a pointer that contains an invalid value.
(2) unreference a null pointer (usually returned from the system, but not checked ).
(3) Access is not authorized. For example, storing a value in a read-only text segment causes a segment error.
(4) The stack or heap space is used up.

Frequency:
1. Bad pointer value error: Use the pointer to reference the memory before the value is assigned; or pass the bad pointer to the library function (the system program may still have a bad pointer problem in its own code ); after the pointer is released, it also accesses its content,
After the pointer is released, you must set it to null.

2 rewrite error: write data beyond the array boundary, write data at both ends of the dynamically allocated memory, or rewrite a bunch of management data structures (writing data at both ends of the dynamic memory will cause this error ).
P = malloc (256); P [-1] = 0; P [256] = 0;

3. Error Caused by pointer release: release a memory block twice, release a memory that has not been allocated with malloc, release the memory in use, or release an invalid pointer.
For example:
For (P = start; P = p-> next)
{
Free (P );
}
The program will unreference the released pointer P in the next loop.

Use DBX to check whether the stack space is used up:
Dbx A. Out
(DBX) catch SIGSEGV

(DBX) Run

...
Signal segv (segmentation violation) in <some_routine> at oxeff57708
(DBX) Where
If you can see that the call Chain shows that the stack space is not used up. If you see the following scenarios:
Fetch at oxeffe7a60 failed --- I/O Error
(DBX)
If the stack space is used up, the 16 forbidden number above is the stack address that can be extracted or shot.
In the C-SHELL, you can adjust the stack space to 10 K through limit stacksize 10, the process's bus address space still receives the swap size limit, you can use the swap-s command to view the swap size.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.