我需要调整m_Array的大小,这就是说添加11时valgrind中的问题。element m_Max = 10 m_Len = 10
我被允许使用cstdio,cstdlib,cstring和iostream。
/* m_Len is length of array and m_Max is maximal length */
resizing in CAccount::NewAccount
if ( m_Len >= m_Max )
{
if (m_Max == 0) m_Max = 5;
m_Max *= 2;
CAccount * tmp = new CAccount[m_Max];
memcpy ( tmp, m_Array, m_Len * sizeof(CAccount));
delete[] m_Array;
m_Array = tmp;
}
m_Array[m_Len] = x;
m_Len++;
这是与此相关的附加代码:
CAccount
{
public:
~CAccount ( void )
{
delete[] m_Trans;
}
private:
struct Transaction
{
int a;
const char * b;
const char * c;
}
Transaction * m_Trans;
}
CAccount * m_Array;
这就是valgrind所说的
我的喊话...
==2458== Invalid read of size 4
==2458== at 0x8048D67: CAccount::AddTransaction(int const&, char const*, char const*) (bank_test.cpp:121)
==2458== by 0x8049645: CBank::Transaction(char const*, char const*, int, char const*) (bank_test.cpp:268)
==2458== by 0x8049946: main (bank_test.cpp:327)
==2458== Address 0x434b150 is 0 bytes inside a block of size 4 free'd
==2458== at 0x402B598: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==2458== by 0x8048B0A: CAccount::~CAccount() (bank_test.cpp:92)
==2458== by 0x804951E: CBank::NewAccount(char const*, int) (bank_test.cpp:254)
==2458== by 0x804991B: main (bank_test.cpp:323)
==2458==
==2458== Invalid free() / delete / delete[] / realloc()
==2458== at 0x402B598: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==2458== by 0x8048D9A: CAccount::AddTransaction(int const&, char const*, char const*) (bank_test.cpp:121)
==2458== by 0x8049645: CBank::Transaction(char const*, char const*, int, char const*) (bank_test.cpp:268)
==2458== by 0x8049946: main (bank_test.cpp:327)
==2458== Address 0x434b150 is 0 bytes inside a block of size 4 free'd
==2458== at 0x402B598: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==2458== by 0x8048B0A: CAccount::~CAccount() (bank_test.cpp:92)
==2458== by 0x804951E: CBank::NewAccount(char const*, int) (bank_test.cpp:254)
==2458== by 0x804991B: main (bank_test.cpp:323)
==2458==
==2458== Invalid read of size 4
==2458== at 0x8048D67: CAccount::AddTransaction(int const&, char const*, char const*) (bank_test.cpp:121)
==2458== by 0x8049678: CBank::Transaction(char const*, char const*, int, char const*) (bank_test.cpp:269)
==2458== by 0x8049946: main (bank_test.cpp:327)
==2458== Address 0x434b188 is 0 bytes inside a block of size 4 free'd
==2458== at 0x402B598: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==2458== by 0x8048B0A: CAccount::~CAccount() (bank_test.cpp:92)
==2458== by 0x804951E: CBank::NewAccount(char const*, int) (bank_test.cpp:254)
==2458== by 0x804991B: main (bank_test.cpp:323)
==2458==
==2458== Invalid free() / delete / delete[] / realloc()
==2458== at 0x402B598: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==2458== by 0x8048D9A: CAccount::AddTransaction(int const&, char const*, char const*) (bank_test.cpp:121)
==2458== by 0x8049678: CBank::Transaction(char const*, char const*, int, char const*) (bank_test.cpp:269)
==2458== by 0x8049946: main (bank_test.cpp:327)
==2458== Address 0x434b188 is 0 bytes inside a block of size 4 free'd
==2458== at 0x402B598: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==2458== by 0x8048B0A: CAccount::~CAccount() (bank_test.cpp:92)
==2458== by 0x804951E: CBank::NewAccount(char const*, int) (bank_test.cpp:254)
==2458== by 0x804991B: main (bank_test.cpp:323)
==2458==
然后写另一个提示,然后说
==2458== Invalid read of size 4
==2458== at 0x8048AD7: CAccount::~CAccount() (bank_test.cpp:92)
==2458== by 0x804922E: CBank::~CBank() (bank_test.cpp:224)
==2458== by 0x80499F0: main (bank_test.cpp:381)
==2458== Address 0x434b348 is 0 bytes inside a block of size 4 free'd
==2458== at 0x402B598: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==2458== by 0x8048B0A: CAccount::~CAccount() (bank_test.cpp:92)
==2458== by 0x804951E: CBank::NewAccount(char const*, int) (bank_test.cpp:254)
==2458== by 0x804991B: main (bank_test.cpp:323)
==2458==
==2458== Invalid free() / delete / delete[] / realloc()
==2458== at 0x402B598: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==2458== by 0x8048B0A: CAccount::~CAccount() (bank_test.cpp:92)
==2458== by 0x804922E: CBank::~CBank() (bank_test.cpp:224)
==2458== by 0x80499F0: main (bank_test.cpp:381)
==2458== Address 0x434b348 is 0 bytes inside a block of size 4 free'd
==2458== at 0x402B598: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==2458== by 0x8048B0A: CAccount::~CAccount() (bank_test.cpp:92)
==2458== by 0x804951E: CBank::NewAccount(char const*, int) (bank_test.cpp:254)
==2458== by 0x804991B: main (bank_test.cpp:323)
==2458==
==2458==
==2458== HEAP SUMMARY:
==2458== in use at exit: 0 bytes in 0 blocks
==2458== total heap usage: 17 allocs, 27 frees, 1,268 bytes allocated
==2458==
==2458== All heap blocks were freed -- no leaks are possible
==2458==
==2458== For counts of detected and suppressed errors, rerun with: -v
==2458== ERROR SUMMARY: 20 errors from 6 contexts (suppressed: 0 from 0)
最佳答案
这是发生了什么:CAccount * tmp = new CAccount[m_Max];
您创建了20个新对象CAccount。memcpy ( tmp, m_Array, m_Len * sizeof(CAccount));
您将10个旧对象的内容存储到了新数组中。
问题是,由于仅使用memcpy而不使用复制构造函数来处理数据的所有权,因此对象的新旧版本都指向相同的m_Trans
数据。
调用delete[] m_Array;
后,将调用旧对象上的析构函数,它们将删除m_Trans
delete[] m_Trans;
现在,新对象指向已删除的m_Trans
数据,因此访问指针将导致未定义的行为。
解决此问题的最干净方法是使用而不是memcpy ( tmp, m_Array, m_Len * sizeof(CAccount));
做std::copy(m_Array, m_Array + m_Len, tmp)
这将在新对象上调用opreator =,因此您也需要正确进行设置:
CAccount
{
public:
CAccount::CAccount(const CAccount& account)
: m_trans(<initialisation code here>)
{
<Copy the data from the account>
}
该解决方案是最干净的,但次优,因为您正在复制数据,您可以只切换数据的所有者,但是如果您不追求性能,那就更好。