分配空间的字符串数组,释放时出问题,请指点

shangwei97 2007-07-31 05:43:01
char * p_recvmsg; //动态分配
p_recvmsg=new char [50];
memset(p_recvmsg,0x00,50);//赋值为空
.......
char *pc_temp=(char *)malloc(120);
memset(pc_temp,0x00,120);

long i_len = strlen(p_recvmsg);
char pc_[10];
memset(pc_,0x00,10);
for(long i_lop=0;i_lop<i_len;i_lop++)
{
pc_[0]=p_recvmsg[i_lop];
if(p_recvmsg[i_lop]==char(13))
{
p_list->AddString(pc_temp);
memset(pc_temp,0x00,120);
}
else
{
strcat(pc_temp,pc_);
}//进行这些操作后,
delete []pc_temp;//没问题
delete []p_recvmsg;//会出现内存泄露问题
请指点一二!!!!
...全文
293 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
taodm 2007-08-01
  • 打赏
  • 举报
回复
提示很清楚啊,你p_recvmsg要么它或者它前后的malloc出的内存发生内存越界,要么你的代码修改了p_recvmsg指针本身。
你可以在malloc后把指针值记下来,到free时再看看这个值被改了没有。
shangwei97 2007-07-31
  • 打赏
  • 举报
回复
出现问题了,我都改好了用malloc 分配空间
用free 释放空间,不过还时有问题
char * p_recvmsg;
p_recvmsg=( char*) malloc(500);
memset(p_recvmsg,0x00,500);
char *pc_temp= (char*) malloc (120);
memset(pc_temp,0x00,120);

long i_len = strlen(p_recvmsg);
char pc_[10];
memset(pc_,0x00,10);
for(long i_lop=0;i_lop<i_len;i_lop++)
{
pc_[0]=p_recvmsg[i_lop];
if(p_recvmsg[i_lop]==char(13))
{

p_list->AddString(pc_temp);
memset(pc_temp,0x00,120);
}
else
{
strcat(pc_temp,pc_);
}
free(pc_temp);

free(p_recvmsg);
程序能返回正确值不过运行出现错误
弹出一个对话框
User breakpoint called from code at 0x7c921230
继续运行

Debug Assertion Failed!


Expression:_CrtIsValidHeapPointer(pUserData)

For information on how your program can cause an assertion failure,
see the Visual C++ documentation on asserts.
希望大家帮忙
shangwei97 2007-07-31
  • 打赏
  • 举报
回复
有可能,谢谢
调试时候不能上网,下线调试了
taodm 2007-07-31
  • 打赏
  • 举报
回复
你p_recvmsg数组越界了吧,看看你的strlen(p_recvmsg);是不是已经大于50了。
shangwei97 2007-07-31
  • 打赏
  • 举报
回复
程序运行到 delete []p_recvmsg; 之前程序能返回正确的结果,只是在运行释放p_recvmsg时才会出现错误,把delete []p_recvmsg; 注释之后 程序正常运行
taodm 2007-07-31
  • 打赏
  • 举报
回复
是内存泄露还是程序崩溃啊?你不说是啥问题,咋帮你。
shangwei97 2007-07-31
  • 打赏
  • 举报
回复
多谢,指教,
不过我出的问题不是在这里,好象是动态分配过的字符串,在使用后,就是调用pc_[0]=p_recvmsg[i_lop];之后,释放出的问题
taodm 2007-07-31
  • 打赏
  • 举报
回复
vc++ free 不时关键字 不能用来释放空间
那malloc就是关键字了?就可以用来申请空间了?
你都看什么书学的编程啊,最基本知识都没掌握。
我啃 2007-07-31
  • 打赏
  • 举报
回复
free(pc_temp);
free是函数
void free(void *);
shangwei97 2007-07-31
  • 打赏
  • 举报
回复
谢谢大家,不过还是不对
free pc_temp; 出现
error C2146: syntax error : missing ';' before identifier 'pc_temp'
我啃 2007-07-31
  • 打赏
  • 举报
回复
free(pc_temp);
虫子也会……啊~
jixingzhong 2007-07-31
  • 打赏
  • 举报
回复
free pc_temp;
我啃 2007-07-31
  • 打赏
  • 举报
回复
靠,提供malloc会不提供free吗?自己试试不久知道啦
shangwei97 2007-07-31
  • 打赏
  • 举报
回复
vc++ free 不时关键字 不能用来释放空间
taodm 2007-07-31
  • 打赏
  • 举报
回复
一个new出来的,一个malloc出来的,怎么可以都用delete呢!!!
我啃 2007-07-31
  • 打赏
  • 举报
回复
靠用C malloc分配的干什么用delete啊,用free释放……

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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