glibc 检测到 free():下一个大小无效(快)
该代码生成随机数,然后根据有关间隔的函数的输入生成直方图。 “bins”表示直方图间隔,“bin_counts”保存给定间隔内的随机数数量。
我已经回顾了几篇处理类似问题的帖子,我知道我在内存中的某个地方超出了范围,但 GBD 只将我指向“free(bins);”在代码的末尾。我已经仔细检查了我的数组长度,我认为它们在不访问不存在的元素/写入未分配的内存方面都是正确的。奇怪的是,代码按预期工作,它生成了准确的直方图,现在我只需要帮助清理这个 free() 无效的下一个大小错误。如果有人有任何建议,我将非常感激。整个输出是:
检测到 glibc ./file: free(): invalid next size (fast): 0x8429008
后跟内存中的一堆地址,由回溯和内存映射分隔。 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);
}
This code generates random numbers and then produces a histogram based on input to the functions regarding the intervals. "bins" represents the histogram intervals and "bin_counts" holds the number of random numbers in a given interval.
I've reviewed several of the posts dealing with similiar issues and I understand that I'm out of bounds in the memory somewhere but GBD only points me to the "free(bins);" at the end of the code. I've double-checked my array lengths and I think they are all correct in terms of not accessing elements that don't exist/writing to memory not allocated. The weird thing is that the code works as intended, it produces an accuarate histogram, now I just need helping cleaning up this free() invalid next size error. If anybody has any suggestions I would be much obliged. The whole output is :
glibc detected ./file: free(): invalid next size (fast): 0x8429008
followed by a bunch of addresses in memory, seperated by Backtrace and Memory Map.
The Backtrace only points me towards line 129, which is "free(bins);". Thanks in advance
#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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您超越了数组,为
nbins
双精度分配位置,但写入nbins+1
位置,并使用nbins
位置作为bin_counts
但只分配了nbins-1
。You are overstepping your arrays, you allocate place for
nbins
doubles but write tonbins+1
locations, and usenbins
locations forbin_counts
but have only allocatednbins-1
.