Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 对向量使用推回功能时出现分段错误_C++_Vector_Segmentation Fault_Std - Fatal编程技术网

C++ 对向量使用推回功能时出现分段错误

C++ 对向量使用推回功能时出现分段错误,c++,vector,segmentation-fault,std,C++,Vector,Segmentation Fault,Std,因此,我试图从头构建一个带有自定义哈希函数的哈希表,在测试insert函数时遇到了分段错误问题。如果我正确读取了gdb输出,那么在我的wn_to_bool函数中尝试对布尔向量进行推回时就会出现问题(获取一个整数并将其转换为布尔向量)。我尝试调试这个问题已经有一段时间了,但还没有找到解决方案。如果有任何帮助,我将不胜感激,如果必要,我可以用调试中引用的其余代码更新帖子。先谢谢你 无新运算符的GDB输出 Program received signal SIGSEGV, Segmentation fa

因此,我试图从头构建一个带有自定义哈希函数的哈希表,在测试insert函数时遇到了分段错误问题。如果我正确读取了gdb输出,那么在我的wn_to_bool函数中尝试对布尔向量进行推回时就会出现问题(获取一个整数并将其转换为布尔向量)。我尝试调试这个问题已经有一段时间了,但还没有找到解决方案。如果有任何帮助,我将不胜感激,如果必要,我可以用调试中引用的其余代码更新帖子。先谢谢你

无新运算符的GDB输出

Program received signal SIGSEGV, Segmentation fault.
0x000055555555668b in std::vector<bool, std::allocator<bool> >::push_back (this=0x0, __x=false) at /usr/include/c++/9.2.0/bits/stl_bvector.h:955
955             if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_addr())
(gdb) bt
#0  0x000055555555668b in std::vector<bool, std::allocator<bool> >::push_back (this=0x0, __x=false) at /usr/include/c++/9.2.0/bits/stl_bvector.h:955
#1  0x0000555555556302 in HashTable<int, int>::wn_to_bool (this=0x7fffffffe3f0, wn=2) at Data_Structures/Hash_Table.cpp:186
#2  0x0000555555556230 in HashTable<int, int>::hashfct (this=0x7fffffffe3f0, key=2) at Data_Structures/Hash_Table.cpp:146
#3  0x0000555555556163 in HashTable<int, int>::insert (this=0x7fffffffe3f0, key=2, value=3) at Data_Structures/Hash_Table.cpp:53
#4  0x0000555555555b0f in main (argc=1, argv=0x7fffffffe4f8) at main.cpp:16
Program received signal SIGABRT, Aborted.
0x00007ffff7ab8f25 in raise () from /usr/lib/libc.so.6
(gdb) bt
#0  0x00007ffff7ab8f25 in raise () from /usr/lib/libc.so.6
#1  0x00007ffff7aa2897 in abort () from /usr/lib/libc.so.6
#2  0x00007ffff7b03758 in __malloc_assert () from /usr/lib/libc.so.6
#3  0x00007ffff7b05f6f in sysmalloc () from /usr/lib/libc.so.6
#4  0x00007ffff7b06d32 in _int_malloc () from /usr/lib/libc.so.6
#5  0x00007ffff7b07e84 in malloc () from /usr/lib/libc.so.6
#6  0x00007ffff7e47cba in operator new (sz=40) at /build/gcc/src/gcc/libstdc++-v3/libsupc++/new_op.cc:50
#7  0x000055555555633d in HashTable<int, int>::wn_to_bool (this=0x7fffffffe3f0, wn=2) at Data_Structures/Hash_Table.cpp:181
#8  0x0000555555556230 in HashTable<int, int>::hashfct (this=0x7fffffffe3f0, key=2) at Data_Structures/Hash_Table.cpp:146
#9  0x0000555555556163 in HashTable<int, int>::insert (this=0x7fffffffe3f0, key=2, value=3) at Data_Structures/Hash_Table.cpp:53
#10 0x0000555555555b0f in main (argc=1, argv=0x7fffffffe4f8) at main.cpp:16
程序接收信号SIGSEGV,分段故障。
std::vector::push_back(this=0x0,_x=false)中的0x0000555668b位于/usr/include/c++/9.2.0/bits/stl_bvector.h:955
955如果(此->\u M\u impl.\u M\u finish.\u M\u p!=此->\u M\u impl.\u end\u addr())
(gdb)英国电信
#在/usr/include/c++/9.2.0/bits/stl_bvector.h:955处的std::vector::push_back(this=0x0,_x=false)中0 0x0000555668b
#1 0x0000555556302位于数据结构/哈希表处的哈希表::wn_to_bool(this=0x7fffffffe3f0,wn=2)。cpp:186
#在数据结构/哈希表处的HashTable::hashfct(this=0x7fffffffe3f0,key=2)中有2个0x00005556230。cpp:146
#3哈希表中的0x00005556163::在数据结构/哈希表处插入(this=0x7fffffffe3f0,key=2,value=3)。cpp:53
#main中的4 0x00005555B0F(argc=1,argv=0x7fffffffe4f8)位于main处。cpp:16
带有新运算符的GDB输出

Program received signal SIGSEGV, Segmentation fault.
0x000055555555668b in std::vector<bool, std::allocator<bool> >::push_back (this=0x0, __x=false) at /usr/include/c++/9.2.0/bits/stl_bvector.h:955
955             if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_addr())
(gdb) bt
#0  0x000055555555668b in std::vector<bool, std::allocator<bool> >::push_back (this=0x0, __x=false) at /usr/include/c++/9.2.0/bits/stl_bvector.h:955
#1  0x0000555555556302 in HashTable<int, int>::wn_to_bool (this=0x7fffffffe3f0, wn=2) at Data_Structures/Hash_Table.cpp:186
#2  0x0000555555556230 in HashTable<int, int>::hashfct (this=0x7fffffffe3f0, key=2) at Data_Structures/Hash_Table.cpp:146
#3  0x0000555555556163 in HashTable<int, int>::insert (this=0x7fffffffe3f0, key=2, value=3) at Data_Structures/Hash_Table.cpp:53
#4  0x0000555555555b0f in main (argc=1, argv=0x7fffffffe4f8) at main.cpp:16
Program received signal SIGABRT, Aborted.
0x00007ffff7ab8f25 in raise () from /usr/lib/libc.so.6
(gdb) bt
#0  0x00007ffff7ab8f25 in raise () from /usr/lib/libc.so.6
#1  0x00007ffff7aa2897 in abort () from /usr/lib/libc.so.6
#2  0x00007ffff7b03758 in __malloc_assert () from /usr/lib/libc.so.6
#3  0x00007ffff7b05f6f in sysmalloc () from /usr/lib/libc.so.6
#4  0x00007ffff7b06d32 in _int_malloc () from /usr/lib/libc.so.6
#5  0x00007ffff7b07e84 in malloc () from /usr/lib/libc.so.6
#6  0x00007ffff7e47cba in operator new (sz=40) at /build/gcc/src/gcc/libstdc++-v3/libsupc++/new_op.cc:50
#7  0x000055555555633d in HashTable<int, int>::wn_to_bool (this=0x7fffffffe3f0, wn=2) at Data_Structures/Hash_Table.cpp:181
#8  0x0000555555556230 in HashTable<int, int>::hashfct (this=0x7fffffffe3f0, key=2) at Data_Structures/Hash_Table.cpp:146
#9  0x0000555555556163 in HashTable<int, int>::insert (this=0x7fffffffe3f0, key=2, value=3) at Data_Structures/Hash_Table.cpp:53
#10 0x0000555555555b0f in main (argc=1, argv=0x7fffffffe4f8) at main.cpp:16
程序收到信号SIGABRT,中止。
来自/usr/lib/libc.so.6的提升()中的0x00007ffff7ab8f25
(gdb)英国电信
#在/usr/lib/libc.so.6的raise()中0 0x00007FF7AB8F25
#1 0x00007FF7AA2897位于/usr/lib/libc.so.6的abort()中
#来自/usr/lib/libc.so.6的2 0x00007FF7B03758 in u_malloc_assert()
#来自/usr/lib/libc.so.6的sysmalloc()中的3 0x00007FF7B05F6F
#4 0x00007FF7B06D32 in_int_malloc(),来自/usr/lib/libc.so.6
#来自/usr/lib/libc.so.6的malloc()中的5 0x00007FF7B07E84
#6 0x00007FF7E47CBA位于/build/gcc/src/gcc/libstdc++-v3/libsupc++/new_op.cc:50处的运算符new(sz=40)中
#7 0x000055555633D在哈希表中::wn_to_bool(this=0x7fffffffe3f0,wn=2)在数据结构/哈希表中。cpp:181
#8数据结构/Hash_表处HashTable::hashfct(this=0x7fffffffe3f0,key=2)中的0x00005556230。cpp:146
#9哈希表中的0x00005556163::在数据结构/哈希表处插入(this=0x7fffffffe3f0,key=2,value=3)。cpp:53
#主管道中的10 0x00005555B0F(argc=1,argv=0x7fffffffe4f8)位于主管道中。cpp:16
wn\u to\u bool函数(请注意v最初是一个空向量,因此插入函数将不起作用)

template std::vector*HashTable::wn\u to\u bool(长wn)
{
std::vector*v;
while(wn!=0)
{
如果(wn%2==0)
v->推回(假);
其他的
v->push_back(真);
wn=wn/2;
}
返回v;
}

执行
std::vector*v
时,将有一个指向
std::vector
的未初始化指针。这意味着
v
有一些任意值,而没有分配给任何分配的内存地址。 您需要创建一个
新的
向量并将其分配给
v

template <typename K, typename V> std::vector<bool>* HashTable<K,V>::wn_to_bool(long wn)
{
    auto v = new std::vector<bool>();

    for(;wn!=0;wm/=2)
        v->push_back(wn % 2 != 0);

    return v; 
}
template std::vector*HashTable::wn\u to\u bool(长wn)
{
自动v=新的标准::向量();
对于(;wn!=0;wm/=2)
v->推回(wn%2!=0);
返回v;
}
您不需要使用
if
语句,因为您直接使用的是布尔语句的结果。您只需向后推
wn%2!=0
这比使用
if
更有效。您还可以使用
for
循环来提高可读性,因为您将在一行代码中编写整个逻辑

正如注释中所建议的,在堆内存中创建对象实例(即使用
new
)时应格外小心,因为当不再需要分配的内存时,您必须注意删除该内存,否则会导致内存泄漏。强烈建议改为使用

请注意,v最初是一个空向量

事实并非如此。它是一个统一的指针。看不到实际的矢量

不要使用原始指针

撇开这样一个函数的实用性不谈,这里有一个编写它的方法

template <typename K, typename V> 
std::vector<bool> // no pointer
HashTable<K,V>::wn_to_bool(long wn)
{
    std::vector<bool> v; //no pointer
    while(wn != 0)
    {
        if(wn % 2 == 0)
            v.push_back(false);
        else
            v.push_back(true);
        wn = wn / 2;
    }
    return v; 
}
模板
std::vector//无指针
哈希表::wn_to_bool(长wn)
{
std::vector v;//没有指针
while(wn!=0)
{
如果(wn%2==0)
v、 推回(假);
其他的
v、 推回(真);
wn=wn/2;
}
返回v;
}
因此插入功能将不起作用)


insert
可以很好地处理空向量,例如..
vector v;v、 insert(v.begin(),42)

v
未初始化,您有一个悬空指针=>未定义的引用。为什么要使用指向向量的指针?同样要小心,
vector
相当奇怪。“取一个整数并将其转换为布尔向量”这是一个相当可疑的实用程序。为什么您需要布尔向量?很抱歉,前面没有说得更具体,但我确实使用了您的方法尝试了新操作符,并且std::vector*v=new std::vector,但它们导致了上面gdb输出中描述的中止(使用新操作符)。@patelvrajn根据您的公开代码(IMHO),答案是正确的。如果你尝试了其他不那么有效的方法,那是另一个问题。请注意,v最初是一个空向量,因此insert函数不能工作。不,它不是一个空向量,它是一个未初始化的指针(正如正确指出的)。插入初始为空的向量应该有效。@如果向量为空(本例中不包含布尔值),则根据STD文档进行的Scheff插入无效。插入操作需要迭代器,如果没有要指向的对象,迭代器无法指向任何位置。请不要建议毫无戒心的新手使用新的