腾出的malloc失败 [英] free up on malloc fails

查看:104
本文介绍了腾出的malloc失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些这样的事

char *temp, xyz;
temp = (char *)malloc(sizeof(somestring));

xyz = (char *) malloc(sizeof(temp));
xyz= strrchar(temp, "_"); // temp does not contain "_", but need to check for error validation

现在如果我尝试做
    免费(TEMP);
它的崩溃;

Now If I try to do free(temp); its crashing;

任何想法,我是一个beginer。

any idea, I am a beginer.

推荐答案

我想通了,纠正我,如果我错了,
XYZ只是指着它具有_字符串中的临时位置。当我腾出临时,该位置得到了释放。但XYZ仍然指向该位置,这已经释放。这是我对释放XYZ崩溃的原因。

I figured it out, correct me if I am wrong, xyz is just pointing to the location where it has the "_" in the string temp. As soon as I free up the temp, that location got freed up. but xyz is still pointing to that location, which is already freed. That is the reason I had crash on freeing xyz.

这篇关于腾出的malloc失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆