#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
using namespace std;
class te
{
public:
te(char *chs)
{
if (chs == NULL)
{
return ;
}
m_str = (char *)malloc(strlen(chs) + 1);
sprintf(m_str, "%s", chs);
}
~te()
{
printf("\n call ~te()");
free(m_str);
m_str = NULL;
}
void show()
{
printf("\nsting=%s \n",m_str);
}
private:
char *m_str;
};
void func1(te p)
{
printf("\nin func1 start \n");
//te t1(p);
p.show();
printf("\nin func1 end \n");
}
main()
{
te t2("ddddd");
t2.show();
func1(t2);
printf("\n aaaa \n");
t2.show();
printf("\n cccc \n");
}
=======================
运行结果:
./a.out
sting=ddddd
in func1 start
sting=ddddd
in func1 end
call ~te()
aaaa
sting=
cccc
*** Error in `./a.out': double free or corruption (fasttop): 0x0000000001bd6010 ***
======= Backtrace: =========
浙公网安备 33010602011771号