带有重新分配的段错误 [英] Segfault with realloc

查看:98
本文介绍了带有重新分配的段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在程序中使用了malloc,然后在程序内部的方法中重新分配了内存.在我多次调用此方法后,我会收到分段错误(核心已转储)".

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)".

在进一步检查时,我意识到由于某种原因,当我的指针从0x25d7d60或0x223fae0(或任何由7位数字(0xHHHHHHH)表示的地址)变为0x7f47d370a010(具有7位以上的数字)时,就会从内部抛出段错误在realloc调用中,realloc甚至不会返回NULL.我通过简单地使用malloc然后使用memcpy来解决此问题.但是,我对为什么会发生这种情况感到非常困惑,并想看看是否有任何stackoverflow用户可以弄清这种情况的发生原因.

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.

谢谢

以下是相关代码:

 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);
 }

 }

推荐答案

通过调用程序内部方法中的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:

#include <iostream>
#include <cstdlib>
int sz = 1000;
int *buffer = 0;
static int methodName (void) {
    if (sz == 100000)
        sz = 100;
    sz = sz * 10;
    if ((buffer = (int*)realloc (buffer, sz)) == 0) {
        std::cout << "Realloc error" << std::endl;
        return 1;
    }
    return 0;
}
int main(void) {
    int i;
    if ((buffer = (int*)malloc (sz)) == 0) {
        std::cout << "Alloc error" << std::endl;
        return 1;
    }
    for (i = 0; i < 10000000; i++)
        if (methodName() != 0)
            return 1;
    std::cout << "All okay" << std::endl;
    return 0;
}

效果很好,可以进行一千万次重新分配.

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.

这篇关于带有重新分配的段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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