-6

我的程序 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 }

字符串.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];
}

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

感谢您查看这个。寻求您有价值的解决方案

4

2 回答 2

0

试试std::string。那应该行得通。

于 2013-04-24T06:44:52.453 回答
0
String s2("");
...
strcat(s2,...);

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

于 2013-03-14T20:33:43.360 回答