我在創建這個C程序時得到了一個STATUS_ACCESS_VIOLATION,我相信這是因爲一次初始化結構,然後使用malloc()來增加它們的大小,從而導致已經使用的引用內存指針。這是此異常發生功能:由於malloc()導致的STATUS_ACCESS_VIOLATION?
void process_track(char filename[], struct track *tracks) {
int i = 0;
FILE* fp;
int lines;
lines = count_file_lines(filename);
tracks = malloc(lines);
fp = fopen(filename, "r");
if (fp == NULL) {
printf("\n\"%s\" File not found", filename);
exit(1);
} else {
for (i = 0; i < lines; i++) {
fscanf(fp, "%d %d %d %d\n", &tracks[i].number, &tracks[i].startnode, &tracks[i].endnode, &tracks[i].minutes);
}
for (i = 0; i < lines; i++) {
printf("%d ", tracks[i].number);
printf("%d ", tracks[i].startnode);
printf("%d ", tracks[i].endnode);
printf("%d\n", tracks[i].minutes);
}
}
}
具體地,它發生在下面的行:
fscanf(fp, "%d %d %d %d\n", &tracks[i].number, &tracks[i].startnode, &tracks[i].endnode, &tracks[i].minutes);
該函數的調用在下面的函數(情況3):
void get_file(char textfilename[], int optnumber) {
struct node *nodes;
struct track *tracks;
struct course *courses;
struct entrant *entrants;
struct checkpointdata *checkpointdatas;
char filename[20]; //Array of char for filename
printf("Enter the name of the %s text file(with file extension) :", textfilename);
scanf(" %[a-zA-Z._1-9]", &filename);
switch (optnumber) {
case 1:
process_name(filename);
break;
case 2:
process_node(filename, nodes);
break;
case 3:
process_track(filename, tracks);
break;
case 4:
process_course(filename, courses);
break;
case 5:
process_entrant(filename, entrants);
break;
case 6:
process_checkpointdata(filename, checkpointdatas);
break;
}
}
它讀取的第一行很好,但在此之後失敗。由於其他「process_ [data]」非常相似,我也嘗試了這些。有些可以正常工作,有些會想出異常,有些會在輸出中檢索垃圾。
我將如何解決這個問題,或者分配內存,以便它不會與預先分配的內存衝突(如果這確實是問題)?
您忘記關閉您打開的文件。它與你的崩潰無關,但你現在應該學習良好的習慣。 –