0
int p_entity(char **data){
  char *pch;

  pch = strtok(*data, " \n");
  // printf("%s ", pch);
  pch = strtok(NULL, " \n");
  // (*data) = (*data) + 1;   // 1. this is okay
  //  (*data) = pch;          // 2. but doing this will cause an error
  printf("%c %d \n", *pch, pch); 
  printf("%c %d \n", **data, *data);
}

From the above code I will get the following if I uncomment 1:

g 4927479
e 4927456

I will get the following if I uncomment 2:

g 4927479
g 4927479
      3 [main] main 8172 exception::handle: Exception: STATUS_ACCESS_VIOLATION
    470 [main] main 8172 open_stackdumpfile: Dumping stack trace to main.exe.stackdump

Can anyone explain why I am getting that error? I would think both assignments would be legal and incrementing (*data) would be equivalent to straight assignning the address I want it to be at.

4
  • 2
    what sort of evil code is that? :) why are you even passing as char**? why are you trying to reassign (*data) ? Commented Sep 28, 2011 at 0:08
  • 1
    this is... oy. this code is scary beyond words. Commented Sep 28, 2011 at 0:09
  • Identifiers like entity, data, pch for "pointer to char" come straight from mindprod.com/jgloss/unmainnaming.html Commented Sep 28, 2011 at 0:28
  • I am writing a code beautifier, so I load the file into memory and begin priting it out in a beautified format. So *data is the location of the current position in the memory. **data is the actual current char. So I pass this function the current location on **data, and I process it, then I move foward in the data by incrementing *data. The think I don't understand is that I can increment *data but I can't directly assign it an address. Commented Sep 30, 2011 at 0:11

2 Answers 2

2

Seems like even when you get the error (and uncomment 2), you still get the two printfs... seems like it is crashing much later in your code.

Sign up to request clarification or add additional context in comments.

1 Comment

I think error happens when I try to leave the function, something gets popped out of the stack that shouldn't or something by assigning the location ch to *data
0

Answer really depends on how you are handling data before and after calling this function.

if you are allocating memory to *data before calling this function and tying to free it after this function returns, it could crash, as you have changed its value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.