我有以下代码,我需要在使用它之后释放d_array结构的数组,并且我总是得到SiGABRT。
typedef struct d_array
{
int len;
double * content;
} d_array;
quantile(d_array arr, d_array percentages, d_array borders )
{
int i;
d_array temp_arr;
temp_arr.content =(double *) malloc(sizeof(double)*arr.len);
memcpy(temp_arr.content, arr.content, sizeof(double)*arr.len);
qsort(temp_arr.content, arr.len, sizeof(double), d_compare);
double med = arr.content[(int)(arr.len/2)];
for(i=0; i< percentages.len; i++)
{
borders.content[i] = temp_arr.content[(int)(100*(percentages.content[i]))];
}
free(temp_arr.content);
}
答案 0 :(得分:0)
基于标记&#34; Segmentation-fault&#34;,我相信您正面临程序崩溃的信号&#34; segmentation-fault&#34;。您可能需要检查以下代码中的percentages.content [i]的值。 100 *该值可能超出为temp_arr.content分配的值。
borders.content [i] = temp_arr.content [(INT)(100 *(pecentages.content [I]))];
就内存泄漏而言,我在此功能中找不到任何内容。
答案 1 :(得分:0)
这是我的功能已完成,但免费现在正在使用,因为我在temp_arr声明之前添加了 struct : 这可能对其他人有帮助:
void quantile(d_array arr, d_array percentages, d_array borders )
{
int i;
struct d_array temp_arr;
temp_arr.content = (double *)malloc(sizeof(double)*(arr.len+2));
memcpy(temp_arr.content+1, arr.content, sizeof(double)*arr.len);
t_d(temp_arr.content, 25);
qsort(temp_arr.content+1, arr.len, sizeof(double), d_compare);
t_d(temp_arr.content, 25);
temp_arr.content[0] = temp_arr.content[1];
temp_arr.content[arr.len+1] = temp_arr.content[arr.len];
t_d(temp_arr.content, 25);
d_array q;
q.len = arr.len+2;
q.content = malloc(sizeof(double)*(arr.len+2));
q.content[0] = 0;
q.content[1] = 100*0.5/arr.len;
q.content[arr.len+1] = 100;
for(i=2; i<arr.len+1 ;i++)
q.content[i] = (q.content[i-1] + (double)100/arr.len);
int indx=0;
double elem=0;
for(i=0; i< percentages.len; i++)
{
find(q, percentages.content[i]*100, '>', &indx, &elem);
if (temp_arr.content[indx] == temp_arr.content[indx-1] )
borders.content[i] = temp_arr.content[indx-1];
else
borders.content[i] = temp_arr.content[indx-1] + (percentages.content[i]*100 - q.content[indx-1])*(temp_arr.content[indx]-temp_arr.content[indx-1])/(q.content[indx]-q.content[indx-1]) ;
}
free(temp_arr.content);
}
find function:
int find(d_array mat, double el, char op, int *indx, double * eee)
{
int i;
if (op=='>')
for(i=0;i<mat.len ;i++)
if (mat.content[i]>el)
{
*indx = i;
*eee= mat.content[i];
return 0;
}
else if(op=='=')
for(i=0;i<mat.len ;i++)
if (mat.content[i] == el)
{
*indx = i;
*eee = mat.content[i];
return 0;
}
else if(op=='<')
for(i=0;i<mat.len ;i++)
if (mat.content[i] < el)
{
*indx = i;
*eee = mat.content[i];
return 0;
}
return 1;
}
和: struct d_array {double *内容 int len } d_array;
电话是: double test_sig [] = {1.0,0.0,2.0,0.0,1.0,0.0,1.0,0.0,1.0,0.0,15.0,0.0,51.0,20.0,510.0,2.0,0.0,151.0,20.0,51.0,0.0,0.0.0, 05.0}; struct d_array pecentages; pecentages.content = malloc的(的sizeof(双)* 5); pecentages.len = 5;
struct d_array borders;
borders.content=malloc(sizeof(double)*5);
borders.len=5;
mat.content = test_sig;
pecentages.content[0] = 0.1;
pecentages.content[1] = 0.2;
pecentages.content[2] = 0.5;
pecentages.content[3] = 0.8;
pecentages.content[4] = 0.9;
quantile( mat, pecentages, borders );