问题
I run the following code in CLion:
int main()
{
char amessage [] = "oafaojfpa";
char * pmessage = "oafaojfpa";
char * apmessage = amessage;
amessage[2]='X';
*(pmessage+2)='X';
printf(amessage);
printf("\n");
printf(pmessage);
printf("\n");
printf(apmessage);
return(0);
}
The code *(pmessage+2)='X'; should raise exceptions. However, the output is:
/Users/spacegoing/Library/Caches/CLion12/cmake/generated/1ab7f406/1ab7f406/Debug/TCPL_Learn
Process finished with exit code 10
CLion only says exit code 10. But where can I view the exception message?
回答1:
Only c++ code throws exceptions. In this case you are experiencing low level errors. You see a C/OS return value 10 which is BUS ERROR.
Bus errors are rare nowadays on x86 and occur when your processor cannot even attempt the memory access requested, typically:
- using a processor instruction with an address that does not satisfy its alignment requirements.
- modifying read only memory
Your pointer pmessage points to a string literal. This string is stored at read-only memory and trying to modify this memory leads to undefined behavior. It usually either segfaults or bus errors.
来源:https://stackoverflow.com/questions/36489338/where-to-see-clions-exception-messages