at first excuse me for not providing any code, but it’s hard to just C+P an excerpt, since the errors are caused somehow randomly.
I am encountering a very strange error when compiling my C source with GCC. I am developing a linked-in driver for Erlang, and I do not understand what is causing the error. The error goes like this:
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0xffffffffb012aae8
[Switching to process 7316 thread 0x1503]
ktqk_exec (query=0x13e0af00, table=0x13e00ea0) at ktqk.c:215
215 clock_t start = clock();
I am running the Erlang virtual machine wrapped with GDB, so I can access the memory sections. To me, the high address 0xffffffffb012aae8 looks very suspicious. However, with Clang everything works as expected, no errors, no segfaults. I tried to investigate:
(gdb) p clock
$1 = {<text variable, no debug info>} 0x7fff85c29fd0 <clock>
(gdb) p start
$2 = 2954013712
So the value was obviously not initialized, it crashed before. When I set breakpoints in the same file, they are simply skipped. Why does everything work with Clang, but not with GCC?
Since Clang uses C99 and GCC C89 by default, I had to included the -std=c99 flag for compilation on GCC. May this be a potential source? However, when I comment out the code above, it fails at the next function call. So it seems somehow related to function calls. Nevertheless, all function calls before this line are fine.
A very strange error. Does anybody have any ideas? Sorry for this rather fuzzy explanation, I am simply not understanding the error.
All the best,
Martin
I can answer my own question: the code that was causing the error can be found below:
So
selectwas initialized to -1 and, if something would have been found, it would have been> 0. Now, in my example nothing was found, soselect = -1. Putting the -1 inlists, the result was alsolists[select] = -1, so obviously the same memory region asselect. However, now we’re initializing a list of integer pointers of size -1. And that is clearly wrong.Why is Clang not complaining about this severe error!?