Location of bus errors and segment errors

Source: Internet
Author: User

For many preliminary programs, it turns out that if a bus error or segment error occurs (segementation fault/core dump, people cannot find any good method at a time and do not know where to solve the problem. Like many people, I soon encountered such a problem:

When this error occurs, the error message does not provide a simple or detailed explanation of the source code that causes the error. The knowledge is simple to show the error: the above information of segement default does not provide clues about how to find errors in the code, and the difference between the two is not very clear, that is, today is still the case.
Most errors present the fact that errors are detected by the operating system, this exception is a warning triggered by a problem that occurs due to the handling rules of the operation system as much as possible; the exact cause of bus errors and segment errors is the version running between different operating systems.

When the hardware tells the system that there is an incorrect memory reference, the two error operating systems send signal to the wrong process for communication. The signal notifies the occurrence of an event or the generation of soft interruptions, which is widely used in linux systems, but rarely used in application programming. By default, the process dumps and terminates the information after receiving a segment error or Bus Error signal. Of course, you can process these interruptions or signals and write some handler functions for them, modifies the default reflection of a process.

The signal is generated due to hardware interruption, and programming of interruption is very difficult because it is generated asynchronously and the time of its occurrence is unpredictable.

 

Bus Error:

Bus errors are almost caused by data Non-Alignment reads or writes. They are called bus errors because of the presence of non-alignment memory read/write requests, the blocked groups are bus errors. Alignment means that the data item can only be stored in the memory space where the address is an integer multiple of the data item size. A lot (

Reduced Instruction Set Computer, short Instruction Set Computer) needs to align data, any data alignment will have a huge impact on the memory space and logic, and slow down the system performance and speed. Memory alignment means that memory access is limited to a cache or a separate segment/page, and memory can be greatly used to increase the access rate and program execution rate, it will not cause memory disorder and facilitate memory management. Of course, a good compiler will have a corresponding warning on memory alignment. Segment error: the segment error is caused by an exception in the memory management unit MMU (responsible for supporting hardware in the virtual inner village, this exception is usually caused by unreferencing an uninitiated or invalid pointer. If the pointer references an address that is not in your address space, the operating system will but interfere with this. A small segment error code is returned:

  1. Int * P
    = 0; P = 88;

A subtle note is that if the initialization pointer happens to have an unaligned value, it will generate a bus error rather than a segment error. For most architectures, the CPU will first see the address and then send it to MMU; another point is that leading to Invalid Pointer values is usually caused by different programming errors. Unlike the bus, segment errors are more like indirect symptoms. Usually there are several direct causes of segment errors: 1. Remove a pointer that contains invalid values. 2. unreference a null pointer (it is often used because a null pointer is returned from the system program without any check ).
3. Access is performed when no definite permission is obtained. A segment error occurs when you attempt to perform write operations on a read-only file. 4, exceeds the virtual memory size (4 GB); or the occurrence frequency of the stack space is sequential, which may eventually lead to a common programming error: P1: Bad pointer error: use the pointer to reference the memory or send a bad pointer to the library function. This bad pointer is caused by releasing the pointer and then accessing it. Warning: After free, the pointer must be directed to null, free (p); P = NULL; P2: data is written beyond the boundary of the array, the amount of memory allocated dynamically is prone to a problem. P3: two times to release the same memory, or to release memory that has never been malloc or invalid pointers, or to release the memory space in use, an extremely common error statement related to memory release:

  1. For (P
    = Head; P
    ; P = p->
    Next)
  2. {
  3. Free (P );
  4. }

In this Code, when executing the next loop iteration, the pointer released by the program is removed from the reference operation, leading to unexpected errors.

Modification method:
  1. Struct node * P,
    * Head,
    * Q;
  2. For (P
    = Head; P
    = P->
    Next)
  3. {
  4. Q = p-
    > Next;
  5. Free (P );
  6. }
In summary, it can be basically determined that the first consideration for a segment error or a bus error is Pointer usage and memory access. The general problems can be identified through these basic

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.