获取未知分段错误"(地址)在 std::__cxx11::basic_string<char, std::char_traits<char>,..., std::分配器<cha
Getting unknown Segmentation Fault "(address) in std::__cxx11::basic_string<char, std::char_traits<char>,..., std::allocator<char> > const&) const ()
我正在用 c++ 制作一个程序,它代表一个具有这些类和结构的论坛:
struct Date{ /* struct used to get a random day for each comment */
int d,m,y,h,min;
Date();
void GetDate() const;
};
class Post{
private:
std::string Title;
std::string Creator;
std::string text;
Date PostDate;
int id;
public:
Post();
~Post();
void postprint() const;
int idprint();
std::string get_creator() const; //returns the creator of the post
};
class Thread{
private:
std::string Subject;
std::string Creator;
Date ThreadDate;
int post_nums = rand()%10+1; // this will be the number of posts that will be made in the thread, used in threadprint()
Post *posts = new Post[post_nums]; //so the randomizer doesn't return 0 array cells, max 10 posts for each thread
public:
Thread();
~Thread();
std::string subjprint() const;
void threadprint() const;
int getnums() const; //returns the number of posts in the posts array
Post *getposts() const; //returns to a pointer the array of posts
};
class Forum{
private:
std::string Title;
Thread *threads = new Thread[3];
//void printt(int) const;
public:
Forum(const std::string);
~Forum();
void Print_threads() const;
void Print_specified_thread(const std::string);
Thread *Find_thread_subject(const std::string);
void Print_specified_post(const int);
Post *Find_specified_post(const int);
};
struct Post_list{
Post *post;
Post_list *next;
Post_list *prev;
};
struct Tree_node{
std::string Creator;
Post_list *list_ptr;
Tree_node *left;
Tree_node *right;
};
class Thread_tree{
private:
Tree_node *node_root;
void Tree_create(Tree_node *);
int Tree_empty(Tree_node *);
void Tree_sort(const Thread *, Tree_node *);
void Tree_insert(const std::string, Tree_node *);
public:
Thread_tree(const Thread *);
};
现在我正在尝试将每个线程的评论的创建者放入 BST 中,对其进行排序,其中每个节点struct Tree_node
都有一个创建者字符串和他的评论列表。class Thread_tree
的构造函数是这样的
Thread_tree::Thread_tree(const Thread *Thread1){ //constructor only to be called once
cout << "Creating tree of post creators from thread " << Thread1->subjprint() << endl;
Tree_create(this->node_root); //points root to NULL
Tree_sort(Thread1, this->node_root);
}
而Tree_sort()
是
void Thread_tree::Tree_sort(const Thread *Thread1, Tree_node *root){
int k = Thread1->getnums(); //k is assigned to the number of the posts that the thread has
Post *post_ptr = Thread1->getposts(); //ptr is assigned to the posts array of the thread
for (int i=0; i<k; i++){
cout << "ni is " << i << endl;
Tree_insert((post_ptr+i)->get_creator(), root);
}
}
和Tree_insert()
void Thread_tree::Tree_insert(const string creator, Tree_node *root){
if (Tree_empty(root)){
root = new Tree_node;
root->Creator = creator;
root->list_ptr = NULL;
root->left = NULL;
root->right = NULL;
}
else if (creator.compare(root->Creator) < 0){ //creator in string1 is smaller than the root->Creator
Tree_insert(creator, (root->left)); // in ascending order
}
if (creator.compare(root->Creator) > 0) {//creator in string1 is bigger than the root->Creator
Tree_insert(creator, (root->right)); //in ascending order
}
}
在main((中,当我使用"thread"作为指向现有线程的指针进行树Thread_tree Tree1(thread);
时,我得到了一个分段错误。我运行 gdb 调试器,使用bt
后收到以下消息:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b7625a in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::compare(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const ()
from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
(gdb) bt
#0 0x00007ffff7b7625a in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::compare(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const ()
from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1 0x00000000004025f1 in Thread_tree::Tree_insert (this=0x7fffffffe7c0, creator="user1234", root=0x1002) at classes.cpp:211
#2 0x00000000004026be in Thread_tree::Tree_insert (this=0x7fffffffe7c0, creator="user1234", root=0x604110) at classes.cpp:218
#3 0x00000000004024e4 in Thread_tree::Tree_sort (this=0x7fffffffe7c0, Thread1=0x616d98, root=0x604110) at classes.cpp:197
#4 0x00000000004023b7 in Thread_tree::Thread_tree (this=0x7fffffffe7c0, Thread1=0x616d98) at classes.cpp:181
#5 0x0000000000402f03 in main () at main.cpp:19
我不明白问题是什么,因为从我从消息中了解到的情况来看,程序在第二次调用时会崩溃Tree_insert()
你的问题是Tree_create(this->node_root);
Tree_create
创建传递给它的指针的副本,并将NULL
分配给此副本,而this->node_root
保持不变,并且在调用Tree_sort
时未初始化。
最简单的解决方案是将NULL
(或nullptr
c++11
中更可取(直接分配给this->node_root
这同样适用于您的其他方法。按值传递指针时,对它的任何赋值都是在复制时完成的。这里的解决方案是通过引用传递指针(例如Tree_create(Tree_node*& root)
(
相关文章:
- 将无符号char*转换为std::istream*C++
- 如何在对<char>C++程序进行逆向工程的同时将 std::basic_string 转换为 Rust 可读值?
- 将函数参数"const char*"转换为"std::string_view"是
- std::string 构造函数如何处理固定大小的 char[]?
- 如何使用多次 memcpy 转换回 std::vector<unsigned char>?
- 在标准中,模板参数的语法在哪里定义,例如,'std::function<int(char)>'?
- 为什么从 char 转换为 std::byte 可能是未定义的行为?
- <char> 使用 Vulkan 映射内存时如何使用 std::vector 而不是 void**?
- 类型为 'std::map< char,int > 的 <Swig 对象的代理
- C++:将模板类型 char 转换为 std::string
- 有效地将数据加载到 std::vector 中<char>
- 为什么 char 可以在 std::array 中初始化为 nullptr,而不是单独初始化?
- 从 std::string 到 std::array<char,size> 的 memcopy 额外数据是否是一种未定义的行为?
- "No suitable conversion function from 'std::string' to 'const char *' exists"
- 未定义模板"std::__1::basic_istringstream<char, std::__1::char_traits<char>, std::__1::allocator&
- 获取未知分段错误"(地址)在 std::__cxx11::basic_string<char, std::char_traits<char>,..., std::分配器<cha
- 如何创建 std::vector of char/std::byte,其中第一个字节对齐到 16 个字节,但没有填充
- C++和Visual Studio错误 - 不存在从"std::basic_ostream<char, std::char_traits<char>>"到"int"的合适转换功
- 将 std::vector 的一部分转换为<char> std::array<char, n>, c++11
- 错误:与 std::operator<< <std::char_traits<char> >(*&std::cout),((常量字符*)中的运算符<<不