memory addressing in c++

Started by
10 comments, last by ryt 18 years, 5 months ago
i have byte *vgamem =(byte *) 0xA0000; is this same as byte *vgamem = 0xA0000; ?? it should be since vgamem is a pointer and pointers store addresses and 0xA0000 is an address
Advertisement
No it's not the same. The first one will probably compile and the second one probably won't, since the second one is an assignment of an integer value to a pointer you need a cast of some sort to get the compiler to accept it.
so what byte (byte *) exactly mean?
Quote:Original post by ryt
so what byte (byte *) exactly mean?


0xa0000 is a number (type unsigned int), so by casting it to a (byte*) you tell the compiler that this number is to be treated as a memory address to a byte.
yep, i cant compile it, it says "Conversion from 'void*' to pointer to non-'void' requires an explicit cast"
im still confuzed, is there some good tut abaut this exactly? in my c++ book this isnt mentioned
it should be since vgamem is a pointer and pointers store addresses and 0xA0000 is an address

Not quite, I guess from your post you aren't aware of casting. A pointer is an address. 0xA000 is a number. C needs you to match types together by "casting" which means converting the type. So this:

int* foo = 0xA000;

won't work because the types don't match. One is a pointer, one is an integer. You cast by putting the type in brackets before the object you want to cast.

int* foo = (int*) 0xA000;

which says to the compiler "convert this value to an int*" which means you can then assign it to foo.
Quote:Original post by Anonymous Poster
int* foo = (int*) 0xA000;

which says to the compiler "convert this value to an int*" which means you can then assign it to foo.


couldnt we just put
int* f00 = (int)0xA0000;, whats the difference?

Quote:Original post by ryt
Quote:Original post by Anonymous Poster
int* foo = (int*) 0xA000;

which says to the compiler "convert this value to an int*" which means you can then assign it to foo.


couldnt we just put
int* f00 = (int)0xA0000;, whats the difference?


An integer is not an address. Even tough addresses an d integers are usually implemented as a word in modern hardware, there might be some architectures where they aren't.

In your last attempt, you're reinforcing the compiler's way of seeing 0xA00000: as a number. You effectively tell it: "Hey, this is a number, not a pointer, get it ? Don't go and think this is an address 'cause this isn't."

However, int* foo = (int*)0xA00000; is more along the line of: "OK, I know this is a number, but, you see, I want you to use it as an address, since, in my case, they're the same. Just so we're clear."
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
thx, i tried it and it seems to go ok
one more thing, if i make it like this:
int* intr = 0;
int* pint = intr;
what i have stored at pint??, since by &intr i would have address of intr, and by *intr i would have a value pointed by intr

aha, i think i get it, i would have the address stored at intr, am i right??

This topic is closed to new replies.

Advertisement