简体   繁体   English

C ++结构和指针的成员

[英]Members of structs and pointers, C++

I have the following snippet of code: 我有以下代码片段:

struct Node
{
    Node* left;
    Node* right;
    string data;
};    

void test()
    {
        Node thing;
        thing.data = "h";
        thing.left = NULL;
        thing.right = NULL;

        cout<< "Thing data = " << thing.data << endl;

        Node* thing2;
        thing2->data = "f";
        thing2->left = NULL;
        thing2->right = NULL;

        cout<< "Thing2 data = " << thing2->data << endl;

    }

The problem I'm having is that thing2->data = "f" is producing a segmentation fault during runtime. 我遇到的问题是,thing2-> data =“ f”在运行时产生分段错误。 I've run the program through GDB and get this error, but I can't figure out what it means: 我已经通过GDB运行该程序并收到此错误,但是我无法弄清楚它的含义:

Reading symbols for shared libraries ++. 读取共享库的符号++。 done Thing data = h 完成数据= h

Program received signal EXC_BAD_ACCESS, Could not access memory. 程序收到信号EXC_BAD_ACCESS,无法访问内存。 Reason: 13 at address: 0x0000000000000000 0x00007fff874d59a3 in std::string::assign () 原因:13地址:0x0000000000000000 0x00007fff874d59a3 in std :: string :: assign()

Any help would be great. 任何帮助都会很棒。 Thanks! 谢谢!

thing2 is a non initialized pointer. Thing2是一个未初始化的指针。 It doesn't point to a valid Node object. 它没有指向有效的Node对象。

You should allocate it: 您应该分配它:

thing2 = new Node;

or make it point to a valid Node object: 或使其指向有效的Node对象:

thing2 = & thing;

thing2 is a pointer to a Node , but you haven't made it point to anything: thing2是指向Node的指针,但是您尚未将其指向任何东西:

    Node* thing2 = new Node;
    thing2->data = "f";
    thing2->left = NULL;
    thing2->right = NULL;

    cout<< "Thing2 data = " << thing2->data << endl;
    delete thing2;

The above code allocates a Node on the heap, assigning it to thing2 . 上面的代码在堆上分配一个Node,并将其分配给thing2 When it's done with the object, it deletes it. 处理完对象后,将其删除。

A more idiomatic approach is to use a smart pointer: 更惯用的方法是使用智能指针:

#include <memory>
...
    std::auto_ptr<Node> thing2(new Node);
    thing2->data = "f";
    thing2->left = NULL;
    thing2->right = NULL;

    cout<< "Thing2 data = " << thing2->data << endl;

Since auto_ptr 's destructor deletes whatever it is pointing at, you don't need to explicitly delete the object. 由于auto_ptr的析构函数会删除其指向的内容,因此您无需显式删除该对象。

thing2 is declared as a pointer, and you never allocate (via new, malloc, or even on the stack) the actual node to which that pointer will point. Thing2被声明为一个指针,并且您永远不会(通过new,malloc甚至在堆栈上)分配该指针将指向的实际节点。 Thus, node2 points to some unknown bit of memory apparently outside of your program's address space, and when you attempt to modify that memory via the thing2->data = "f" call the OS rightly protects itself (and you) by disallowing it. 因此,node2指向内存的某个未知位,显然在程序的地址空间之外,并且当您尝试通过thing2->data = "f"修改该内存时,操作系统会通过不允许它正确地保护自己(和您自己)。 That's what the seg. 那就是段。 fault is all about. 故障全在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM