0

Not very prof. with C++. The code I've been working on is : https://msdn.microsoft.com/en-us/library/windows/desktop/ee175819(v=vs.85).aspx

My problem resides in this area :

_tprintf(TEXT("  Data portion begins at: %#p\n  Size: %d bytes\n") \
             TEXT("  Overhead: %d bytes\n  Region index: %d\n\n"),
             Entry.lpData,
             Entry.cbData,
             Entry.cbOverhead,
             Entry.iRegionIndex);
}

The problem I'm facing is, The Entry.lpData is the address of data portion of heap block. I want to read 8 byte before Entry.lpData address. So when I'm simply subtracting 8 from Entry.lpData and trying to read bytes, I'm getting the error

hexDump(entry.lpData - 8, 8);


heapwalk.cpp(119): error C2036: 'PVOID' : unknown size
Dev.K.
  • 2,428
  • 5
  • 35
  • 49

1 Answers1

2

Pointers to void are pointers to anything, so it makes no sense to perform pointer arithmetic on them directly. In this case, since you know you need an 8 byte offset, you will simply cast it to a char* first. In the general case you would know what sort of data it actually points to and cast it to a pointer of that type.

char *p = static_cast<char*>(entry.lpData) - 8;

This works because char* is an exception to the strict aliasing rule. Don't try this with arbitrary types.

Community
  • 1
  • 1
Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • Something like this -> unsigned char *p = (unsigned char*)address; ? – Dev.K. Feb 06 '15 at 20:01
  • @DavidA., C++ casts are better and `char*` is fine. `static_cast(lpData) - 8`. Note that 8 is a magic number that should be replaced with something that explains what the 8 actually does. – chris Feb 06 '15 at 20:02
  • @DavidA.: I added an example – Ed S. Feb 06 '15 at 20:04