gpt4 book ai didi

c - 自由(): invalid next size (normal) with qsort()

转载 作者:行者123 更新时间:2023-11-30 19:03:10 27 4
gpt4 key购买 nike

当对大小为 210*8 的种子数据集运行此代码时,我在预测函数中的 qsort() 行之后收到错误。它不在 qsort() 之后执行。

我不确定 qsort 是否导致了此错误或为什么会发生此错误,但如果有任何见解,我们将不胜感激。

执行 qsort 语句时出现错误。

我的代码:

`include stdio.h
include stdlib.h
include string.h
include math.h

typedef struct point{
int class;
float coords[7], dist;
}point;

int argmax(int arr[], int n){
int marg = -1, mxf = -1, i;
for (i =0; i<= n; i++){
if (arr[i] > mxf){
mxf = arr[i];
marg = i;
}
}
return marg;
}

float get_accuracy(int pred[], int act[], int n){
float cor = 0;
int i;
for (i = 0; i< n; i++){
if (pred[i] == act[i]) cor +=1;
}
return (cor*100.0)/n ;
}

float get_avg(float arr[], int n){
float sum = 0;
int i;
for (i = 0; i<n; i++){
sum += arr[i];
}
return sum/n;
}

point *shuffle(point *dataset, int rows, int features, int groups, int classes)
{
int i,j,k,l,m = 0;
point *shuffled_dataset;
shuffled_dataset = (point *)malloc(sizeof(point)*rows);
for(i=0; i<rows/classes; i++,m++)
{
for(j=0; j<rows/(groups*classes); j++)
{
for(k=0; k<classes; k++,m++)
{
shuffled_dataset[m] = dataset[rows/classes*k + i];
/*for(l=0; l<features; l++)
{
shuffled_dataset[m].coords[l] = dataset[rows/classes*k + i].coords[l];
}*/
}
i++;
}
i--;
}
return shuffled_dataset;
}


float minkowski_dist(float* x, float* y, int len, int p){
int i;
float sum=0;
for(i=0;i < len; i++){
sum += pow(fabs(x[i] - y[i]),p);
}
return pow(sum,1/p);
}

int comparison(const void *a, const void *b) {
point *ia = (point *)a;
point *ib = (point *)b;
return (int)(100.f*ia->dist - 100.f*ib->dist);
}

int predict(point test_point, point train[], int n, int k, int p, int classes, int features){
int i;
printf("Hi\n");
for (i = 0; i < n; i++)
{
train[i].dist = minkowski_dist(test_point.coords, train[i].coords, features, p);
printf("%d.\t", i+1);
print_point(train[i]);
}
qsort (train, n-1, sizeof(train[0]), comparison);
int freq[classes+1];
for (i = 0; i < classes+1; ++i)
freq[i] = 0;
for (i = 0; i < k; i++)
freq[train[i].class]++;
return argmax(freq,classes);
}

float rFoldKNN(point *arr, int num, int r, int k, int p, int classes, int features){
int gsize = num/r;
int i, j, h;
int pred[gsize], act[gsize];
point cval[gsize], train[num - gsize];
float acc[r];
for(i=0; i<r; i++)
{
int cind = 0, tind = 0;
for(j=0; j<gsize; j++)
{
cval[cind++] = arr[gsize*i+j];
for(k=0; k<r; k++)
{
if(k!=i)
{
train[tind++] = arr[gsize*k+j];
}
}
}
for(j=0; j<gsize; j++)
{
printf("%d\t%d\n", tind, cind);
pred[j] = predict(cval[j], train, num-gsize, k, p, classes, features);
act[j] = cval[j].class;
}
acc[i] = get_accuracy(pred, act, gsize);
}
return get_avg(acc,r);
}

int main()
{
FILE *fp;
int r = 10, p = 5, k = 10;
int rows = 210;
int columns = 8;
int classes = 3;
int size = rows * columns; /*Assumed size of the dataset*/
float *data; /*Creating an array of assumed size as 1d(split after every 8 values to get the next row)*/
int count = 0;
int i, j;
float accuracies [k][p], maxac = -1.0;;
int maxk, maxp;
float c;
point *all;
all = (point *)malloc(sizeof(point)*rows);
data = (float*)malloc(sizeof(float*)*size);
if ((fp = fopen("seeds_dataset.txt", "r")) == NULL)
{
printf("Error reading file!");
exit(1);
}
for(i = 0; i < rows; i++){
for (j = 0 ; j < columns; j++){
fscanf(fp,"%f",&c);
if (j == columns-1)
all[i].class = c;
else
all[i].coords[j] = c;
}
}
fclose(fp);
for(i=0; i<rows; i++)
{
printf("%d.\t", i+1);
print_point(all[i]);
}
all = shuffle(all, rows, columns-1, 10, classes);
printf("Hi\n");
for(i=0; i<rows; i++)
{
printf("%d.\t", i+1);
print_point(all[i]);
}
for (i = 1; i <= k; ++i){
for (j = 1; j <= p; ++j){
accuracies[i][j] = rFoldKNN(all, rows, r, i, j, classes, columns-1);
if (accuracies[i][j] > maxac){
maxac = accuracies[i][j];
maxk = i;
maxp = j;
}
}
}
printf("best validation accuracy %f best k %d best p %d ",maxac, maxk, maxp );
return 0;
}

`

最佳答案

free(): invalid next size 是当您损坏了 malloc 使用的内存区域时经常收到的错误消息,例如写入超出了内存的末尾分配的 block ,破坏内存分配函数使用的内联记帐信息。

考虑到您的问题中实际代码的稀缺(或者,在您更新之后,您似乎没有缩小到更准确地定位的巨大代码量问题),这就是我能提供的尽可能多的细节。我的建议是检查您的代码是否有未正确使用分配内存的区域。

关于c - 自由(): invalid next size (normal) with qsort(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54453389/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com