Error:program received signal SIGSEGV, segmentation fault. (Codeblocks, C + +) (2) __c++

Source: Internet
Author: User

Before, for this problem also summed up: "Error:program received signal SIGSEGV, segmentation fault. (Codeblocks, C + +) "

http://blog.csdn.net/libing_zeng/article/details/55684405

The previous conclusion was that the problem occurred because the null pointer was being manipulated.

This conclusion is indeed not rigorous. Here, to sum up again.


Why this error occurs.

The reason for this is that the " invalid pointer " has been manipulated.


According to the problems encountered, experience learned that:

There are two types of "invalid pointers" that cause segment errors:


First: The pointer is not initialized.


Like what:

Material_ptr->shade (SR)
This error may be reported if the material_ptr is a null pointer (or the corresponding pointer is not a pointer to an object that has a shade () member method).


The problem of this situation is better to check. Just look at the corresponding pointer. Because this invalid pointer is usually a few strange, abnormal pointers.


Problem Resolution: Initialize the pointer.


Second: The object pointed to by the pointer has been delete.


Like what:

If you have a pointer to a class in two classes. The world class below and the Arealight class simultaneously use pointers to the rectangle class.



It is easy to go wrong in the destructor. As below, if the world's destructor deletes the object pointed to by the rectangle pointer, the Arealight destructor then deletes the object pointed to by the rectangle pointer and then reports "segmentation fault."




The problem with this situation is somewhat difficult to find, because the corresponding pointer is a normal pointer, except that the object to which the pointer is pointing has been deleted.

Because it is a normal pointer, if (OBJECT_PTR) does not work at all.


Problem Resolution: Do not do the delete operation in the destructor of the latter class (above is Arealight).


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.