我的所有程序因delete [] meanings;
,delete [] meanings;
,delete [] temp_meaning;
而崩溃,当我删除这3行时它工作正常,所以可能我错误地使用了删除...可以任何人请在这里赐教我?
#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(const 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 * [meanings_ctr]; // Allocate Space for meanings
}
Expression::~Expression() {
while(meanings_ctr-->0){
delete meanings[meanings_ctr];
}
delete [] meanings; // Deleting the memory we allocated
delete [] word_with_several_meanings; // Deleting the memory we allocated
}
void Expression::word(const 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[ meanings_ctr ] = new char [strlen(p2c) + 1];
//strcpy(meanings[ meanings_ctr++ ] , p2c);
// temp
if (meanings_ctr < 1){
meanings[ meanings_ctr ] = new char [strlen(p2c) + 1];
strcpy(meanings[ meanings_ctr++ ] , p2c);
}
else {
int temp_ctr;
char **temp_meaning;
temp_meaning = new char * [meanings_ctr-1];
for(temp_ctr =0; temp_ctr<meanings_ctr;temp_ctr++){
temp_meaning[temp_ctr] = new char [strlen(meanings[ temp_ctr ]) + 1];
strcpy(temp_meaning[temp_ctr], meanings[ temp_ctr ]);
}
for (temp_ctr =0; temp_ctr<meanings_ctr;temp_ctr++){
delete meanings[temp_ctr];
}
delete [] meanings;
meanings = new char * [meanings_ctr];
for(temp_ctr =0; temp_ctr<meanings_ctr;temp_ctr++){
meanings[ temp_ctr ] = new char [strlen(temp_meaning[temp_ctr]) + 1];
strcpy(meanings[ temp_ctr ], temp_meaning[temp_ctr]);
}
meanings[ meanings_ctr ] = new char [strlen(p2c) + 1];
strcpy(meanings[ meanings_ctr ] , p2c);
for (temp_ctr =0; temp_ctr<meanings_ctr;temp_ctr++){
delete temp_meaning[temp_ctr];
}
delete [] temp_meaning;
meanings_ctr++;
}
}
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 very long test");
char str[] = "Meaning_ ";
for(int kx =0; kx<31; kx++){
str[8] = ('A'+kx);
expr3.add_meaning(str);
}
cout << expr3.get_word() << endl;
for( int i = 0; i<expr3.get_total_number_of_meanings(); i++)
cout << " " << expr3.get_meaning(i) << endl;
return 0;
}
答案 0 :(得分:4)
此程序显示出来自以下声明的内存损坏迹象:
meanings_ctr = mctr; // Setting the counter to 0
meanings = new char * [meanings_ctr]; // Allocate Space for meanings
和
meanings = new char * [meanings_ctr];
由于add_meaning()包含以下代码:
if (meanings_ctr < 1){
meanings[ meanings_ctr ] = new char [strlen(p2c) + 1];
你实际上在含义[0]上写,而你为它分配了0个字节。由于C中的索引从0开始,对于索引最高的数组max_index
,您需要分配max_index+1
个元素。对于max_index = 0
的数组,您需要分配1个元素。
换句话说,您需要分配meanings = new char * [meanings_ctr + 1]
代替new char * [meanings_ctr]
,temp_meaning = new char * [meanings_ctr]
代替new char * [meanings_ctr - 1]
。
至于delete
和delete[]
的使用,一般规则是使用new
分配的内容应使用delete
解除分配,并使用{{1}分配的内容应该用new[]
销毁。它上面有一个帖子:delete vs delete[] operators in C++。在how does delete know it is an array的答案中可以找到一些好的背景。
以下是如何在不使用任何难以学习的昂贵工具或工具的情况下调试程序。
如果您将调试打印添加到构造函数和析构函数中,如下所示:
delete[]
并且
Expression::Expression(int mctr ) {
meanings_ctr = mctr; // Setting the counter to 0
meanings = new char * [meanings_ctr]; // Allocate Space for meanings
cout << "[debug] allocated " << sizeof(char*)*meanings_ctr << " bytes @" <<
hex << meanings << dec << endl;
}
,同样在add_meaning()中,你得到了
Expression::~Expression() {
while(meanings_ctr-- > 0){
// if(meanings[meanings_ctr]) delete [] (meanings[meanings_ctr]);
}
cout << "[debug] to deallocate @" << hex << meanings << dec << endl;
// delete [] meanings; // Deleting the memory we allocated
// delete [] word_with_several_meanings; // Deleting the memory we allocated
}
这里令人担忧的是[debug] allocated 0 bytes @0x804c008
[debug] to deallocate @0x804c008
[debug] allocated 4 bytes @0x804c078
...
[debug] allocated 120 bytes @0x804fa78
[debug] to deallocate @0x804fa78
[debug] to deallocate @0x804c260
[debug] to deallocate @0x804c150
。由于add_meanings()中的代码包含:
allocated 0 bytes
它使用未分配的内存@ if (meanings_ctr < 1){
meanings[ meanings_ctr ] = new char [strlen(p2c) + 1];
并导致损坏。
此处参考是所有累积的变化:
meanings[0]
答案 1 :(得分:3)
你必须来自C世界,你不会在C ++程序中找到任何char*
来存储一个单词......你应该看看std::string,它会启发你的一天我可以向你保证。
顺便说一下,在C ++中,我们倾向于摆脱所有delete
和delete []
,你应该选择一本好的C ++书并学习正确的C ++,而不是使用已经使用过的类的C语言30年前。