开发者

Pointer comparison crashing my program

开发者 https://www.devze.com 2023-01-29 03:58 出处:网络
I have a line of code that is crashing my program in release mode, but not in debug mode... if((DWORD)pPrev < (DWORD)pBlock && pPrev->pList == &gFreeList)

I have a line of code that is crashing my program in release mode, but not in debug mode...

if((DWORD)pPrev < (DWORD)pBlock && pPrev->pList == &gFreeList)

I cast to DWORD to compare the addresses of the pointers... In this if-block, the previous node will accumulate the current node, but only if its physical address is lower than the current bl开发者_C百科ock and it is free already... The block code is not the problem. It won't even enter the block. It crashes during the comparison of the pointers.

The code works perfectly in debug mode, so I can't get it to crash or even malfunction in debug mode for me to see what is going on... I don't see what's wrong with this. Any ideas?

If it helps, I am using VC6 for prototype development (simply because the IDE is less resource-intensive)...

Thanks


Can't pPrev be not initialized?

If so, the precondition, (DWORD)pPrev < (DWORD)pBlock can not hold in debug mode because unitialized pPrev is set to 0xCCCCCCCC (or CD?), that will be surely larger than any pointer casted to DWORD (2, at most 3GBs in 32bit mode -> max userspace address 0xBFFFFFFF).

However, in release mode it would contain any garbage, so it could be dereferenced in (DWORD)pPrev < (DWORD)pBlock and crash the program.


Comparison should not be causing problems here. In fact, I think the comparison would work even without the casts to DWORD. I would suspect that it's pPrev->pList that's causing the problem. Are you sure pPrev has not been freed by accident before the dereference?

0

精彩评论

暂无评论...
验证码 换一张
取 消