为什么当我尝试插入树时会出现分段错误*

发布于 2024-11-04 22:10:36 字数 558 浏览 0 评论 0原文

#include<iostream>
#include<set>
#include<stdlib.h>
using namespace std;
typedef set<short> pos;
typedef struct tree
{
        pos first;
}tree;
class check
{
public:
        pos last;
        void check_set();
};
void check::check_set()
{
        tree *root,p;
        root=(tree*)malloc(sizeof(tree));
        root->first.insert(2);//SEGMENTATION FAULT HERE WHY???
        p.first.insert(3);//NO SEGMENTATION FAULT
}
int main()
{
check obj;
obj.check_set();
obj.last.insert(1);//NO ERROR HERE
return 0;
}
#include<iostream>
#include<set>
#include<stdlib.h>
using namespace std;
typedef set<short> pos;
typedef struct tree
{
        pos first;
}tree;
class check
{
public:
        pos last;
        void check_set();
};
void check::check_set()
{
        tree *root,p;
        root=(tree*)malloc(sizeof(tree));
        root->first.insert(2);//SEGMENTATION FAULT HERE WHY???
        p.first.insert(3);//NO SEGMENTATION FAULT
}
int main()
{
check obj;
obj.check_set();
obj.last.insert(1);//NO ERROR HERE
return 0;
}

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

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

发布评论

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

评论(4

深爱不及久伴 2024-11-11 22:10:36

使用new 而不是malloc

malloc 仅分配内存,不会以任何方式初始化内存,也不会构造将位于该内存中的对象。另一方面的 new 构造了 C++ 对象。因此,要获取有效的 tree 对象(具有正确初始化的 first 成员),请使用以下命令:

root = new tree();

稍后,当您想要释放该对象时,请使用 delete:

delete root;

Use new instead of malloc.

malloc only allocates memory, it doesn't initialize it in any way and doesn't construct the objects that will be living in that memory. new on the other had constructs C++ objects. So to get a valid tree object (with a properly initialized first member), use this:

root = new tree();

Later, when you want to release that object, use delete:

delete root;
菊凝晚露 2024-11-11 22:10:36
树 *root,p;
根=(树*)malloc(sizeof(树));
root->first.insert(2);//这里出现分段错误,为什么???
p.first.insert(3);//无分段错误

p 分配在堆栈上!所以它的构造函数被调用。另一方面,root 的构造函数从不被调用!您只需分配树所需大小的内存即可!

tree *root,p;
root=(tree*)malloc(sizeof(tree));
root->first.insert(2);//SEGMENTATION FAULT HERE WHY???
p.first.insert(3);//NO SEGMENTATION FAULT

p is allocated on the stack! So it's constructor is called. root's constructor on the other hand is never called! You just allocate a memory of the size a tree would need!

惜醉颜 2024-11-11 22:10:36

问题在于根并不指向树,而是指向树大小的已分配内存块。然后,当集合(及其内部结构和精心修饰的指针)实际上并不在那里时,您尝试对内部成员执行 set 操作。

The problem is that root doesn't point to a tree, it points to a tree-sized chunk of allocated memory. Then you try to execute a set operation on an internal member, when the set (with its internal structure and well-groomed pointers) isn't actually in there.

无边思念无边月 2024-11-11 22:10:36

malloc 不会调用构造函数,因此 tree 的构造函数和 std::set 的构造函数都不会被调用,并且您尝试填充未构造 std::set。这就是为什么你会出现段错误。

使用 new 作为:

root = new tree(); //this allocates memory, and constructs the object as well.

//deallocation
delete root; //call the destructor, and then deallocate the memory

或使用placement-new作为:

root=(tree*)malloc(sizeof(tree)); //only allocates memory
root = new (root) tree; //constructs the object in the memory pointed to by root.

//deallocation
root->~tree(); //call the destructor
free(root);  //and then deallocate the memory

malloc doesn't call the constructor, so neither tree's constructor nor std::set's constructor ever get called and you're trying to populate the unconstructed std::set. That is why you get segfault.

Use new as:

root = new tree(); //this allocates memory, and constructs the object as well.

//deallocation
delete root; //call the destructor, and then deallocate the memory

Or use placement-new as:

root=(tree*)malloc(sizeof(tree)); //only allocates memory
root = new (root) tree; //constructs the object in the memory pointed to by root.

//deallocation
root->~tree(); //call the destructor
free(root);  //and then deallocate the memory
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文