我创建了实现指向结构的指针数组的类...我知道如何将记录添加到这个数组,但我不知道如何正确删除它们,因此我有内存泄漏。我的数组的大小在需要时会增加,我知道数组的大小以及那里有多少记录。我习惯于用具有垃圾收集器的语言进行编码,所以这对我来说很困惑。如果你们中的任何人能告诉我如何正确释放该数组,我会很高兴。
请注意,我不能使用vector
. 我仅限于包括以下内容:
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
我的代码:
struct DbRecord
{
string oName;
string oAddr;
string cName;
string cAddr;
};
class CCompanyIndex
{
public: CCompanyIndex(void);~CCompanyIndex(void);
bool Add(const string & oName,
const string & oAddr,
const string & cName,
const string & cAddr);
bool Del(const string & oName,
const string & oAddr);
bool Search(const string & oName,
const string & oAddr,
string & cName,
string & cAddr) const;
int size;
int position;
DbRecord * * db;
};
CCompanyIndex::CCompanyIndex(void)
{
db = new DbRecord * [1000];
size = 1000;
position = 0;
}
CCompanyIndex::~CCompanyIndex(void)
{
}
int main(int argc, char const * argv[])
{
CCompanyIndex c1;
// do something..with c1, i.e. add there some records to array
// ...
// ...
// delete it now
}