在C中重新分配无效的指针

编程入门 行业动态 更新时间:2024-10-27 11:28:28
本文介绍了在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中重新分配无效的指针

本文发布于:2023-07-29 08:40:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239162.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   分配

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!