Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
持续获取错误:free():读取文件时在tcache2中检测到双重空闲_C_Sorting_Filestream - Fatal编程技术网

持续获取错误:free():读取文件时在tcache2中检测到双重空闲

持续获取错误:free():读取文件时在tcache2中检测到双重空闲,c,sorting,filestream,C,Sorting,Filestream,我正在用C语言编写一个程序,它将读取一个带有数字(假定很长)的文档,并将它们放入一个动态数组中,然后对数组进行排序。 我一直在获取错误free():在tcache2中检测到双重空闲,我不确定这是因为推送功能还是因为我实现获取文档中每一行的方式。 感谢您的帮助 #include <stdio.h> #include <stdlib.h> #include <errno.h> void push(long *arr,long value, int *size){

我正在用C语言编写一个程序,它将读取一个带有数字(假定很长)的文档,并将它们放入一个动态数组中,然后对数组进行排序。 我一直在获取错误free():在tcache2中检测到双重空闲,我不确定这是因为推送功能还是因为我实现获取文档中每一行的方式。 感谢您的帮助

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
void push(long *arr,long value, int *size){
    //printf("%ld",value);
    arr = realloc(arr,8 + (8 * (*size + 1))); //allocate 4 more bytes to the arr
    arr[*size] = value; //add the value to the end of array
    *size = *size + 1; //increase size of array
}
int cmpfunc (const void * a, const void * b) {
       return ( *(long*)a - *(long*)b );
}
int main(int argc,char** argv){
    if(argc != 2){
        fprintf(stderr,"Error too many arguments");
        exit(1);
    }
    FILE *fp = fopen(argv[1],"r+");
    if(fp == NULL){
        fprintf(stderr,"Error file not found, Error Number: %d\n",errno);
        exit(1);
    }

    int size = 0;

    long* arr = (long*) malloc(8);  //allocate some mememory to be able to store data points from the file
    char str[256]; //create a string capable of storing each singluar line, 10 because the max amount of                
    while(fgets(str,sizeof(str),fp)){
        printf("%s",str);
        push(arr,atol(str),&size);
    }
    qsort(arr,size,8,cmpfunc);
    fclose(fp);
    return 0;
}
#包括
#包括
#包括
无效推送(长*arr、长值、整数*大小){
//printf(“%ld”,值);
arr=realloc(arr,8+(8*(*size+1));//为arr再分配4个字节
arr[*size]=value;//将值添加到数组的末尾
*size=*size+1;//增加数组的大小
}
int cmpfunc(常数无效*a,常数无效*b){
返回(*(长*)a-*(长*)b);
}
int main(int argc,字符**argv){
如果(argc!=2){
fprintf(stderr,“错误参数太多”);
出口(1);
}
文件*fp=fopen(argv[1],“r+”);
如果(fp==NULL){
fprintf(stderr,“找不到错误文件,错误号:%d\n”,错误号);
出口(1);
}
int size=0;
long*arr=(long*)malloc(8);//分配一些mememory来存储文件中的数据点
char str[256];//创建一个能够存储每一行的字符串,10因为
while(fgets(str,sizeof(str),fp)){
printf(“%s”,str);
推送(arr、atol(str)和尺寸);
}
qsort(arr,尺寸,8,cmpfunc);
fclose(fp);
返回0;
}
此功能

void push(long *arr,long value, int *size){
    //printf("%ld",value);
    arr = realloc(arr,8 + (8 * (*size + 1))); //allocate 4 more bytes to the arr
    arr[*size] = value; //add the value to the end of array
    *size = *size + 1; //increase size of array
}
不更改原始指针arr。它处理指针的副本。因此,每次在函数中调用realloc都会尝试释放相同范围的已释放内存

必须通过引用传递指针

void push(long **arr,long value, int *size){
    //printf("%ld",value);
    *arr = realloc(*arr,8 + (8 * (*size + 1))); //allocate 4 more bytes to the arr
    ( *arr )[*size] = value; //add the value to the end of array
    *size = *size + 1; //increase size of array
}
就这样说吧

push( &arr,atol(str),&size);
请注意,这样调用函数realloc

    *arr = realloc(*arr,8 + (8 * (*size + 1))); //allocate 4 more bytes to 
这是不安全的。该函数可以返回NULL。在这种情况下,早期分配内存的地址将丢失


你应该使用一些中间变量。如果分配成功,请将中间变量的值分配给指针
*arr

请不要使用。在您的分配中,数字
8
代表什么?
arr=realloc()
。这会改变局部变量。调用方的变量未更改。返回指针并让调用者分配回原始变量,或者传入一个指向函数的双指针。这是否回答了您的问题<代码>“找不到错误文件,错误号:%d\n”远不如简单的
“错误:%s%s”、argv[1]、strerror(errno)
,只需编写
peror(argv[1])