Int值急剧变化和分段错误

时间:2015-03-21 23:09:06

标签: c++ class pointers linked-list segmentation-fault

我最近一直在努力学习C ++,而我正在制作一个没有交集的区间链表,但我得到了一些非常奇怪的错误。在我第二次运行reuinion函数后,我的整数(从get_bi或get_bs获得)给出了一些非常荒谬的值。由于这些值导致一些意外的行为,我得到分段错误。我认为那是问题的根源,但我不确定如何修复它们。我不认为我正在访问指向什么的指针或任何导致它的指针。

#include <cstddef>
#include <iostream>

using namespace std;

class Uinter
    /*
     * Regrouping class for intervals (_Inter class) 
     * offering 3 different methods:
     *  "reunion(int bi, int bs)" -> Unites the interval pointed by "_tete"
     *                               with the one defined by [bi, bs]
     *  "printUinter()"           -> Prints the interval pointed by "_tete"
     *  "contient(int nb)"        -> Returns True if "nb" is present inside
     *                               the intervals, else False. 
     */
    {
        class _Inter
        /*
         * Class representing a single interval.
         * _bi stands for the lower limit of the interval.
         * _bs stands for the upper limit of the interval.
         */
        {
            int _bi, _bs;
            _Inter* _next;
        public:
            _Inter(int _bi, int _bs, _Inter* _next)
                {
                this->_bi = _bi;
                this->_bs = _bs;
                this->_next = _next;
                cout<<"_Inter created with bi, bs and _next = " << _bi << " , "<< _bs << " , " << _next <<endl;  
                };

                // Defining getters and setters for the interval.

            void set_bi(int value)
                {cout <<"setting bi = "<< _bi << endl;
                _bi = value;}

            void set_bs(int value)
                {cout <<"setting bs = "<< _bi << endl;
                _bs = value;}

            void set_next(_Inter* value)
                {_next = value;}

            int get_bi()
                {cout <<"getting bi = "<< _bi << endl;
                return _bi;}

            int get_bs()
                {cout <<"getting bs = "<< _bs << endl;
                return _bs;}

            _Inter* get_next()
                {cout << "next = " << _next << endl;
                return _next;}

            void print_self()
                {
                cout << "[" << this->_bi << ";" << this->_bs << "]" << endl;
                }
        };
        _Inter* _tete;
    public:
        Uinter(): _tete(nullptr){}
        void reunion(int bi, int bs)
            {
            if(_tete == nullptr)
                {cout << "Case 1" << endl;
                _Inter new_interval = _Inter(bi, bs, nullptr);
                _tete = &new_interval;
                cout << "_tete after Case 1 end = " << _tete << endl;
                cout << "_tete values after Case 1 end = " << _tete->get_bi() << " " << _tete->get_bs() << " " << _tete->get_next() << endl;
                cout << "_tete->get_next() == nullptr = " << (_tete->get_next() == nullptr) << endl;
                }
            else
                {cout << "Case 2" << endl;
                if(bi < _tete->get_bi())
                    {cout << "Case 2.1" << endl;
                    if (bs < _tete->get_bi())
                        {cout << "Case 2.1.1" << endl;
                        _Inter new_interval = _Inter(bi, bs, _tete);
                        _tete = &new_interval;
                        }

                    else if (bs > _tete->get_bs())
                        {cout << "Case 2.1.2" << endl;
                        _tete->set_bi(bi);
                        _Inter* temp;
                        temp = _tete->get_next();
                        while (temp != nullptr &&bs > temp->get_bs())
                            {cout << "Case 2.1.2 while" << endl;
                            temp = temp->get_next();
                            }
                        if (temp == nullptr)
                            {cout << "Case 2.1.2.1" << endl;
                            _tete->set_bs(bs);
                            _tete->set_next(nullptr);
                            }
                        else
                            {cout << "Case 2.1.2.2" << endl;
                            _tete->set_bs(temp->get_bs());
                            _tete->set_next(temp->get_next());
                            }
                        }

                    else if (bs > _tete->get_bi())
                        {cout << "Case 2.1.3" << endl;
                        _tete->set_bi(bi);
                        }                   
                    }
                else if (bi > _tete->get_bi())
                    {cout << "Case 2.2" << endl;
                    if (bi > _tete->get_bs())
                        {cout << "Case 2.2.1" << endl;
                        _Inter* temp;
                        temp = _tete->get_next();

                        while(temp != nullptr && bi > temp->get_bs())
                            {cout << "Case 2.2.1 while" << endl;
                            temp = temp->get_next();
                            }
                        if (temp == nullptr)
                            {cout << "Case 2.2.1.1" << endl;
                            _Inter new_interval = _Inter(bi, bs, nullptr);
                            temp->set_next(&new_interval);
                            }
                        else if (bi < temp->get_bi())
                            {cout << "Case 2.2.1.2" << endl;
                            if (bs < temp->get_bi())
                                {cout << "Case 2.2.1.2.1" << endl;
                                _Inter new_interval = _Inter(temp->get_bi(), temp->get_bs(), temp->get_next());
                                temp->set_bi(bi);
                                temp->set_bs(bs);
                                temp->set_next(&new_interval);
                                }

                            else if (bs > temp->get_bs())
                                {cout << "Case 2.2.1.2.2" << endl;
                                temp->set_bi(bi);
                                temp->set_bs(bs);
                                }

                            else if (bs > temp->get_bi())
                                {cout << "Case 2.2.1.2.3" << endl;
                                temp->set_bi(bi);
                                }                   
                            }
                        else if (bi > temp->get_bi())  // bi < temp.get_bs()
                            {cout << "Case 2.2.1.3" << endl;
                            if (bs > temp->get_bs())
                                {cout << "Case 2.2.1.2.1" << endl;
                                temp->set_bs(bs);
                                }
                            }
                        }
                    else if (bi < _tete->get_bs())
                        {// bi > tete->get_bi() et bi < _tete->get_bs()
                        cout << "Case 2.2.2" << endl;
                        _Inter* temp;
                        temp = _tete;
                        while (temp != nullptr && bs > temp->get_bs())
                            {cout << "Case 2.2.2 while" << endl;
                            temp = temp->get_next();    
                            }
                        if (temp == nullptr)
                            {cout << "Case 2.2.2.1" << endl;
                            _tete->set_bs(bs);
                            _tete->set_next(nullptr);
                            }
                        else
                            {// bi > tete->get_bi() et bi < _tete->get_bs() et bs < temp->get_bs()
                            cout << "Case 2.2.2.2" << endl;
                            if(bs > temp->get_bi())
                                {cout << "Case 2.2.2.2.1" << endl;
                                _tete->set_bs(temp->get_bs());
                                _tete->set_next(temp->get_next());
                                }
                            else
                                {cout << "Case 2.2.2.2.2" << endl;
                                _tete->set_bs(bs);
                                _tete->set_next(temp);
                                }
                            }
                        }
                    }
                }
            }
        void printUinter()
            {
            }

        bool contient(int nb);
    };


int main() 
{
    Uinter interval;
    interval.reunion(12, 23);
    interval.printUinter();
    cout << "___________" << endl;
    interval.reunion(24,36);
    interval.printUinter();
    cout << "___________" << endl;
    interval.reunion(0,11);
    interval.printUinter();
    cout << "___________" << endl;

    return 0;
}

这是我运行编译器时得到的结果(我真的不知道它代表什么,因为它只是说“运行失败”)。

> Case 1
> _Inter created with bi, bs and _next = 12 , 23 , 0
> _tete after Case 1 end = 0x23ca90 next = 0 getting bs = 23 getting bi = 12
> _tete values after Case 1 end = 12 23 0 next = 0
> _tete->get_next() == nullptr = 1
> ___________ Case 2 getting bi = -17000128 getting bi = -17000128 Case 2.2 getting bs = 3 Case 2.2.1 next = 0x1fefcb6c0
> 
> RUN FAILED (exit value 1, total time: 274ms)

我已经读过运行调试器可能有所帮助,但是在NetBeans 8.0.2(gdb)中运行调试器只会给我一个分段错误而且没有关于它的真实信息,所以我不确定如何修复它。它突出了一句话:

    int get_bs()
        {cout <<"getting bs = "<< _bs << endl;
        return _bs;}

但是我不确定我是怎么解释那个,因​​为那条线上没有指针或其他什么。

Here is what I get on my debugger

1 个答案:

答案 0 :(得分:6)

您有一个名为_tete的小组成员:

        _Inter* _tete;

使用&#34; stack&#34;的地址初始化类成员。已分配对象(本地对象)。

                _Inter new_interval = _Inter(bi, bs, nullptr);
                _tete = &new_interval;

一旦函数返回并重新使用该对象,_tete将不会为NULL,但会指向无效的对象。

此外,_tete未初始化_Inter::_Inter()


C ++内存模型的完整描述超出了本答案的范围,但足以说明对象仅在存在时才有效。只要对象有效,指向对象的指针才有效。本地对象仅在定义它的本地范围内有效。

Foo *a_ptr = NULL;
if (condition) {
    Foo a; // a is local to the statement block
    a_ptr = &a;
    //...
}          // a has been destructed (it no longer exists)
           // a_ptr points to an invalid object

如果要创建一个对象并使其超出创建它的范围,则需要使用某种动态内存分配。在C ++中,这可以通过使用new来完成。通常,如果使用new创建对象,则需要使用delete销毁该对象。因此,这种内存管理需要手动跟踪代码何时完成对象,以便可以应用适当的清理。

Foo *a_ptr = NULL;
if (condition) {
    a_ptr = new Foo(/*...*/);
    //...
}
delete a_ptr; // Note that delete does not set the argument to NULL

脚本语言通常以自动方式处理此问题,具有某种垃圾收集功能和/或引用计数。 C ++没有指定任何垃圾收集工具,但标准C ++库中包含智能指针。智能指针在代码完成对象时自动跟踪,并确保在不再引用对象时进行适当的清理。在这种情况下,似乎可以将_tete设为unique_ptr

    std::unique_ptr<_Inter> _tete;
    //...
        _tete.reset(new _Inter(bi, bs, std::nullptr));