gpt4 book ai didi

c++ - 字典的动态内存分配

转载 作者:行者123 更新时间:2023-11-30 02:04:40 24 4
gpt4 key购买 nike

你好,我需要构建一些类似字典的东西,根据我的代码,每个词都可以有 100 种含义,但也许它只有 5 种含义,那么我将分配 95 个额外的空间,或者它可能有超过 100 种含义然后程序会崩溃,我知道 vector 类非常简单并且可以很好地使用,但任务几乎是构建我自己的 vector 类,以了解它是如何工作的。因此**含义和其他一些东西保持不变,这是我的代码,而且我知道我导致内存泄漏,我怎样才能正确删除? :

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

class Expression {

char *word_with_several_meanings; // like "bank", "class"
char **meanings; // a pointer to a pointer stores all meanings
int meanings_ctr; // meanings counter

//-----------FUNCTIONS------------------------------------------------
public:
void word( char* = NULL );
void add_meaning(char* = NULL);
char* get_word();
int get_total_number_of_meanings();
char* get_meaning(int meanx = 0);
Expression(int mctr = 0); // CTOR
~Expression(); // DTOR
};

Expression::Expression(int mctr ) {
meanings_ctr = mctr; // Setting the counter to 0
meanings = new char * [100]; // Allocate Space for 100 meanings
}

Expression::~Expression() {
delete [] meanings; // Deleting the memory we allocated
delete [] word_with_several_meanings; // Deleting the memory we allocated
}

void Expression::word( char *p2c )
{

word_with_several_meanings = new char[strlen(p2c)+1];
// copy the string, DEEP copy
strcpy(word_with_several_meanings, p2c);
}

void Expression::add_meaning(char *p2c)
{

//meanings = new char * [meanings_ctr+1];
meanings[meanings_ctr] = new char[strlen(p2c)+1];
strcpy(meanings[meanings_ctr++],p2c);


}

char * Expression::get_meaning( int meanx )
{

return *(meanings+meanx);

}

char * Expression::get_word()
{

return word_with_several_meanings;

}

int Expression::get_total_number_of_meanings()
{

return meanings_ctr;

}

int main(void) {
int i;
Expression expr;
expr.word("bank ");
expr.add_meaning("a place to get money from");
expr.add_meaning("b place to sit");
expr.add_meaning("4 letter word");
expr.add_meaning("Test meaning");
cout << expr.get_word() << endl;

for(int i = 0; i<expr.get_total_number_of_meanings(); i++)
cout << " " << expr.get_meaning(i) << endl;
Expression expr2;
expr2.word("class");
expr2.add_meaning("a school class");
expr2.add_meaning("a classification for a hotel");
expr2.add_meaning("Starts with C");
cout << expr2.get_word() << endl;
for( i = 0; i<expr2.get_total_number_of_meanings(); i++)
cout << " " << expr2.get_meaning(i) << endl;

Expression expr3;
expr3.word("A long test ... ");
char str[] = "Meaning_ ";
for (int kx=0;kx<26;kx++)
{
str[8] = (char) ('A'+kx);
expr3.add_meaning(str);
}

cout << expr3.get_word() << endl;
for(i = 0; i < expr3.get_total_number_of_meanings(); i++)
cout << " " << expr3.get_meaning(i) << endl;

return 0;
}

最佳答案

当你用 new 分配一个多维数组时,你就是在用循环分配它,例如

char **x = new char*[size]
for (int i = 0; i < N; i++) {
x[i] = new int[size];
}

所以你也必须以这种方式删除它:

for (int i = 0; i < N; i++) {
delete[] x[i];
}
delete[] x;

因此,当您拥有任意大小的数组时,您必须将它们存储在某个地方以便在析构函数中使用它们。

关于c++ - 字典的动态内存分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10339291/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com