简体   繁体   中英

Subtracting from LPVOID

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM