“ ”中的 C++ 错误:free():指针无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30451391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:46:55  来源:igfitidea点击:

C++ Error in ' ' : free(): invalid pointer

c++templatesfreeabort

提问by Vaderico

I've read through similar problems, but I can't find anything that specifically addresses my problem (or I simply don't understand the other solutions)

我已经阅读了类似的问题,但我找不到任何专门解决我的问题的内容(或者我根本不了解其他解决方案)

I am trying to implement a template Stack class, and am having an issue when trying to push to the stack. here is my Stack.cpp:

我正在尝试实现一个模板 Stack 类,并且在尝试推送到堆栈时遇到了问题。这是我的 Stack.cpp:

#ifndef _STACK_H
#define _STACK_H

#include <string>
#include <stdio.h>
#include "Node.cpp"

template<typename T>
class Stack{
    private:
        Node<T>* mHead; 

    public:
        Stack();
        ~Stack();
        void push(T data);
};

template<typename T>
Stack<T>::Stack(){
    mHead = NULL;
}

template<typename T>
Stack<T>::~Stack(){
    delete mHead;
}

template<typename T>
void Stack<T>::push(T data){        // <-- having trouble with this method
    Node<T>* temp = new Node<T>;
    temp->data = data;
    //if head is already empty, just create 1 Node
    if(mHead==NULL){
        printf("if working\n");
        mHead = temp;
    }else{
        printf("else working\n");
        //rearrange Nodes
        temp->next = mHead;
        mHead = temp;
    }
    printf("success\n");
}

#endif

push() gets called from a function in the manager class:

push() 从 manager 类中的函数调用:

void Manager::testPush(){
    Stack<int> test;
    int number = 3;
    test.push(3);
}

When I run the code and call managers testPush() method, i get the following being printed:

当我运行代码并调用管理器 testPush() 方法时,我打印出以下内容:

if working
success
*** Error in `./assignment': free(): invalid pointer: 0x0000000000f11078 ***
[1]    14976 abort (core dumped)  ./assignment


I'm not sure what free() means, and what could possibly be causing this error/abort

我不确定 free() 是什么意思,以及可能导致此错误/中止的原因

回答by Vlad from Moscow

It seems that you forgot to set data member next to NULL in node temp.

您似乎忘记在节点临时中将数据成员设置为 NULL 旁边。

template<typename T>
void Stack<T>::push(T data){        // <-- having trouble with this method
    Node<T>* temp = new Node<T>;
    temp->data = data;
    temp->next = NULL; // <=== add this statement

    //if head is already empty, just create 1 Node
    if(mHead==NULL){
        printf("if working\n");
        mHead = temp;

If the class Node has a constructor with two parameters or if it is an aggregate you could write simpler

如果类 Node 有一个带有两个参数的构造函数,或者它是一个聚合,你可以写得更简单

template<typename T>
void Stack<T>::push( const T &data )
{
    mHead = new Node<T> { data, mHead };
}

Take into account that the destructor of class Node must delete all nodes in the stack.

考虑到Node类的析构函数必须删除栈中的所有节点。

This function

这个功能

void Manager::testPush(){
    Stack<int> test;
    int number = 3;
    test.push(3);
}

also looks questionably because test is a local variable of the function. The stack can be used only inside the function.

看起来也有问题,因为 test 是函数的局部变量。堆栈只能在函数内部使用。