作为 int 传递和类内的指针之间的差异给了我分段错误

普尼特·戈斯瓦米

我正在尝试在 C++ 中实现一个简单的链表插入。现在,我的代码在第一次尝试时看起来像,代码本身是不言自明的,但我添加了一些明显的注释,因为 stackoverflow 不允许我在不发表评论的情况下发布问题:

// Simple class and struct node
struct node {
        int data;
        struct node *next;
};

class list {
     // Member functions
     private:
        // Defining struct object
        node *root;

    public:
        bool insertNode(node *root); // Insertion function
        void print_list();
};

bool list::insertNode(node *n){
    root->next = n;
    return true;
    /*  
     node *temp = new node; 
     temp->data = data;

    if(root == NULL){
        root->next = new node;`enter code here`
        root->next->data = data;
        root->next->next = NULL;
        cout << "Coming" << "\n";
        return true;
    } else {
        temp->next = root;
        root = temp;
        return true;
    } */

}


void list::print_list(){

    node *p = root;
    // . Iterating the list
    p = p->next;
    while(p != NULL){
        cout << p->data << "\n";
        p = p->next;
    }
}

int main(){

    class list l;
    node *n = new node;
    n->data = 5;
    n->next = NULL;
    if(!l.insertNode(n)){
        cout << "Failed" << "\n";
        return 1;
    }
    l.print_list();

    return 1;
}

问题是,当我尝试如下修改我的程序时,不同之处在于,如果我有一个 int 作为参数,而不是将节点作为插入函数中的参数,那么它会给我分段错误。
================================================== ========================

包括

#include <string>

using namespace std;

struct node {
        int data;
        struct node *next;
};

class list {

     private:
        node *root;

    public:
        bool insertNode(int data);
        void print_list();
};

// Insertion function
bool list::insertNode(int data){

    if(root == NULL){
        root = new node; 
        root->data = data;
        root->next = NULL;
        return true;
    } else {
        node *temp = new node; 
        temp->data = data;
        temp->next = root;
        root = temp;
        return true;
    } 

    return false;
}

// Print function
void list::print_list(){

    node *p = root;

    p = p->next;
    while(p != NULL){
        cout << p->data << "\n";
        p = p->next;
    }
}

// Main driver code
int main(){

    class list l;
    
    if(!l.insertNode(5)){
        cout << "Failed" << "\n";`enter code here`
        return 1;
    }
    l.print_list();

    return 1;
}
    

输出:./p

5
0
Segmentation fault: 11

这意味着在插入函数中我传递了一个整数而不是一个节点,那么为什么会出现分段错误?由于root在列表类中定义从未分配过内存,并且在我的插入函数中,如果我检测到 root 为空,那么我只是root = new node;做错了什么?另一方面,如果我在插入函数中插入一个节点而不是“int”,那么我会得到正确的输出吗?有什么区别?

迈克尔·罗伊

要使列表正常工作,需要对其进行正确初始化。你有一个裸指针,但你没有构造函数。你也没有析构函数。

class list 
{
    private:
        node *root;

    public:
        List() { root = NULL; }
        ~List() { clear(); }

        void clear()
        {
            node *p = root, *q;
            while (p)
            {
               q = p->next;
               delete p;
               p = q;
            }
            root = NULL;
        }

        bool insertNode(node* p)
        {
            if (!p)
               return false;

            p->next = root;
            root = p;
            return true;
        }

        bool insertNode(int data)
        {
            node* p = new node;
            if (!p)
               return false;
            p->data = data;
            return insertNode(p);
        }
        // ...
};

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从不同大小的整数转换为指针(将int作为const void *参数传递)

来自分类Dev

我将向量作为双精度函数传递给函数,但Visual Studio称其为unsigned int

来自分类Dev

将IEnumerable <int>作为参数传递给WCF服务

来自分类Dev

将`Num`作为`Int`传递给`take`

来自分类Dev

捕获输入作为输入(用于int和char变量)

来自分类Dev

int作为指向包含视频流的文件的指针

来自分类Dev

将List <int>作为查询参数传递给SQL Server

来自分类Dev

为什么我必须创建int数组而不是直接传递一个作为参数的原因?

来自分类Dev

我的分段选择器具有正常的Int值作为标记,这如何传递到CoreData和从CoreData传递出来?

来自分类Dev

在C / C ++中,将数组作为形式参数传递为int arr []和int arr [N]之间的区别

来自分类Dev

使用类和int参数作为模板的C ++重载operator +

来自分类Dev

Option [Int]作为循环索引

来自分类Dev

汇编中的int 10h,ah作为0d始终返回0作为鼠标指针下方像素的颜色

来自分类Dev

在C和C ++中,作为函数参数的int ** a和int a [] []之间的确切区别是什么?

来自分类Dev

学习c从strtok中获取位置指针作为int

来自分类Dev

C ++“错误:将'const std :: map <int,std :: basic_string <char>>'作为...的'this'参数传递”

来自分类Dev

Java readFully作为int []

来自分类Dev

传递静态const int作为参考时的链接器错误

来自分类Dev

捕获输入作为输入(用于int和char变量)

来自分类Dev

为什么在传递int(不是int *)作为scanf()的参数时,编译器没有发出错误?

来自分类Dev

如何定义可以接受List [List [String]]和List [List [Int]]作为输入的案例类?

来自分类Dev

将List <int>作为查询参数传递给SQL Server

来自分类Dev

错误:将“ const Integer”作为“ Integer Integer :: pow(int)”的“ this”参数传递

来自分类Dev

为什么系统不加警告地接受作为int参数传递的long int?

来自分类Dev

错误:将'const Vector <int>'作为...的'this'参数传递时,将丢弃限定符[-fpermissive]

来自分类Dev

无法将 #define 整数作为 uint8_t / int 类型的参数或作为数组索引传递

来自分类Dev

int *val = otherVal 和 int val = otherVal 之间的指针差异

来自分类Dev

方法定义中的错误“传递 const List<int> 作为此参数丢弃限定符”

来自分类Dev

X86-64 传递 float 和 int 作为参数

Related 相关文章

  1. 1

    从不同大小的整数转换为指针(将int作为const void *参数传递)

  2. 2

    我将向量作为双精度函数传递给函数,但Visual Studio称其为unsigned int

  3. 3

    将IEnumerable <int>作为参数传递给WCF服务

  4. 4

    将`Num`作为`Int`传递给`take`

  5. 5

    捕获输入作为输入(用于int和char变量)

  6. 6

    int作为指向包含视频流的文件的指针

  7. 7

    将List <int>作为查询参数传递给SQL Server

  8. 8

    为什么我必须创建int数组而不是直接传递一个作为参数的原因?

  9. 9

    我的分段选择器具有正常的Int值作为标记,这如何传递到CoreData和从CoreData传递出来?

  10. 10

    在C / C ++中,将数组作为形式参数传递为int arr []和int arr [N]之间的区别

  11. 11

    使用类和int参数作为模板的C ++重载operator +

  12. 12

    Option [Int]作为循环索引

  13. 13

    汇编中的int 10h,ah作为0d始终返回0作为鼠标指针下方像素的颜色

  14. 14

    在C和C ++中,作为函数参数的int ** a和int a [] []之间的确切区别是什么?

  15. 15

    学习c从strtok中获取位置指针作为int

  16. 16

    C ++“错误:将'const std :: map <int,std :: basic_string <char>>'作为...的'this'参数传递”

  17. 17

    Java readFully作为int []

  18. 18

    传递静态const int作为参考时的链接器错误

  19. 19

    捕获输入作为输入(用于int和char变量)

  20. 20

    为什么在传递int(不是int *)作为scanf()的参数时,编译器没有发出错误?

  21. 21

    如何定义可以接受List [List [String]]和List [List [Int]]作为输入的案例类?

  22. 22

    将List <int>作为查询参数传递给SQL Server

  23. 23

    错误:将“ const Integer”作为“ Integer Integer :: pow(int)”的“ this”参数传递

  24. 24

    为什么系统不加警告地接受作为int参数传递的long int?

  25. 25

    错误:将'const Vector <int>'作为...的'this'参数传递时,将丢弃限定符[-fpermissive]

  26. 26

    无法将 #define 整数作为 uint8_t / int 类型的参数或作为数组索引传递

  27. 27

    int *val = otherVal 和 int val = otherVal 之间的指针差异

  28. 28

    方法定义中的错误“传递 const List<int> 作为此参数丢弃限定符”

  29. 29

    X86-64 传递 float 和 int 作为参数

热门标签

归档