sem_open really strange behavior
Tue Jun 22 14:08:00 GMT 2010
Sergio Sobarzo wrote:
> Thanks a lot Andrew,
> I read that paragraph thousand of times. However it is not an answer why
> it works if a line is commented or not. I cannot figure an acceptable
> explanation, that's why I thought it will be a gcc question.
It's because of the stack. Probably on your host (x86 I guess?)
arguments to functions are passed on the stack.
On your case you forget to pass two arguments, but sem_open
will read something from the stack anyway, most
certainly garbage. "Invalid argument" is documented in
the manpage I have here as
"EINVAL value was greater than SEM_VALUE_MAX".
Adding a local variable to a function modifies the layout and
content of the stack.
One time you will be lucky. The other one no. In any case,
you have a bug in your program.
Something like that. To fully understand try to trace your
program at the assembly level and see how the arguments are
smoked from the stack in sem_open (or read the sources of
the C library, you might get some clues too).
Hope that helps.
More information about the Gcc-help