Skip to main content
gamedev.net
🔒 Locked

Typecasting pointers doesn't just copy values?

Started by Enfekted Jun 9, 2004 at 10:33 AM 6 replies 400+ views
Original Post
Enfekted
Enfekted
Wow, am I ever confused. I''m writing a compiler for a custom syntax and I ran into an interesting phenominon that I find incredably odd. I have a variable that is pointing to memory dynamically allocated using Kernel32.dll''s HeapAlloc(). When I typecast the pointer to another variable the new variable points to a different location in memory. Why does it do that?
void *pValue = HeapAlloc(ph, HEAP_ZERO_MEMORY, size );
// pValue == 0x04a4295c


char *pCharValue = (char*)pValue;
// pCharValue == 0x00173C83


// Shouldn''t pValue == pCharValue like it would if I were to do the same thing in C++?

kuphryn
kuphryn
How did you determine these memory addresses?

pValue == 0x04a4295c
pCharValue == 0x00173C83

Sounds like those are the actual address of the pointers, not what they point to.

Kuphryn
fooman
fooman
i suggest to use new and delete for heap-allocation

addition: they cant be the adress of the pointer, bc then he had to give pCharValue the address of pValue (would be pCharValue = (char*)&pValue )

[edited by - fooman on June 9, 2004 12:22:25 PM]
Enfekted
Enfekted
quote:
Original post by kuphryn
How did you determine these memory addresses?

pValue == 0x04a4295c
pCharValue == 0x00173C83

Sounds like those are the actual address of the pointers, not what they point to.

Kuphryn


I got the values from the Visual Studio Debugger
Enfekted
Enfekted
Hmmm, Actually I think something weird is happening here.

This is a screenshot of the debugger reporting the values of pValue and pDstValue which should equal each other.



The Console.WriteLine() produces the following
pValue = 0x00176f68
pDstVal = 0x00176f68

But you can see in the above image that pValue is being reported as 0x04a4295c

Maybe its a bug in Microsoft Studio???
quorn
quorn
you got to step over the line for the assignment to actually happen
Enfekted
Enfekted
I did step over the line. The break stopped the debugger just above where I cut off the screenshot. I even stepped through Memory.ReAlloc() which returned the 0x00176f68 value, but the debugger never updated pValue.

Weird... I never thought there'd be a bug in one of Microsoft's Products

[edited by - Enfekted on June 9, 2004 1:28:29 PM]
fooman
fooman
as i was working on my radiysort, i recognized, that there is a bug... but not all hope is lost: the local variables window workes fine for me...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.