1

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

#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;
}
4

3 回答 3

2

当您分配多维数组时,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;

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

于 2012-04-26T18:43:03.927 回答
2
delete [] meanings; // Deleting the memory we allocated

不会摆脱分配的内存,只有指针本身。

要释放实际内存,您需要遍历meanings数组以及delete []其中的每个元素。

就像是:

for (int i = 0; i < meanings_ctr; ++i)
{
    delete [] meanings[meanings_ctr];
    meanings[meanings_ctr] = NULL;
}
delete [] meanings;

--

对于如果你得到超过 100 个含义(或者通常当你的集合已满)该怎么办的问题,标准技术是分配一个两倍大小的新数组(你可以这样做,因为它是动态的),将您现有的收藏复制到该收藏中,然后处置您现有的收藏。

于 2012-04-26T18:43:16.523 回答
0

我会使用一个简单的链表(这是简化的,不完整且未经测试;还应该有适当的 getter/setter 和东西):

class Meaning {
    char text[20];
    Meaning *next;

    Meaning(const char *text) : next(0) {
        strcpy(this->text, text);
    }
}

class Word {
    char text[20];
    Meaning *first;
    Meaning *last;

    Word(const char *text) : first(0), last(0) {
        strcpy(this->text, text);
    }

    ~Word() {
        Meaning *m = first, *n;
        while(m) {
            n = m->next;
            delete m;
            m = n;
        }
    }

    void AddMeaning(const char *text) {
        if (last) {
            last = last->next = new Meaning(text);
        }
        else {
            first = last = new Meaning(text);
        }
    }

    void print() {
        printf("%s:\n\t", text);
        Meaning *m = first;
        while (m) {
            printf("%s, ", m->text);
            m = m->next;
        }
    }
}
于 2012-04-26T18:53:28.300 回答