应用程序不断收到分段错误 - 除非我添加 for 循环

发布于 2024-12-09 21:40:19 字数 2352 浏览 1 评论 0原文

大家好:这里我从两个堆栈创建了一个队列:您添加到一个堆栈并从另一个堆栈中删除 - 当您想删除第一个堆栈时,会将其所有数据转储到第二个堆栈中,并且它工作完美 - 但是 每当我尝试在没有底部 for 循环或 cin 的情况下执行此循环 程序收到分段错误,我的意思是最底层的 for 循环甚至不执行,而是将其取出来看看会发生什么。这可能是某种缓冲区溢出吗 Gcc 需要时间来管理内存吗?

=================================================== ===================

    struct Node
    {
        int DataMember;
        Node* Next;
    };

    class Que
    {
        public:
            Que();
            ~Que();
            void Add(int);
            void Pop();
            int getSize();
            void Purge();
        private:
            Node* Head;
            bool StackOrQue; //True = Que False = Stack
            int Size;
            int Remove();
            void Reverse();
    };

    void Que::Purge()
    {
        while(Head != NULL)
            Pop();

        if(StackOrQue)
            StackOrQue = false;
    }

    int Que::getSize()
    {
        return Size;
    }

    Que::Que()
    {
        Head = NULL;
        Size = 0;
        StackOrQue = false;
    }

    Que::~Que()
    {
        Head = NULL;
    }

    void Que::Add(int q)
    {
        if(StackOrQue)
            Reverse();
        Size += 1;
        Node* Temp = new Node;

        Temp->DataMember = q;

        Temp->Next = Head;
        Head = Temp;
    }

    int Que::Remove()
    {
        int i = Head->DataMember;
        Node* Temp = Head->Next;
        delete Head;
        Size -= 1;
        Head = Temp;
        return i;
    }

    void Que::Pop()
    {
        if(!StackOrQue)
            Reverse();
        cout << Remove();
    }

    void Que::Reverse()
    {
        Que TempStack;
        int k = Size;
        for(int i = 0; i < k; i++)
            TempStack.Add(this->Remove());
        delete this;
        *this = TempStack;

        if(!StackOrQue)
            StackOrQue = true;
        else
            StackOrQue = false;
    }

================================= ========================================

Que q;
char a = NULL;

while(a != 'x')
{
    q.Purge();
    q.Add(1);
    q.Add(2);
    q.Add(3);
    q.Add(4);
    q.Add(5);
    q.Add(6);
    q.Add(7);
    q.Add(8);
    int size = q.getSize();
    for(int i = 0; i < size; i++)
        q.Pop();
    //cin >> a;
    for(int i = 0; i < 0; i++)
        ;
}

提前致谢

Hi everyone: Here i have created a queue from two stacks: You add to the one and remove from the other - when you want to remove the first stack dumps all its data into the second one, and it works perfectly - BUT
whenever i try to execute this loop without the bottom for loop or cin
the program receives a segmentation fault, i mean the most bottom for loop doesn't even execute but take it out and see what happens. Could this be some sort of buffer overflow
and Gcc needs time to manage the memory?

=====================================================================

    struct Node
    {
        int DataMember;
        Node* Next;
    };

    class Que
    {
        public:
            Que();
            ~Que();
            void Add(int);
            void Pop();
            int getSize();
            void Purge();
        private:
            Node* Head;
            bool StackOrQue; //True = Que False = Stack
            int Size;
            int Remove();
            void Reverse();
    };

    void Que::Purge()
    {
        while(Head != NULL)
            Pop();

        if(StackOrQue)
            StackOrQue = false;
    }

    int Que::getSize()
    {
        return Size;
    }

    Que::Que()
    {
        Head = NULL;
        Size = 0;
        StackOrQue = false;
    }

    Que::~Que()
    {
        Head = NULL;
    }

    void Que::Add(int q)
    {
        if(StackOrQue)
            Reverse();
        Size += 1;
        Node* Temp = new Node;

        Temp->DataMember = q;

        Temp->Next = Head;
        Head = Temp;
    }

    int Que::Remove()
    {
        int i = Head->DataMember;
        Node* Temp = Head->Next;
        delete Head;
        Size -= 1;
        Head = Temp;
        return i;
    }

    void Que::Pop()
    {
        if(!StackOrQue)
            Reverse();
        cout << Remove();
    }

    void Que::Reverse()
    {
        Que TempStack;
        int k = Size;
        for(int i = 0; i < k; i++)
            TempStack.Add(this->Remove());
        delete this;
        *this = TempStack;

        if(!StackOrQue)
            StackOrQue = true;
        else
            StackOrQue = false;
    }

=====================================================================

Que q;
char a = NULL;

while(a != 'x')
{
    q.Purge();
    q.Add(1);
    q.Add(2);
    q.Add(3);
    q.Add(4);
    q.Add(5);
    q.Add(6);
    q.Add(7);
    q.Add(8);
    int size = q.getSize();
    for(int i = 0; i < size; i++)
        q.Pop();
    //cin >> a;
    for(int i = 0; i < 0; i++)
        ;
}

Thanks in-advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不…忘初心 2024-12-16 21:40:19
    delete this;
    *this = TempStack;

在一些极端的极端情况下,delete this; 实际上做了正确的事情。这不是其中之一。特别是因为您的队列被放置在堆栈中,并且您进一步尝试删除它。如果您打算调用析构函数,请执行 this->~Queue(),但是在手动销毁之后,下一步唯一明智的做法是placement new。分配给 *this 几乎总是一个坏主意(如果您将继承带入图片中,您只会导致创建一个切片对象,并且会导致更多问题)。此外,您的类应该实现复制构造函数和赋值运算符,以正确处理分配的资源。

    delete this;
    *this = TempStack;

There are some extreme corner cases in which delete this; actually does the right thing. This is not one of them. Specially since your Queue is placed in the stack, and you further try to delete it. If you intend to call the destructor instead do this->~Queue(), however after a manual destruction the only sensible thing to do next is a placement new. Assigning to *this is almost always a bad idea (if you bring inheritance into the picture, you have just caused a slice object to be created and more problems ahead the road). Also, your class should be implementing a copy constructor and an assignment operator, to correctly handle the resources allocated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文