36213

C Segmentation Fault when accessing a char array index

Question:

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. When I tried printf-ing breakStatus and breakAddress, they both hold the expected values (an input example would be <em>break set x0001</em>, where breakStatus = "set" and breakAddress = "x0001").

I know that if I use the specifier to %c, or <em>printf("breakStatus[0] is %c\n", breakStatus[0]);</em> , 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.

I'm not sure how to handle this. I've tried casting breakAddress[0] to a char and I still get the same error.

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.

Below are parts of my set() and executeBreak() functions for reference:

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. 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.

Answer1:

Heed the compiler warnings! :)

You have defined char breakStatus[20];, i.e. an array of characters. Then, in the printf statement, you've got printf("...%s...", breakStatus[0]);.

The %s formatting code expects a pointer to a string. breakStatus[0], however, is a char value. 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). Suppose the char value happens to be zero, then the whole thing magically turns into a NULL pointer. Trying to dereference a NULL pointer (or a 0xff pointer, perhaps... the value is essentially random, after all) almost inevitably causes a segfault.

(Update: what Vlad said... "did you mean: %c")

Answer2:

Use gdb instead of debug printings:

#gdb ./myprog (gdb) r

When the program stops with SIGSEGV take a look at the stack trace:

(gdb) bt

Recommend

  • Get all parent nodes with RecursiveArrayIterator
  • Convert XML into Lists of Tags and Values with Python
  • PHP: exif_imagetype() not working?
  • nasm issue relocation R_X86_64_PC32 shared library
  • Unit-test a method wrapper
  • Unexpected EOF while Parsing in Python Random Sentence Generator
  • Libspotify playing problems
  • How to randomly select values for a list?
  • a concept similar to pointers in as3?
  • JPA CDI Injecting DAO into an Entity
  • Replace and retrieve placeholder value
  • MySQL: Difference between `… ADD INDEX(a); … ADD INDEX(b);` and `… ADD INDEX(a,b);`?
  • Asynchronous Sockets - Handling false socket.AcceptAsync values
  • Find longest path less than or equal to given value of an acyclic, directed graph in Python
  • Why cout is producing no output on Code Blocks?
  • How to print columns containing value
  • Scipy Leastsq Optional Output Variable (Mesg)
  • Undefined references when compiling gSOAP client
  • Login not working in Firefox in Meteor
  • Unable to get column index with table.getColumn method using custom table Model
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • Meteor: Do Something On Email Verification Confirmation
  • Can I display google adwords (AdView) in javafx on android
  • Where to put my custom functions in Wordpress?
  • Can I make an Android app that runs a web view in Chrome 39?
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Build own AppleScript numerical error handling
  • Websockets service method fails during R startup
  • Google cloud sdk not working when python points python3
  • Delete MySQLi record without showing the id in the URL
  • RestKit - RKRequestDelegate does not exist
  • How do I rollback to a specific git commit
  • Is there a mandatory requirement to switch app.yaml?
  • WPF Applying a trigger on binding failure
  • Hits per day in Google Big Query
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass