请问:为什么delete的时候出问题了?

isaxu 2003-01-22 07:41:51
#include<stdafx.h>
#include<iostream.h>

char * strcpy(char* dest,const char* source);

void main()
{
char* a = new char;
cout<<"input a:"<<endl;
cin>>a;
char* b = new char;
cout<<"the end is "<<strcpy(b,a);
cout<<"after copy, a is:"<<a<<endl;
cout<<"after copy, b is:"<<b<<endl;
delete b; //如果加上这两个delete,就会出错,可是,书上不是说要释放空间嘛?
delete a;
}

char * strcpy(char* dest,const char* source)
{
char *tmp = new char;
tmp = dest;
while((*source)!='\0')
{
*dest = *source;
source++;
dest++;
}
dest = tmp;
cout<<"dest is "<<dest<<endl;
return dest;
}

...全文
54 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
hfqian 2003-01-23
  • 打赏
  • 举报
回复
#include<stdafx.h>
#include<iostream.h>

char * strcpy(char* dest,const char* source);

void main()
{
char* a = new char[100];
cout<<"input a:"<<endl;
cin>>a;
char* b = new char[100];
cout<<"the end is "<<strcpy(b,a);
cout<<"after copy, a is:"<<a<<endl;
cout<<"after copy, b is:"<<b<<endl;
delete []b;
delete []a;
}

char * strcpy(char* dest,const char* source)
{
char *tmp=dest;
while(*dest++=*source++);
return tmp;
}
本程序已经在VC6.0上测试过了,no problem!


在函数
char * strccpy(char* dest,const char* source)
{
...
}
中,再次分配内存会出现内存泄漏。
isaxu 2003-01-23
  • 打赏
  • 举报
回复
还是以前的程序,把两条delete语句注释掉,就可以通过了
如果改成 delete [] a;delete [] b; 还是会出现debug error!的windows报错阿~
point_to 2003-01-22
  • 打赏
  • 举报
回复
不要定义成strcpy(),因为这是保留函数(所以改成乐strccpy)
已经有了这样一个标准函数乐,意思就是这个样子的。
hdqqq 2003-01-22
  • 打赏
  • 举报
回复
我这里测试的结果是内存overrun,就是说发生内存溢出,可能光new一个char是不够的,需要new char[10],然后delete [] a,删除内存使用,另外,在strcpy中有内存泄漏。
point_to 2003-01-22
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <iostream>
using namespace std;


char * strccpy(char* dest,const char* source);

void main()
{
char* a = new char[10];//要比较的不是一个char 下同!
cout<<"input a:"<<endl;
cin>>a;
char* b = new char[10];
cout<<"the end is "<<strcpy(b,a);
cout<<"after copy, a is:"<<a<<endl;
cout<<"after copy, b is:"<<b<<endl;
delete []b; //如果加上这两个delete,就会出错,可是,书上不是说要释放空间嘛?
delete []a;
}

char * strccpy(char* dest,const char* source)
{
char *tmp = new char;
tmp = dest;
while((*source)!='\0')
{
*dest = *source;
source++;
dest++;
}
dest = tmp;
cout<<"dest is "<<dest<<endl;
return dest;
}



Cybergate 2003-01-22
  • 打赏
  • 举报
回复
接上面的补充一下:

delete[] b;
delete[] a;
glassshark 2003-01-22
  • 打赏
  • 举报
回复
#include<stdafx.h>
#include<iostream.h>

char * strcpy(char* dest,const char* source);

void main()
{
char* a = new char[100];
cout<<"input a:"<<endl;
cin>>a;
char* b = new char[100];
cout<<"the end is "<<strcpy(b,a);
cout<<"after copy, a is:"<<a<<endl;
cout<<"after copy, b is:"<<b<<endl;
delete b; //如果加上这两个delete,就会出错,可是,书上不是说要释放空间嘛?
delete a;
}

char * strcpy(char* dest,const char* source)
{
char *tmp=dest;
while(*dest++=*source++);
return tmp;
}
glassshark 2003-01-22
  • 打赏
  • 举报
回复
#include<stdafx.h>
#include<iostream.h>

char * strcpy(char* dest,const char* source);

void main()
{
char* a = new char[100];
cout<<"input a:"<<endl;
cin>>a;
char* b = new char[100];
cout<<"the end is "<<strcpy(b,a);
cout<<"after copy, a is:"<<a<<endl;
cout<<"after copy, b is:"<<b<<endl;
delete b; //如果加上这两个delete,就会出错,可是,书上不是说要释放空间嘛?
delete a;
}

char * strcpy(char* dest,const char* source)
{
char *tmp=dest;
while(*dest++=*source++)
return tmp;
}

69,375

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧