free()下一个大小无效(快速)-C++内存错误
free() invalid next size (fast) -C++ Memory error
我的程序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.
}
相关文章:
- 将字符串存储在c++中的稳定内存中
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- Win32编译器选项和内存分配
- 当vector是tje全局变量时,c++中vector的内存管理
- 带内存和隔离功能的SQLite
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 迭代时从向量和内存中删除对象
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 为什么示例代码访问IUnknown中已删除的内存
- 如何在C++类内存结构中创建"spacer"?
- 从构造函数抛出异常时如何克服内存泄漏
- malloc() 可能出现内存泄漏
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 类型总是使用其大小存储在内存中吗
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存