Skip to main content
gamedev.net
🔒 Locked

Simple(?) Pointers Questions

Started by Razza2005 Mar 20, 2005 at 1:44 AM 4 replies 1.3k views
Original Post
Razza2005
Razza2005
Hello all. I have a question about pointers.

int *a,b=10,c=20;

a = &b
cout << *a << endl;

a = &c
cout << *a << endl;
In that bit of code, the memory address of b is assigned to *a and the value is printed, then the same if done for c. What I am wondering, is how I can (if I can) assign any memory address to *a, rather then just that of another variable. For example, so I can print the contents my RAM tot he screen or search for certain values, etc. Thanks for any help :)
nts
nts
void* p = _any_value_here; // like 0, 123, etc doesn't have to be a void pointer either

watch out of access violations though if you step out of your programs address space.
MaulingMonkey
MaulingMonkey
What the others said. However, since processes don't share the same address-space (that is, the same address in two different programs points to different blocks of actual memory/swap file area) you'll have to use OS specific functions if you want to try and snoop on what other processes have in their RAM.
Sharlin
Sharlin
EDIT: In addition to what MaulingMonkey said:

Note, however, that this is deeply in the Undefined Behaviour land, and almost certainly (assuming an x86 platform) won't yield desired results in anything but real mode (and even then you need special support from the compiler because of the segmented memory model).

With modern 32-bit operating systems and processors (Linux, BSDs, Win9x/NT series with anything above a 80386 80286) running in protected mode, there simply isn't any predictable mapping from a program's logical address space into the "real" physical addresses in the RAM. Most probably, the OS will kill your application if it tries to access data outside the pages allocated for it. So this is a pretty big no-no on modern systems, unless your platform/compiler/OS specifically guarantees that some special "magic" memory addresses are safe to use.

[Edited by - Sharlin on March 20, 2005 5:56:19 AM]
Roboguy
Roboguy
Quote:
Original post by Anonymous Poster
cast:

unsigned char* a = (unsigned char*)0x8000000; // where 0x8000000 is of course the address you want.
Or, if your using C++:
unsigned char* a = reinterpret_cast<unsigned char*>(0x8000000)
And, like the others said, don't use this unless you have a very good reason to (like you're making an OS).

Topic Locked

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

Sign in to reply to this topic.