free()下一个大小无效(快速)-C++内存错误

free() invalid next size (fast) -C++ Memory error

本文关键字:-C++ 内存 错误 快速 下一个 free 无效      更新时间:2023-10-16

我的程序mycpp.c抛出内存错误,我认为这个错误是由于覆盖对象指针而引起的,但我无法跟踪错误的根本原因。我觉得"ref3[1]=ref3[0]+reference;"这一行引起了问题,我对此进行了评论。但这对我没有帮助。你能帮我解决这个错误吗。mycpp.c

void mycpp::filldata(ptrStructda pStData)
   1871 {
             ../stmt/...
   1947         String s2("");
   1948         strcat(ref3[0],reference);
   1949         strcat(s2,ref3[0]);
   1950            // ref3[1]= ref3[0] +reference;
   1951             s2.replace_char('-','.');
   1952            // Clean and hold the output value
   1953             temp_Buffer->erase();
   1954         *temp_Buffer = "";
   1955         cout<<"S2:t"<<s2<<endl;
   1956 //      strcpy(*temp_Buffer,s2);
   1957 }

String.c

String::String()
{
        _text = new char[1024];
}
String::String(char *ch)
{
        if (ch == NULL )
        {
                // if input char* is empty - allocate short buffer
                // and set it to ""
                _text = new char[2];
                strcpy(_text, "");
        }
        else
        {   
                _text = new char[strlen(ch) + 1];
                    if(_text)
                         strcpy(_text, ch);
                else
                {
                        _text = new char[2];
                        strcpy(_text, "");
                }
        }
}
String::String(int iLen)
{
        _text = new char[iLen+1];
}
String::String(String const & string)//jl202 added const
{
       _text = new char[string.length() + 1];
       strcpy(_text,string);
}
String::~String()
{
        delete[] _text;
}
String &String::operator=(String &ptrStr)
{
        delete[] _text;
        _text = new char[ptrStr.length() + 1];
        strcpy(_text, ptrStr);
        return *this;
}
String &String::operator=(char *ch)
{
        delete[] _text;
        _text = new char[strlen(ch) + 1];
        strcpy(_text, ch);
        return *this;
}
void String::erase()
{
        delete[] _text;
        _text = new char[1];
}

String.h

class String
{
    private:
            char *_text;
            friend class String_Iterator;
    public:
            // ctors and dtor
            explicit String ();                     // create String as of 1024 chars
            explicit String (char *);
            explicit String (int );
            String (String const & );               // copy ctor
            ~String();
            /////////////////
            // conversions:
            /////////////////
            //  to C-like type char *
            operator  char *() {return _text;}
            operator const char*()const
            {
               return (const_cast <char *>(_text) );
            }
 };

用于观察的gdb输入

(gdb) bt
   #5  0x0000000000402fda in String::~String (this=0x7fffffffd2f0, __in_chrg=<value optimized out>) at String.c:55
**#6  0x000000000040d58c in mycpp::filldata (this=0x61f0e0, pStData=0x7fffffffdd50) at mycpp.c:1955**
#7  0x000000000041159d in mycpp::base (this=0x61f0e0, pStData=0x7fffffffdd50, account_id=0x6418e0 "0300130",
    page_balance=0x7fffffffdf38, items_on_page=0x7fffffffdf34, txn_per_acc=0x7fffffffdf30, total_cash_bal=0x7fffffffdf28, total_cv_bal=0x7fffffffdf20)
    at mycpp.c:1328
#8  0x0000000000414e77 in mycpp::Proc (this=0x61f0e0) at mycpp.c:899
#9  0x000000000041704e in mycpp::Run (this=0x61f060) at mycpp.c:97
#10 0x0000000000417146 in main (argc=3, argv=0x7fffffffe1f8) at mycpp.c:2264

谢谢你调查此事。寻找有价值的解决方案

String s2("");
...
strcat(s2,...);

这绝对是个坏主意。在第一行中,您将分配一个长度为1的char数组,并将其分配给s2._text。对于strcat,您尝试将字符附加到s2._text,但没有为其保留额外的字符,因此您正在覆盖(损坏(不应该写入的内存(在程序的随机位置造成意外效果(,当然,除非strcat(..)的第二个参数是空字符串。

尝试std::string。这应该行得通。

问题:

未定义的内容:

String::String()
{
        _text = new char[1024];
        // The content of this array is not defined.
        // It may not contain a null terminator.
        // FIX:
        _text[0] = '';
}
String::String(char *ch)
{
        if (ch == NULL )  // Should use `nullptr`
        {
                _text = new char[2];   // Why 2!
                strcpy(_text, "");
        }
        else
        {   
                _text = new char[strlen(ch) + 1];
                    if(_text)                     // new never returns null.
                                                  // So this test is usless.
                                                  // It works or it throws.
                         strcpy(_text, ch);
                else
                {
                        _text = new char[2];     // What would have happened if this failed?
                        strcpy(_text, "");
                }
        }
}
String::String(int iLen)
{
        _text = new char[iLen+1];  // Again not null termianted.
}
String::String(String const & string)//jl202 added const
{
       _text = new char[string.length() + 1];
       strcpy(_text,string);
}
String::~String()
{
        delete[] _text;
}
// Normally you would set the parameter as const reference.
String &String::operator=(String &ptrStr)
{
        delete[] _text;
        _text = new char[ptrStr.length() + 1];
        strcpy(_text, ptrStr);
        return *this;
}
String &String::operator=(char *ch)
{
        delete[] _text;
        _text = new char[strlen(ch) + 1];
        strcpy(_text, ch);
        return *this;
}
void String::erase()
{
        delete[] _text;
        _text = new char[1]; // This does not initialize the string with a null terminator.
}