关于C#:free():下一个尺寸无效(正常)

free(): invalid next size(normal)

使用输入的.txt文件运行此代码时,输??入的.txt文件包含200-300之间的整数(用空格分隔),在使用fprintf语句进行for循环之前,我得到了一个错误。

我不确定qsort是否会引起此错误,或者为什么会发生此错误,但是不胜感激。

(此文件通过在命令行中添加输入文件和输出文件的名称来运行,例如:./program input.txt output.txt

我的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int cmpfunc (const void * a, const void * b)
{
   return ( *(int*)a - *(int*)b );
}

int main(int argc, char *argv[]){

    if(argc != 3){
        printf("\
Invalid input\
Please provide the input and output text file names as %s name1 name2\
"
, argv[0]);
    }else{

    printf("\
Part A: \
"
);
    printf("..............................................................................................................\
\
"
);

    char *fn1 = argv[1];  //variables
    char *fn2 = argv[2];

    int temp = 0;
    int counter = 0;
    int index = 0;
    int index2 = 0;
    int sort = 0;


    FILE *fp1 = fopen(fn1,"r"); //read file
    FILE  *fp2 = fopen(fn2,"w"); //write file

    if(fp1 == NULL){  //test if fp1 was opened
        printf("There was an error opening the input file");
    }

     char data[10]; //ints can only hold 10 digits
     int *integerArr;
     int *tempPointer;

    integerArr = malloc(10*sizeof(int));

     int sizeOfArrs = 10;

    printf("Reading in the textfile:");

     while(fscanf(fp1,"%s",data) != EOF){  //reads in the file breaking on each whitespace and ends at the EOF pointer

  temp = strlen(data);
  if(temp <=10){
    temp = atoi(data);
    integerArr[counter] = temp;

    printf(".");

    counter++;
    if(counter == sizeOfArrs -1){

        temp = sizeOfArrs * 2;

        tempPointer = realloc(integerArr, temp);

        if(tempPointer != NULL){
            integerArr = tempPointer;
        }

    }
  }else printf("\
integer had too many digits\
"
);


    }


     printf(" Done\
%d Numbers were found\
"
,  counter);
     printf("The integers found in the %s file: \
"
, argv[1]);

        index = 0;  //reset index to 0;
    for(index;index<counter;index++){  //prints the unsorted contents of the file
        printf("%d", integerArr[index]);
    }

    printf("\
\
Part B\
"
);
    printf("..............................................................................................................\
\
"
);


    printf("The integers found in the %s file after sorting: \
"
, argv[1]);

     qsort(integerArr, counter, sizeof(int), cmpfunc); //best function ever (sorts the array using the cmpfunc to tell if an integer is greater than less than or equal to the next one)

    index = 0; //resets the index
    for(index; index <counter; index++){ //prints the sorted contents of the file
        printf("%d", integerArr[index]);

        fprintf(fp2,"%d",integerArr[index]); //writes the sorted integers to the new file
    }

    if(fp2 == NULL){ //tests if the write worked
        printf("There was an error writing the outputfile");

    }

    printf("\
"
);
    close(fp1,fp2); //closes both files
    }
return 0;
}


您的fscanf循环中断。您实际上并没有以更大的大小进行重新分配。这是更正后的程序[抱歉,您需要修改书呆子风格,但您碰到了我的其中一件:侧边栏长评论]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int
cmpfunc(const void *a, const void *b)
{
    return (*(int *) a - *(int *) b);
}

int
main(int argc, char *argv[])
{

    if (argc != 3) {
        printf("\
Invalid input\
Please provide the input and output text file names as %s name1 name2\
"
, argv[0]);
        return 1;
    }

    printf("\
Part A: \
"
);
    printf("..............................................................................................................\
\
"
);

    char *fn1 = argv[1];            // variables
    char *fn2 = argv[2];

    int temp = 0;
    int counter = 0;
    int index = 0;
    int index2 = 0;
    int sort = 0;

    FILE *fp1 = fopen(fn1,"r");
    FILE *fp2 = fopen(fn2,"w");

    // test if fp1 was opened
    if (fp1 == NULL) {
        printf("There was an error opening the input file");
        return 1;
    }

    // ints can only hold 10 digits
    char data[10];
    int *integerArr;
    int *tempPointer;

    int sizeOfArrs = 10;
    integerArr = malloc(sizeOfArrs * sizeof(int));

    printf("Reading in the textfile:");

    // reads in the file breaking on each whitespace and ends at the EOF
    // pointer
    while (fscanf(fp1,"%s", data) != EOF) {
        temp = strlen(data);
        if (temp > 10) {
            printf("\
integer had too many digits\
"
);
            continue;
        }

        temp = atoi(data);
        integerArr[counter] = temp;

        printf(".");

        counter++;
        if (counter == sizeOfArrs - 1) {
            sizeOfArrs += 600;
            integerArr = realloc(integerArr, sizeOfArrs * sizeof(int));
        }
    }

    // trim array to actual size needed
    sizeOfArrs = counter;
    integerArr = realloc(integerArr, sizeOfArrs * sizeof(int));

    printf(" Done\
%d Numbers were found\
"
, counter);
    printf("The integers found in the %s file: \
"
, argv[1]);

    // prints the unsorted contents of the file
    for (index = 0; index < counter; index++) {
        printf("%d", integerArr[index]);
    }

    printf("\
\
Part B\
"
);
    printf("..............................................................................................................\
\
"
);

    printf("The integers found in the %s file after sorting: \
"
, argv[1]);

    // best function ever (sorts the array using the cmpfunc to tell if an
    // integer is greater than less than or equal to the next one)
    qsort(integerArr, counter, sizeof(int), cmpfunc);

    // prints the sorted contents of the file
    for (index = 0; index < counter; index++) {
        printf("%d", integerArr[index]);

        // writes the sorted integers to the new file
        fprintf(fp2,"%d", integerArr[index]);
    }

    // tests if the write worked
    if (fp2 == NULL) {
        printf("There was an error writing the outputfile");

    }

    printf("\
"
);

    // closes both files
    fclose(fp1);
    fclose(fp2);

    return 0;
}

此外,请注意底部的关闭符。还有一些小错误供您查找。