#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class sample {
char *s;
public:
sample() { s = 0; }
sample(const sample &ob); //复制构造函数
~sample() { if(s) delete [] s; cout << "Freeing s\n"; }
void show() { cout << s << "\n"; }
void set (char *str);
};
//复制构造函数
sample::sample(const sample &ob)
{
cout << "Copy Constructor called.\n";
s = new char[strlen(ob.s) + 1];
strcpy(s, ob.s);
}
//加载一个字符串
void sample::set(char *str)
{
s = new char[strlen(str) + 1];
strcpy(s, str);
}
//返回sample类型对象
sample input()
{
char instr[80];
sample str;
cout << "Enter a string: ";
cin >> instr; //字符串遇空格或回车终止
str.set(instr);
return str;
}
int main()
{
sample ob;
//将函数返回的对象赋值给 ob
ob = input(); //错误!!
ob.show();
return 0;
}
[beyes@beyes cpp]$ ./reterr
Enter a string: hello
Freeing s
*** glibc detected *** ./reterr: double free or corruption (fasttop): 0x09d40008 ***
======= Backtrace: =========
/lib/libc.so.6[0x4bbb52b5]
/usr/lib/libstdc++.so.6(_ZdlPv+0x20)[0x4cf05460]
/usr/lib/libstdc++.so.6(_ZdaPv+0x1c)[0x4cf054bc]
./reterr[0x8048987]
./reterr[0x80488d5]
/lib/libc.so.6(__libc_start_main+0xf3)[0x4bb5d413]
./reterr[0x80486f1]
======= Memory map: ========
008bd000-008be000 r-xp 00000000 00:00 0 [vdso]
08048000-08049000 r-xp 00000000 fd:02 1188114 /home/beyes/cpp/reterr
08049000-0804a000 rw-p 00000000 fd:02 1188114 /home/beyes/cpp/reterr
09d40000-09d61000 rw-p 00000000 00:00 0 [heap]
4bb23000-4bb40000 r-xp 00000000 fd:01 1966840 /lib/ld-2.14.so
4bb40000-4bb41000 r--p 0001d000 fd:01 1966840 /lib/ld-2.14.so
4bb41000-4bb42000 rw-p 0001e000 fd:01 1966840 /lib/ld-2.14.so
4bb44000-4bcc9000 r-xp 00000000 fd:01 1967407 /lib/libc-2.14.so
4bcc9000-4bcca000 ---p 00185000 fd:01 1967407 /lib/libc-2.14.so
4bcca000-4bccc000 r--p 00185000 fd:01 1967407 /lib/libc-2.14.so
4bccc000-4bccd000 rw-p 00187000 fd:01 1967407 /lib/libc-2.14.so
4bccd000-4bcd0000 rw-p 00000000 00:00 0
4bd00000-4bd28000 r-xp 00000000 fd:01 1967504 /lib/libm-2.14.so
4bd28000-4bd29000 r--p 00028000 fd:01 1967504 /lib/libm-2.14.so
4bd29000-4bd2a000 rw-p 00029000 fd:01 1967504 /lib/libm-2.14.so
4bd2c000-4bd48000 r-xp 00000000 fd:01 1971304 /lib/libgcc_s-4.6.0-20110603.so.1
4bd48000-4bd49000 rw-p 0001b000 fd:01 1971304 /lib/libgcc_s-4.6.0-20110603.so.1
4ce54000-4cf36000 r-xp 00000000 fd:01 662924 /usr/lib/libstdc++.so.6.0.16
4cf36000-4cf3a000 r--p 000e1000 fd:01 662924 /usr/lib/libstdc++.so.6.0.16
4cf3a000-4cf3c000 rw-p 000e5000 fd:01 662924 /usr/lib/libstdc++.so.6.0.16
4cf3c000-4cf42000 rw-p 00000000 00:00 0
b774a000-b774d000 rw-p 00000000 00:00 0
b7762000-b7766000 rw-p 00000000 00:00 0
bfe2b000-bfe4c000 rw-p 00000000 00:00 0 [stack]
Aborted (core dumped)
Enter a string: hello
Copy Constructor called.
Freeing s
Freeing s
葺葺葺葺葺葺葺葺
#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class sample {
char *s;
public:
sample();
sample(const sample &ob);
~sample()
{
if(s) {
delete [] s;
}
cout << "Freeing s\n";
}
void show() { cout << s << "\n"; }
void shows() { cout << &s << endl; }
void set (char *str);
sample operator=(const sample &ob); //重载赋值运算符
};
//普通构造函数
sample::sample()
{
s = new char('\0'); //s指向一个空的字符串
}
//复制构造函数
sample::sample(const sample &ob)
{
cout << "Copy Constructor-1" << endl;
s = new char[strlen(ob.s) + 1];
strcpy(s, ob.s);
}
//装载一个字符串
void sample::set(char *str)
{
s = new char[strlen(str) + 1];
strcpy(s, str);
}
//重载赋值运算符
sample sample::operator=(const sample &ob)
{
cout << "Copy Constructor-2" << endl;
//如果对象中变量s的内存小于ob中s的内存,那么就重新分配内存
if (strlen(ob.s) > strlen(s)) {
delete [] s;
s = new char[strlen(ob.s) + 1];
}
strcpy(s, ob.s);
return *this;
}
//返回sample类型的对象
sample input()
{
char instr[80];
sample str;
cout << "Enter a string: ";
cin >> instr;
str.set(instr);
return str;
}
int _tmain(int argc, _TCHAR* argv[])
{
sample ob;
//将函数返回的对象赋给ob
ob = input();
ob.show();
return 0;
}
Enter a string: hello
Copy Constructor-1
Freeing s
Copy Constructor-2
Copy Constructor-1
Freeing s
Freeing s
hello
Freeing s
$ ./reloadeq
Enter a string: hello
Copy Constructor-2
Copy Constructor-1
Freeing s
Freeing s
hello
Freeing s
Copy Constructor-1
Freeing s
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |