在C中重新分配无效的指针 [英] Realloc Invalid Pointer in C

查看:103
本文介绍了在C中重新分配无效的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一项家庭作业,因此我不想发布任何代码,但是我对自己的错误感到很困惑.

This is a homework assignment so I don't want to post any code, but I'm pretty stumped with the bug that I have.

目前,我已经分配了一个数组,并且正在将指针复制到该数组.现在,我可以对该数组进行memcpy和memmove,并且工作正常.

Currently I have a array that has been malloced and am copying the pointer to the array. Now, I can memcpy and memmove with this array and that works fine.

但是,当我使用它进行重新分配时,出现了无效的指针错误-我完全不知道为什么.

However, when I do a realloc with it an invalid pointer error comes up - and I have absolutely no idea why.

任何人都可以帮忙吗?

Can anyone help?

推荐答案

您说过我将指向此数组的指针复制到另一个变量中".问题是一旦执行重新分配,原始指针将不再有效.我看不到将指针复制到变量的原因吗?

you said 'I copy the pointer to this array into another variable'. The problem is as soon as you do a realloc, the original pointer is no longer valid. I dont see the reason to copy the pointer to a variable?

这篇关于在C中重新分配无效的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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