【问题标题】:Heap Corruption Detected with find and replace program使用查找和替换程序检测到堆损坏
【发布时间】:2013-07-06 11:06:34
【问题描述】:

我有一个有时可以工作的查找和替换程序,但后来我开始收到此错误:HEAP CORRUPTION DETECTED: after Normal block (#142) at address。 CRT 检测到应用程序在堆缓冲区结束后写入内存。

我不太确定问题出在哪里,因为每次分配内存时,我都会释放它。我肯定错过了什么。如果有人有任何建议,将不胜感激。这是完整的代码:

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

void optimize(char*, const char*, const char*);
bool oldStrValidate(char*, string);

int main()
{
string input, old_str, new_str;
bool oldStrValid = false;
string repeat;

do
{
    cout<<"Enter a string: "<<endl;
    getline(cin,input);
    char* inputPtr = new char[input.length() +1];
    strcpy(inputPtr, input.c_str());



    do
    {
        cout<<"Enter the section of the string you wish to replace."<<endl;
        getline(cin, old_str);
        oldStrValid = oldStrValidate(inputPtr, old_str);
    }while(oldStrValid == false);
    cout<<"What would you like to replace\"" <<old_str<<"\" with?"<<endl;
    getline(cin,new_str);
    char* oldPtr = new char[old_str.length() +1];
    strcpy(oldPtr, old_str.c_str());
    char* newPtr = new char[new_str.length() +1];
    strcpy(newPtr, new_str.c_str());
    optimize(inputPtr, oldPtr, newPtr);
    cout<<"          try again? \"y\" for yes or \"n\" to quit." << endl;
    cout<<"          : ";
    cin>>repeat;
    cin.ignore();
    delete [] inputPtr;
    delete [] oldPtr;
    delete [] newPtr;
}while(repeat == "y");

return 0;
  }


void optimize( char* input_str, const char* old_str, const char* new_str  )
{
string input_string(input_str);
string old_string(old_str);
string new_string(new_str);
size_t position = 0;

while ((position = input_string.find(old_string, position)) != string::npos)
{
  input_string.replace( position, old_string.length(), new_string );
  position += new_string.length();
}

strcpy(input_str, input_string.c_str());
cout << input_string << endl;
}
bool oldStrValidate(char* str, string searchFor)
{
string input(str);
int position = 0;

while ((position = input.find(searchFor, position)) != string::npos)
    return true;

{
    cout<<"the substring you enterd does not exist within the string"<<endl;
    return false;
}

} 

【问题讨论】:

  • 请缩进您的代码。
  • 为什么要复制这么多字符串?哎呀!
  • “每次我分配内存时,我也会释放它。”这太棒了,但这不是堆损坏错误所抱怨的。它说:“CRT 检测到应用程序在堆缓冲区结束后写入内存。”

标签: c++ memory-management heap-memory heap-corruption


【解决方案1】:

如果我不得不猜测,我会说这是因为你用一个你没有空间容纳的大字符串替换了一个小字符串——特别是在optimize() 中破坏了这一行的堆:

strcpy(input_str, input_string.c_str());

【讨论】:

  • 根据显然是优化的函数名称:如果您不需要(想要),为什么要分配更多内存?! :D
【解决方案2】:

您可能想考虑一下当您输入字符串 "a" 然后将 a 替换为 this string is way too long 之类的内容时会发生什么。

您会发现,虽然 C++ 字符串可以很好地处理扩展,但对于 C 字符串却不能这样说。当你执行这一行时:

strcpy(input_str, input_string.c_str());

扩展的字符串被复制到(非常未扩展的)input_str 缓冲区中,因此您的堆损坏。

C++ 字符串的全部是为了防止很多人在使用更原始的 C 字符串时遇到的问题,所以我不完全确定您为什么要恢复使用旧方法。在任何地方都使用 C++ 字符串要好得多。否则,您必须确保自己管理空间。

【讨论】:

    猜你喜欢
    • 2011-07-24
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    • 2012-11-24
    • 2011-08-20
    • 2021-07-10
    • 1970-01-01
    • 2020-08-02
    相关资源
    最近更新 更多