C segfault源于读取标准输入,然后操作该数据



我遇到了一个问题,我试图从stdin读取单词,然后对其进行操作,然后稍后打印到stdout。然而,当我不重定向stdout时,如果我试图打印我从STDIN扫描的任何单词(我可以使用puts()向控制台显示信息,这很好),或者当我试图访问每个单词的固有数据时(比如我后来使用的strcasecmpr()),它会在两个地方出现错误。这里有人能帮忙吗?

int main(int argc, char** argv)
{
int aflag = 0;
int bflag = 0;
int cflag = 0;
int c;
//int opteval
Instruction head= NULL;
Instruction first= NULL;
init(head);
puts("Init for head done");
//While there are more instructions
char line [128];
printf("SdlJ");
int numlines = 0;
first = head; //Setting pointer to first instruction in list CHECK THIS LATER
int index = 0;
int rc = 0;
char instr[20], r1[300], r2[300], r3[300], r4[300];
instr[0] = '';
r1[0] = '';
r2[0] = '';
r3[0] = '';
r4[0] = '';
while( fgets ( line, sizeof line, stdin) != NULL){
puts(line);
rc = sscanf(line, "%s %s %s %s %s", instr,r1,r2,r3,r4);
if(rc == 4)
puts("here");
puts(instr);
//printf("sfds%ssfsd", instr); SEGFAULT CASE 1: THIS WOULD SEG FAULT IF I UNCOMMENTED IT
//return 1;
chooseOp(head, instr);
createReg(head, r1, 0);
if(r2[0] != '=' && r2[0] != '')
createReg(head, r2, 1);
if(r3[0] != '='&& r3[0] != '')
createReg(head, r3, 2);
if(rc < 5)
createReg(head, r4, 3);
puts("here");
Instruction next = NULL;
init(next);
numlines++;
head->next = next;
next->prev = head;
head = next;
//Point prevs's next to new head, and that heads prev to the previous head.
}
}
void chooseOp(Instruction head, char* token){
//Simple function to find the opcode responsible for this instruction
//printf("sfdjls");
puts("segfaulting");
// SEGFAULT PLACE 2, IT SEGFAULTS RIGHT AT THIS STRCASECMP BELOW
if(strcasecmp("nop", token) == 0){
head->opcode= NOP;
head->ismem=1;
head->latency = 1;
}
else if(strcasecmp("addI", token) == 0){
head->opcode= ADDI;
head->ismem=1;
head->latency = 1;
}
else if(strcasecmp("add", token) == 0){
head->opcode= ADD;
head->ismem=1;
head->latency = 1;
}
else if(strcasecmp("subI", token) == 0){
head->opcode= SUBI;
head->ismem=1;
head->latency = 1;
}
else if(strcasecmp("sub", token) == 0){
head->opcode= SUB;
head->ismem=1;
head->latency = 1;
}
else if(strcasecmp("mult", token) == 0){
head->opcode= MULT;
head->ismem=1;
head->latency = 3;
}
else if(strcasecmp("div", token) == 0){
head->opcode= DIV;
head->ismem=1;
head->latency = 3;
}
else if(strcasecmp("load", token) == 0){
head->opcode= LOAD;
head->ismem=0;
head->latency = 5;
}
else if(strcasecmp("loadI", token) == 0){
head->opcode= LOADI;
head->ismem=0;
head->latency = 1;
}
else if(strcasecmp("loadAO", token) == 0){
head->opcode= LOADAO;
head->ismem=0;
head->latency = 5;
}
else if(strcasecmp("loadAI", token) == 0){
puts("here23");
head->opcode= LOADAI;
head->ismem=0;
head->latency = 5;
}
else if(strcasecmp("store", token) == 0){
head->opcode= STORE;
head->ismem=0;
head->latency = 5;
}
else if(strcasecmp("storeAO", token) == 0){
head->opcode= STOREAO;
head->ismem=0;
head->latency = 5;
}
else if(strcasecmp("storeAI", token) == 0){
head->opcode= STOREAI;
head->ismem=0;
head->latency = 5;
}
else if(strcasecmp("output", token) == 0){
head->opcode= OUTPUT;
head->ismem=0; //Need to ask prof about this
head->latency = 1;
}
puts("here");
}

这是我应该阅读的一个例子:

loadI 1024     => r0 
loadI 0    => r1 
storeAI r1     => r0, 0 

等等。

请帮忙。

编辑:这是我的init(head)代码

void init(Instruction head)
{
head = (Instruction) malloc(sizeof(struct Command));
head->opcode = 0;
head->next = NULL;
head->prev = NULL;
head->ismem = -1;
head->firstReg = NULL;
head->secondReg = NULL;
head->outputReg1 = NULL;
head->outputReg2 = NULL;
head->successors = NULL;
//head->depends = NULL;
head->numInstructions = 0;
//head->lineNum = -1;
head->priority = 0;
head->numdepends = 0;
head->cycle = 0;
head->delay = -1;
}

这不会变成一个巨大的调试会话,但这是代码中的一个基本问题,必须首先解决。

void init(Instruction head) // note: passed by-value
{
head = (Instruction) malloc(sizeof(struct Command));
// etc...
}

考虑一下:使用一个函数,将调用方的int变量设置为某个值。你可以这样做:

void func(int *p) // note by-address
{
*p = 42;
}
// caller code
int a = 0;
func(&a); // pass address

指针也不例外。指针保存地址,但也有地址(这将它们与数组btw区分开来,后者只是的地址)。你的init()应该是这样的:

void init(Instruction* head) // note: by address
{
Instruction p = malloc(sizeof(struct Command)); // note no cast
p->->opcode = 0;
p->next = NULL;
// etc...
// save result to address given by out-parameter
*head = p;
}

主叫方:

Instruction head = NULL;
init(&head); // note address passed.

这应该会让你继续前进。


旁注

C程序员喜欢星号。真正地不是开玩笑。它们是代码中的巨大旗帜,从山顶上尖叫着"这是指针"。将其隐藏在typedef中很少有帮助。事实上,我只见过三个可以说是合法的案例,其中一个是转移注意力。

  1. 库API的abtract不透明"句柄"类型。这是普遍的,也是完全合法的
  2. 函数指针类型。通常用于确保将正确的类型用于不同的"回调"机制(例如typedef int (*callback_type)(void);)
  3. 避免在同一行中使用不正确的变量声明。(这是转移注意力)

其中第三个得到了最好的证明:

int* a,b; // declares one pointer-to-int, one int.
typedef int *int_ptr;
int_ptr a,b; // declared two pointers-to-int.

虽然这看起来很有帮助,但事实并非如此。当你稍后在代码中尝试这样做时,编译器会爆炸:

*b = something;

所以不管怎样(通常)你都会抓住自己。

总而言之,不要害怕星号。如果你允许的话,他们会帮助你看得更清楚。

相关内容

  • 没有找到相关文章

最新更新