重新分配时出现段错误
所以我在程序中使用 malloc,然后在程序内的方法中重新分配。在我多次调用此方法后,我会收到“分段错误(核心已转储)”。
经过进一步检查,我意识到,由于某种原因,当我的指针从 0x25d7d60 或 0x223fae0 (或由 7 位数字 (0xHHHHHHH) 表示的任何地址)转到 0x7f47d370a010 (超过 7 位数字)时,例如,从 realloc 调用中抛出段错误,realloc 甚至不会返回 NULL。我通过简单地使用 malloc 然后使用 memcpy 来解决这个问题。然而,我对为什么会发生这种情况感到非常困惑,并且想看看 stackoverflow 的用户是否可以解释为什么会发生这种情况。
谢谢
这是相关代码:
unsigned int* myArray;
unsigned int num_ints;
int main()
{
num_ints = 100;
if((myArray =(unsigned int*) malloc(sizeof(unsigned int)*(num_ints)*3))==NULL)
{
std::cout << "Malloc failed!" << std::endl;
return false;
}
.
.
.
//This called when n key is pressed (code left out)
methodName();
return true;
}
void methodName()
{
if((myArray =(unsigned int*) realloc(myArray,sizeof(unsigned int)*(num_ints*4)*3))==NULL)
{
std::cout << "Realloc failed!" << std::endl;
exit(0);
}
}
So I was using malloc in my program and then realloc within a method inside the program. After I called this method so many times I would get a "Segmentation fault (core dumped)".
Upon further inspection I realized that for some reason when my pointer goes from 0x25d7d60 or 0x223fae0 (or any address represented by 7 digits (0xHHHHHHH) ) to 0x7f47d370a010 (with more than 7 digits) for example, a segfault is thrown from within the realloc call, realloc wont even return NULL. I fixed this by simply using malloc and then memcpy instead. However I am very confused at why this happened and wanted to see if any of the users of stackoverflow could shed some light on why this happened.
Thanks
Here is the relevant code:
unsigned int* myArray;
unsigned int num_ints;
int main()
{
num_ints = 100;
if((myArray =(unsigned int*) malloc(sizeof(unsigned int)*(num_ints)*3))==NULL)
{
std::cout << "Malloc failed!" << std::endl;
return false;
}
.
.
.
//This called when n key is pressed (code left out)
methodName();
return true;
}
void methodName()
{
if((myArray =(unsigned int*) realloc(myArray,sizeof(unsigned int)*(num_ints*4)*3))==NULL)
{
std::cout << "Realloc failed!" << std::endl;
exit(0);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有一个很好的机会,通过调用“程序内方法内的 realloc”,您实际上将其加载到一个局部变量中,然后该局部变量被丢弃,并且您的程序继续使用旧指针(现已被释放)。
我们需要查看代码才能确定,但根据我的经验,这是分配错误的主要原因之一。
根据您所展示的内容,您所做的事情没有任何问题。它实际上与此代码相同:
它运行完美,进行了一千万次重新分配。
因此,问题超出了您向我们展示的范围,可能是编译器错误(如果您使用主流编译器,则不太可能)或代码中其他地方的内存损坏。
There's a good chance that, by calling "realloc within a method inside the program", you're actually loading it into a local variable which is then thrown away and your program continues to use the older pointer (which has now been freed).
We would need to see the code to be certain but, in my experience, that's the one of the major causes of allocation errors.
Based on what you've shown, there's nothing wrong with what you're doing. It's effectively the same as this code:
which works perfectly, doing ten million reallocations.
So the problems lies outside what you've shown us, perhaps a compiler bug (pretty unlikely if you're using a mainstream compiler) or memory corruption from elsewhere in your code.
如果
realloc
改变了数组的地址,那么函数作用域(本地)中的myarr
会得到新值,它不会改变myarr
>main
中的变量If
realloc
changes the address of the array, then themyarr
in the function scope (local) gets the new value, it does not change themyarr
variable in themain
您没有在主作用域中更改 myArr,realloc 可能会返回新地址,旧地址无效。
You didn't change myArr in the main scope, realloc maybe return a new address, the old address is invalid.
问题是您的
methodName
函数正在将新指针分配给 myArray 的本地副本。快速解决方法是使 myArray 成为指向指针的指针,如下所示:
然后通过传递 myArr 的地址来调用它:
这样 methodName 即可获取
main()< 的内存地址/code>s myArr 以便可以写入其中。
正如您所看到的,使用输出值的函数参数可能会有点麻烦,因此我建议
返回
新地址:然后只需覆盖
myArr
主要
:The problem is that your
methodName
function is assigning the new pointer to it's local copy of myArray.The quick fix is to make myArray a pointer to a pointer like so:
and then call it by passing the address of myArr:
That way methodName gets the address of the memory of
main()
s myArr so that it can write into it.As you can see though, having function parameters that output values can get a little hairy so I suggest instead
return
ing the new address:Then it's just a matter of overwriting
myArr
inmain
: