【问题标题】:glibc detected free(): invalid next size (fast)glibc 检测到 free():下一个大小无效(快速)
【发布时间】:2012-02-22 19:04:23
【问题描述】:

此代码生成随机数,然后根据有关间隔的函数的输入生成直方图。 “bins”表示直方图区间,“bin_counts”保存给定区间内的随机数个数。

我已经查看了几篇处理类似问题的帖子,并且我知道我在某个地方的内存超出了范围,但 GBD 仅将我指向“free(bins);”在代码的末尾。我已经仔细检查了我的数组长度,我认为它们在不访问不存在的元素/写入未分配的内存方面都是正确的。奇怪的是代码按预期工作,它产生了一个准确的直方图,现在我只需要帮助清理这个 free() 无效的下一个大小错误。如果有人有任何建议,我将不胜感激。整个输出是:

检测到 glibc ./file: free(): invalid next size (fast): 0x8429008

后面是内存中的一堆地址,由 Backtrace 和 Memory Map 分隔。 Backtrace 仅将我指向第 129 行,即“free(bins);”。提前致谢

    #include "stdio.h"
    #include "string.h"
    #include "stdlib.h"

    void histo(int N, double m, double M, int nbins, int *bin_counts, double *bins);

     int main(int argc, char* argv[])
     {

     int *ptr_bin_counts;
     double *ptr_bins; 

     histo(5,0.0,11.0,4, ptr_bin_counts, ptr_bins);

     return 0;
     } 

     void histo(int N, double m, double M, int nbins, int *bin_counts, double *bins)
     {

     srand(time(NULL));
     int i,j,k,x,y;
     double interval;
     int randoms[N-1];
     int temp_M = (int)M;
     int temp_m = (int)m;
     interval = (M-m) /((double)nbins);


     //allocating mem to arrays
     bins =(double*)malloc(nbins * sizeof(double));
     bin_counts =(int*)malloc((nbins-1) * sizeof(int));

     //create bins from intervals
     for(j=0; j<=(nbins); j++)
     {
            bins[j] = m + (j*interval); 
     } 

      //generate "bin_counts[]" with all 0's
      for(y=0; y<=(nbins-1); y++)
       {
         bin_counts[y] = 0; 
       }


      //Generate "N" random numbers in "randoms[]" array
      for(k =0; k<=(N-1); k++)
      {
          randoms[k] = rand() % (temp_M + temp_m);
          printf("The random number is %d \n", randoms[k]);
      }

       //histogram code 
       for(i=0; i<=(N-1); i++)
        {
         for(x=0; x<=(nbins-1); x++)
         {
              if( (double)randoms[i]<=bins[x+1] && (double)randoms[i]>=bins[x] )
               {
                    bin_counts[x] = bin_counts[x] + 1; 
               }
         }
         }
         free(bins);
         free(bin_counts);
         }

【问题讨论】:

  • 不要将malloc()的结果转换成C。
  • 我没有发现任何问题,并且这段代码对我来说很好用。你发送了整个函数吗? (顺便问一下,你为什么要把指针传递给函数?)
  • 我采纳了您的建议,但不幸遇到了同样的问题。
  • 原谅我的笨拙,但是发送整个函数是什么意思?而且我对指针仍然不是 100% 有信心,所以如果您对如何在此代码中改进它们有任何建议,请详细说明。
  • 我的意思是,您是否尝试为我们简化代码?如果是,发送读取代码。我的问题是,当你不使用最后两个参数时,为什么要将它们传递给函数? (您在函数开头更改该变量)

标签: c linux free glibc


【解决方案1】:
bins =(double*)malloc(nbins * sizeof(double));
bin_counts =(int*)malloc((nbins-1) * sizeof(int));

//create bins from intervals
for(j=0; j<=(nbins); j++)
{
    bins[j] = m + (j*interval); 
} 

//generate "bin_counts[]" with all 0's
for(y=0; y<=(nbins-1); y++)
{
    bin_counts[y] = 0; 
}

您超出了您的数组,您为 nbins 双打分配了位置,但写入nbins+1 位置,并为bin_counts 使用nbins 位置但只分配了nbins-1

【讨论】:

  • 你可以用valgrind这样的分析工具来检测这些东西。
  • 感谢您的信息,我一定会调查 valgrind。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-12
  • 2011-04-12
  • 1970-01-01
  • 2011-06-26
  • 2017-03-28
  • 1970-01-01
相关资源
最近更新 更多