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

时间:2013-03-14 16:03:35

标签: c++ exception pointers memory-management

我的程序mycpp.c抛出内存错误,我认为由于覆盖了对象指针而引发了此错误,但我无法追踪 错误的根本原因。我觉得行“ref3 [1] = ref3 [0] +引用;”引起了一个问题,我对它进行了评论。但它没有帮助我。请你帮我解决一下这个错误。 的 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

感谢您对此进行调查。 寻求您宝贵的解决方案

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

试试std::string。这应该有用。