0

I'm seeing some weird behavior on a 64-bit Win 7 box in C++, Visual Studio 2010:

if((Event*)0 != metaData.event)

is true when metaData.event is 0. The debugger says metaData.event has the value of 0x000000000000000f, while (Event*)0 has the value of 0x00000000.

Where could the problem be?

Cheers, Mihai

4
  • That condition is true when metaData.even is different than 0
    – Marco A.
    Jul 28, 2014 at 12:27
  • 4
    I would prefer if (metaData.event) anyway.
    – chris
    Jul 28, 2014 at 12:29
  • because the if is true, although the pointer is 0. Jul 28, 2014 at 12:29
  • A pointer cannot have the value 0xF on Windows. Addresses in the range 0 - 0xffff form a so called NULL-pointer assignment partition, reserved for catching null-pointer assignments. That value may either indicate an incorrect conversion from int to pointer or memory corruption. Jul 28, 2014 at 12:50

2 Answers 2

2

The pointer is not 0 it's

0x0...(lots of zeros)..00f

which means 0xf = 15dec.

You're thus evaluating the condition as true even if the pointer is (likely) invalid.

3
  • Thanks for the response. I want to process the metaData when the pointer is NOT null, otherwise I'm dereferencing a null pointer. The thing is, VS seems to think the pointer is != 0 although it is 0. Jul 28, 2014 at 12:31
  • Woops, didn't see that last F, sorry. I thought the compiler automatically initializes pointers to 0. I manually set them to 0 in the constructor of the metaData object and everything works now, thanks! Jul 28, 2014 at 12:37
  • MSalters gave the correct answer, but after you Marco. You need to answer the question and not comment on this thread so as I can accept it. Jul 28, 2014 at 12:44
2

Your pointer apparently is 0xf, or fifteen. On Windows, that's not a pointer to a valid object (would be >= 0x1000). In fact, it's almost certainly caused by adding an offset to a null pointer. In other words, you've got prior Undefined Behavior.

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.