简体   繁体   English

访问char数组索引时出现C分段错误

[英]C Segmentation Fault when accessing a char array index

I'm programming a feedback loop that takes in user commands and processes the arguments based on the command received. 我正在编写一个反馈循环,该循环将接收用户命令并根据收到的命令处理参数。 So far my function looks like this: 到目前为止,我的函数如下所示:

//main loop of the program that keeps repeating, which each cycle taking in one user input
 void mainLoop(){

char input[100]; // initial command
char reg[2]; // registers R1-R7 and PC
char value[20]; // register value
char breakStatus[20]; // can be either "set" or clear"
char breakAddress[80]; // address of break point in hex


//Retrieve user input
printf("Enter a command: ");
scanf("%s", &input);

//Process user input:

//set (completed)
if(strcasecmp(input, "set") == 0){
    scanf("%s", &reg);
    scanf("%s", &value);    
    set(reg, value);
    printf("register value is %s\n", value);
    printf("value[0] is %x\n", value[0]);
    //printf("Setting %s to %s!\n", reg, value);
}

//break (something's wrong here)
else if(strcasecmp(input, "break") == 0){
    scanf("%s", &breakStatus);
    scanf("%s", &breakAddress);

    printf("breakStatus is %s\n", breakStatus);
    printf("breakStatus[0] is %c\n", breakStatus[0]);
    //printf("%sing break point at address %s\n", breakStatus, breakAddress);
    executeBreak(breakStatus, breakAddress);

}


//error (completed)
else
    printf("Error: %s is not a valid command.\n", input);

 }

My problem is that whenever I try to access breakAddress[index], I get a Segmentation Fault. 我的问题是,每当我尝试访问breakAddress [index]时,都会遇到分段错误。 When I tried printf-ing breakStatus and breakAddress, they both hold the expected values (an input example would be break set x0001 , where breakStatus = "set" and breakAddress = "x0001"). 当我尝试printfinging breakStatus和breakAddress时,它们都保存期望值(输入示例为break set x0001 ,其中breakStatus =“ set”和breakAddress =“ x0001”)。

I know that if I use the specifier to %c, or printf("breakStatus[0] is %c\\n", breakStatus[0]); 我知道,如果我使用对%c的说明符,或printf(“ breakStatus [0]为%c \\ n”,breakStatus [0]); , it returns a valid value. ,它返回一个有效值。 However I am passing breakStatus and breakAddress into another function, and in that function I check if breakAddress[0] == 'x', to which I promptly receive a Segmentation Fault. 但是,我将breakStatus和breakAddress传递给另一个函数,在该函数中,我检查breakAddress [0] =='x',我立即收到细分错误。

I'm not sure how to handle this. 我不确定如何处理。 I've tried casting breakAddress[0] to a char and I still get the same error. 我尝试将breakAddress [0]转换为char,但仍然遇到相同的错误。

What's really confusing me is that I pretty much coded my Set portion in the exact same way (same char[] initialization, same scanf process) and when I tried retrieving an index from one of the variables it works perfectly fine. 真正让我感到困惑的是,我几乎完全以相同的方式(相同的char []初始化,相同的scanf流程)对Set部分进行编码,并且当我尝试从其中一个变量中检索索引时,它工作得很好。

Below are parts of my set() and executeBreak() functions for reference: 以下是我的set()和executeBreak()函数的一部分,以供参考:

void set(char* reg, char* val){
    if(val[0] == 'x'){ // no segmentation fault
    sscanf(val+1, "%x", &registers[r]); 
}

void executeBreak(char* stat, char* add){
    if(!(add[0] == 'x')){ // segmentation fault here (tried casting as char also)
    printf("Error: invalid break address");
    return;
    }
}

If anyone could offer some insight as to why I'm getting a Segmentation Fault in one function and none in the other it would be greatly appreciated. 如果有人能提供我为什么在一个功能中遇到细分错误而在另一个功能中却没有错误的见解,将不胜感激。

EDIT: Got rid of using %s in my printf statement since that's not really where my problem is. 编辑:摆脱了在我的printf语句中使用%s,因为那不是我的问题所在。 Ideally I'd like to know why val[0] works in on function and add[0] does not, since they're given the same types of arguments. 理想情况下,我想知道为什么val [0]在函数上起作用而add [0]不能在函数上起作用,因为它们被赋予了相同类型的参数。

Heed the compiler warnings! 注意编译器警告! :) :)

You have defined char breakStatus[20]; 您已经定义了char breakStatus[20]; , ie an array of characters. ,即字符数组。 Then, in the printf statement, you've got printf("...%s...", breakStatus[0]); 然后,在printf语句中,得到了printf("...%s...", breakStatus[0]); .

The %s formatting code expects a pointer to a string. %s格式代码需要指向字符串的指针。 breakStatus[0], however, is a char value. breakStatus [0],但是,是一个char值。 The compiler warns you about what it does to fix that: it turns the char value into a char * pointer (char value = 0 => pointer address is 0). 编译器会警告您要解决的问题:它将char值转换为char *指针(char值= 0 =>指针地址为0)。 Suppose the char value happens to be zero, then the whole thing magically turns into a NULL pointer. 假设char值恰好为零,那么整个过程神奇地变成了NULL指针。 Trying to dereference a NULL pointer (or a 0xff pointer, perhaps... the value is essentially random, after all) almost inevitably causes a segfault. 尝试取消引用NULL指针(或0xff指针,也许……毕竟该值本质上是随机的)几乎不可避免地导致了段错误。

(Update: what Vlad said... "did you mean: %c") (更新:弗拉德说了什么……“你的意思是:%c”)

Use gdb instead of debug printings: 使用gdb代替调试打印:

#gdb ./myprog
(gdb) r

When the program stops with SIGSEGV take a look at the stack trace: 当程序以SIGSEGV停止时,请查看堆栈跟踪:

(gdb) bt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM