为大小为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;
}
`
答案 0 :(得分:1)
free(): invalid next size
是当您破坏malloc
所使用的内存区域(例如,超出分配的块末尾写入内容,破坏该存储区使用的在线记帐信息)时经常收到的错误消息。内存分配功能。
考虑到问题中实际 code 的稀缺性(或者,在更新后,您似乎没有精简到更精确地查明大量的 volume 的代码)问题),这是我所能提供的尽可能多的细节。我的建议是检查您的代码中是否存在未正确使用分配的内存的区域。