- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我所有的程序都因为 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;
}
最佳答案
此程序显示出来自以下语句的内存损坏迹象:
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];
你实际上写在 meanings[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
释放以及分配给 new[]
的内容应该用 delete[]
销毁.上面有一个线程:delete vs delete[] operators in C++ .在 how does delete know it is an array 的答案中可以找到一些很好的背景。 .
下面是如何在不使用任何昂贵的工具或难以学习的工具的情况下调试程序。
如果像这样将调试打印添加到构造函数和析构函数中:
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;
}
和
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
}
同样在 add_meaning() 中,你会得到
[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
这里看起来令人担忧的是allocated 0 bytes
.由于 add_meanings() 中的代码包含:
if (meanings_ctr < 1){
meanings[ meanings_ctr ] = new char [strlen(p2c) + 1];
它使用内存 @ meanings[0]
未分配并导致腐败。
这里是所有累积的变化供引用:
25c25
< meanings = new char * [meanings_ctr]; // Allocate Space for meanings
---
> meanings = new char * [meanings_ctr + 1]; // Allocate Space for meanings
30,31c30,31
< while(meanings_ctr-->0){
< delete meanings[meanings_ctr];
---
> while(meanings_ctr-- > 0){
> delete [] meanings[meanings_ctr];
58c58
< temp_meaning = new char * [meanings_ctr-1];
---
> temp_meaning = new char * [meanings_ctr ];
64c64
< delete meanings[temp_ctr];
---
> delete [] meanings[temp_ctr];
69c69
< meanings = new char * [meanings_ctr];
---
> meanings = new char * [meanings_ctr + 1];
76,77c76,77
< for (temp_ctr =0; temp_ctr<meanings_ctr;temp_ctr++){
< delete temp_meaning[temp_ctr];
---
> for (temp_ctr =0; temp_ctr<meanings_ctr;temp_ctr++){
> delete [] temp_meaning[temp_ctr];
关于c++ - 内存分配 - 正确使用 delete,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10397544/
我写了这个课: class StaticList { private: int headFree; int headList; int locNe
我目前正在使用 SQL Server Management Studio 2005,我遇到了一些问题,但首先是我的 DB 架构的摘录(重要的): imghack link to the image 我
范围:两个表。创建新顾客时,他们会将一些有关他们的信息存储到第二个表中(这也是使用触发器完成的,它按预期工作)。这是我的表结构和关系的示例。 表 1-> 赞助人 +-----+---------+--
我想知道,在整个程序中,我使用了很多指向 cstrings 的 char* 指针,以及其他指针。我想确保在程序完成后删除所有指针,即使 Visual Studio 和 Code Blocks 都为我做
考虑以下代码: class Foo { Monster* monsters[6]; Foo() { for (int i = 0; i < 6; i++)
关于 this page , 是这么写的 One reason is that the operand of delete need not be an lvalue. Consider: delet
我无法在 DELETE CASCADE ON UPDATE CASCADE 上添加外键约束。 我使用两个简单的表格。 TAB1 有 2 列:ID int(10) unsigned NOT NULL A
你好,有没有办法把它放在一个声明中? DELETE e_worklist where wbs_element = '00000000000000000054TTO'. DELETE e_workli
我有一个表,它是我系统的核心,向我的客户显示的所有结果都存储在那里。它增长得非常快,因此每 3 小时我应该删除早于 X 的记录以提高性能。 仅删除这些记录就足够了,还是应该在删除后运行优化表? 我正在
这个问题在这里已经有了答案: delete vs delete[] operators in C++ (7 个答案) 关闭 9 年前。 做和做有什么区别: int* I = new int[100]
为什么这段代码是错误的?我是否遗漏了有关 delete 和 delete[] 行为的内容? void remove_stopwords(char** strings, int* length) {
当我使用 new [] 申请内存时。最后,我使用 delete 来释放内存(不是 delete[])。会不会造成内存泄漏? 两种类型: 内置类型,如 int、char、double ... 我不确定。
所以在代码审查期间,我的一位同事使用了 double* d = new double[foo]; 然后调用了 delete d。我告诉他们应该将其更改为 delete [] d。他们说编译器不需要基本
范围:两个表。当一个新顾客被创建时,他们将一些关于他们的信息存储到第二个表中(这也是使用触发器完成的,它按预期工作)。这是我的表结构和关系的示例。 表 1-> 赞助人 +-----+---------
C++14 介绍 "sized" versions of operator delete ,即 void operator delete( void* ptr, std::size_t sz ); 和
我正在执行类似的语句 DELETE FROM USER WHERE USER_ID=1; 在 SQLDeveloper 中。 由于用户在许多表中被引用(例如用户有订单、设置等),我们激活了 ON DE
出于某种原因,我找不到我需要的确切答案。我在这里搜索了最后 20 分钟。 我知道这很简单。很简单。但由于某种原因我无法触发触发器.. 我有一个包含两列的表格 dbo.HashTags |__Id_|_
这是我的代码: #include #include #include int main() { setvbuf(stdout, NULL, _IONBF, 0); setvbuf
是否可以在 postgres 中使用单个命令删除所有表中的所有行(不破坏数据库),或者在 postgres 中级联删除? 如果没有,那么我该如何重置我的测试数据库? 最佳答案 is it possib
我想删除一些临时文件的内容,所以我正在开发一个小程序来帮我删除它们。我有这两个代码示例,但我对以下内容感到困惑: 哪个代码示例更好? 第一个示例 code1 删除文件 1 和 2,但第二个示例 cod
我是一名优秀的程序员,十分优秀!