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

zpqajqem  于 2022-12-30  发布在  其他
关注(0)|答案(3)|浏览(171)

我的程序mycpp.c抛出内存错误,我认为这个错误已经提出,由于覆盖对象指针,但我无法追查出错误的根本原因。我觉得行“ref 3 [1]= ref 3 [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 }

字符串.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) 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

感谢您的关注。寻求您有价值的解决方案

zvms9eto

zvms9eto1#

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

肯定不是个好主意,在第一行,你将分配一个长度为1的char数组,并将它赋给s2._text,而对于strcat,你试图将字符追加到s2._text,但你没有为它保留额外的字符,所以你是在覆盖(损坏)不应该写入的内存(在程序的随机位置导致意外的效果),当然除非strcat(..)的第二个参数是空字符串。

vfwfrxfs

vfwfrxfs2#

试试std::string应该可以。

yhived7q

yhived7q3#

问题:

未定义的内容:

String::String()
{
        _text = new char[1024];
        // The content of this array is not defined.
        // It may not contain a null terminator.
        // FIX:
        _text[0] = '\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.
}

相关问题