【发布时间】:2014-03-02 08:11:57
【问题描述】:
我的任务是读取一个 test.m 文件,对其进行解析,并将相关信息放入一个类型语句(我创建的)数组中。我已经完成了大部分任务。唯一错误的是我在输出的最后得到了分段错误 11。认为我正在做的输出部分可能有问题,所以我将其注释掉以查看。它仍然给了我一个段错误。无论我注释什么,它总是在程序的最后给我一个段错误。我很困惑是什么原因造成的。我该怎么办?既然其他一切都在工作,我应该忽略它吗?
这里是代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LC_SIZE 16
#define FREAD_SIZE 1024
#define STATES_SIZE 1024
FILE *ifp;
int linetotal;
int locals[LC_SIZE];
int localspot = 0; // localspot = # of locals
int EP;
typedef char STRING[256];
typedef struct {
STRING label;
STRING opcode;
STRING arg;
} statement;
int main(int argc, char *argv[]) {
//statement states[STATES_SIZE] = {"(NULL)"};
char str[256];
const char newline[3] = "\n";
const char space[2] = " ";
char *token;
unsigned long length;
ifp = (argc > 1) ? fopen(argv[1], "r") : NULL;
if (ifp == NULL) {
printf("Input file not available\n");
return -1; }
fread(str, FREAD_SIZE, 1, ifp);
token = strtok(str, space);
statement states[STATES_SIZE];
for (int i = 0; i < STATES_SIZE; i++) {
strcpy(states[i].label, "(null)");
strcpy(states[i].opcode, "(null)");
strcpy(states[i].arg, "(null)"); }
while (token != NULL) {
length = strlen(token);
if (length > 1 ) { strcpy(str, &token[length-1]); }
// comment check
if ((strncmp(token, "/", 1) == 0) || (strcmp(str, "/") == 0)) {
token = strtok(NULL, newline);
//printf("Comment :: %s\n", token);
token = strtok(NULL, space);
}
// local check
else if (strcmp(token, "LC:") == 0) {
token = strtok(NULL, space);
//printf("==LC here==\n");
locals[localspot] = atoi(token);
localspot++;
//printf("Local variable %i = %s\n", localspot, token);
token = strtok(NULL, space);
}
// starting point check
else if (strcmp(token, "EP:") == 0) {
token = strtok(NULL, space);
EP = atoi(token);
//printf("\nEP: %i\n", EP);
}
// label check
else if (strcmp(str, ":") == 0) {
//printf("--Label found :: %s\n", token);
strcpy(states[linetotal].label, token);
token = strtok(NULL, space);
}
// a function
else if ((strcmp(token, "") != 0) && (strcmp(token, "\n") != 0)){
//printf("Function :: %s\n", token);
strcpy(states[linetotal].opcode, token);
if ((strcmp(token, "halt") != 0) && (strcmp(token, "mul") != 0) && (strcmp(token, "sub") != 0)) {
token = strtok(NULL, space);
strcpy(states[linetotal].arg, token);
//printf("arg :: %s\n", token);
}
token = strtok(NULL, space);
linetotal++;
}
else
break;
}
// the output process
printf("System State:\n-------------\n");
printf("prog.PC: %i\n", EP);
printf("Number of locals: %i\n", localspot);
for (int i = 0; i < localspot; i++)
printf("Local %i: %i\n", i, locals[i]);
printf("prog.PROGSIZE: %i\n\n", linetotal);
printf(" Program \n--------------------\n\n");
for (int i = 0; i < linetotal; i++)
printf("%.6s %.5s %.6s\n", states[i].label, states[i].opcode, states[i].arg);
fclose(ifp);
return(0);
}
【问题讨论】:
-
不要忽略它 - 运行
gdb并进行回溯(找出导致段错误的行) -
fread(str, FREAD_SIZE, 1, ifp);其中str是main()中的自动变量char str[256],而FREAD_SIZE是#define FREAD_SIZE 1024,这是灾难的根源,而且只会变得更糟。还有statement states[STATES_SIZE];,其中每个statement是三个char[256]数组的结构,相当于将768KB 最小 添加到main()的本地堆栈空间。如果它没有违反您的默认限制,它肯定是接近的。
标签: c segmentation-fault