Archived

This topic is now archived and is closed to further replies.

christheffer

how to store a memory address from a pointer in a long variable?

Recommended Posts

i want to put bounds on a certain char pointer and constantly have it looping through a small set amount of memory addresses checking for stuff, but i can not think of any way to get the memory addresses from the pointer to a regular int or long. i set the starting memory address like: var=&startingvar; but i dont have a variable at the ending point that i could set the pointer to the address. basically what i want to be able to do is set the address of the pointer to an arbitrary mem address, one that is not that of a variable in my program and to be able to compare the address to which the pointer points to an arbitrary number i would store in an int or long. is there any way to do this?

Share this post


Link to post
Share on other sites
quote:
Original post by christheffer
i want to put bounds on a certain char pointer and constantly have it looping through a small set amount of memory addresses checking for stuff, but i can not think of any way to get the memory addresses from the pointer to a regular int or long.
i set the starting memory address like: var=&startingvar;
but i dont have a variable at the ending point that i could set the pointer to the address.
basically what i want to be able to do is set the address of the pointer to an arbitrary mem address, one that is not that of a variable in my program and to be able to compare the address to which the pointer points to an arbitrary number i would store in an int or long. is there any way to do this?


I am not totally sure what you are trying to do or why you''re trying to do it but if you really wanted to, this *might* work. A pointer is 4 bytes, 32 bits. So it will fit into a int. When you try and print a non-dereferenced pointer you get something like ''FA57B3A7'' which it just 4 bytes displayed in hex instead of typical decimal format like ''3324238541. To store that value as in an int just cast it to an int like so:


int myVariable = 57;
int* p = &myVariable;

int storePointerAsInt = (int)p; //Store your pointer as an int. This may or may not even work.

//If you try this, you will probably get a number in the billions. But it will be the same ''number'' as your pointer, just in a different form of display.
cout << storePointerAsInt;


If I get what you are trying to do, why not just keep track of how big your array or whatever it is you''re checking is and use a for loop that runs until it reaches the end or finds what you are looking for. Not sure what type of project you are working on but they are surely better ways than storing the pointer as an int. If you give some more detail I or someone can probably show you a less messy way.

Share this post


Link to post
Share on other sites
You can cast a pointer to an int, but an unsigned int is better - unless you expect negative memory addresses...

unsigned int addr = (unsigned int)ptr;

You should familiarize yourself with how your target OS organizes the memory it makes available to a program.

Windows, for example, assigns each process an address space of 4 Gb - which corresponds with 2^32. In other words, you can fit any address into a DWORD (unsigned long).

A pointer stores an address. The address of a pointer could be considered a pointer pointer. Do you want to set a pointer to point to an arbitrary address? or do you want to set a pointer pointer to point to an arbitrary address?

Here''s an example of both, although NULL (ie 0) isn''t quite arbitrary.

char *ptr = NULL;
char **ptr = NULL;

Try this experiment on Windows.

Declare the following pointer

void *base = (void*)0x00400000UL;

Then use ''base'' in place of the hInstance parameter to a function that requires it - RegisterClass, CreateWindow, etc.


Share this post


Link to post
Share on other sites
i forgot to specify that this program is written for windows.

i was trying to set the the pointer to an arbitrary address, that is, i would store the address in a long and assign it to the pointer, as the address to which it points.

i think i got it to work sufficiently. there is still a problem but i think i can overcome it easily.
i did somethhing like:

sprintf(charvar, "%u", &addressvar);
long=atoi(charvar);

then i can put it in the address and use a for loop or similar to count between the addressvar''s address and the arbitrary address.
is there some reason this code would not work correctly?

thx for the help

Share this post


Link to post
Share on other sites