Sign in to follow this  
ryt

memory addressing in c++

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
yep, i cant compile it, it says "Conversion from 'void*' to pointer to non-'void' requires an explicit cast"

Share this post


Link to post
Share on other sites
im still confuzed, is there some good tut abaut this exactly? in my c++ book this isnt mentioned

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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."

Share this post


Link to post
Share on other sites
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??

Share this post


Link to post
Share on other sites
Yep. intr is a pointer, so it is a piece of memory somewhere that holds a value that indicates some other memory. When you copy it to pint, you copy that same value into another piece of memory, so the 'pint' piece of memory now holds the same value (and indicates the same 'other piece of memory') as the 'intr' piece.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this