0

I am using a shared memory for storing some data using the shmget function. Then I use shmat to attach it and save data. The next time i attach it, the program crashes. What are the possibilities that this line:

if((smemPtr = (int*)shmat(sMemId,NULL,0)) != (int *) -1)

crashes? I need to know the scenarios under which this line crashes. The sMemId has a value.

3
  • 2
    Why don't you build a debug build, and look at it in gdb?
    – Nim
    Jan 13, 2012 at 9:19
  • Are you sure it's that line that causes the crash ?
    – nos
    Jan 13, 2012 at 9:25
  • @elmo shmat() returns (void *)-1 when it fails, so because the result's being cast to int * here it kind of makes sense. I'm not convinced it'd actually return the same value though - certainly not on all platforms. Jan 13, 2012 at 9:29

1 Answer 1

0
(int*) (...) != (int *) -1

Pointer to another object is unsigned value. In this code i see C-style casting signed (negative) value to unsigned. Result is depend from compiler, platform, and in the general case -- undefined.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.