在压入vector时读取大小为4的数据无效

Invalid read of size 4 during pushing to vector

本文关键字:小为 数据 无效 读取 vector      更新时间:2023-10-16

你好,我有一个错误从valgrind:

==19383== Invalid read of size 4
==19383==    at 0x57F69FB: xy(int, int) (Generator.cpp:228)
==19383==    by 0x57F71E2: getNext() (Generator.cpp:164)
             . . .
             . . .

==19383==  Address 0x90ddbec is 4 bytes before a block of size 4 alloc'd
==19383==    at 0x4C28C20: malloc (vg_replace_malloc.c:296)
==19383==    by 0x57FE9B7: operator new(unsigned long) (in /xx.so)
==19383==    by 0x57F79BA: allocate (new_allocator.h:104)
==19383==    by 0x57F79BA: allocate (alloc_traits.h:357)
==19383==    by 0x57F79BA: _M_allocate (stl_vector.h:170)
==19383==    by 0x57F79BA: void std::vector<int, std::allocator<int> >::_M_emplace_back_aux<int const&>(int const&) (vector.tcc:412)
==19383==    by 0x57F6F49: push_back (stl_vector.h:923)
==19383==    by 0x57F6F49: reset() (Generator.cpp:142)
             . . .
             . . .
==19383==    by 0x40A8FC: main (main.cpp:24)
==19383== 
==19383== 
==19383== ---- Attach to debugger ? --- [Return/N/n/Y/y/C/c] ---- 

和reset()函数看起来像:

void reset()
{
    this->first = true;
    std::vector<int>* counts = this->counts_iterator.getNext();
    (*this->picks).clear();
    for(unsigned int i = 0; i < this->variant->groups.size(); i++)
    {
        (*this->picks).push_back(std::vector<int> ());
        for(int j = 0; j < (*counts)[i]; j++)
        {
            (*this->picks)[i].push_back(j);
        }
    }
}

在调用函数reset()之前:

this->picks = new std::vector<std::vector<int> > ();

有变量定义:

std::vector<std::vector<int> >* picks;

我不知道为什么我得到这个错误。什么好主意吗?由于

正如@ecatmur所说的问题在其他地方,在文件sy .cpp中,正如valgrind所说的…