Pointers basic question.

how can i get a data from a specific memory location.

for example a number 4 is stored in a memory location 0x229352A.

so how can i point the pointer to that location of memory and get the data.

(the address is accessable by normal user and OS is not interfering on anything)
1
2
int* p = (int*)0x229352A;
int contentsOfThatMemory = *p;
Thanks.

but can you explain the...

 
(int*)0x229352A;

part of the code in a bit detail.


EDIT: oops the code is running fine but the program is keep crashing.

i done it once when i was learning about pointers but now i forgot how to do it. i know it should not give any error.

i tried your code but it's keep crashing and i would like to show you the code i am trying to run.

1
2
3
4
5
6
7
8
9
10
#include<conio.h>
#include<stdio.h>

int main()
{
           int x=9,*p;
           p=(int*)0x2293528; //lets think that the variable x i declared is stored in given adderss 
           printf("%d",*p);
           return 0;
}

the output should be 9 but it's keep crashing.
Last edited on
It instructs the compiler to take the integer value 0x229352A and treat it as if it were a pointer to an int.

Explicit conversion, here: http://www.cplusplus.com/doc/tutorial/typecasting/
Last edited on
code:
int* p = (int*)0x229352A;

int* p pointer to int
0x229352A memory address (hex value)
(int*) convert to int pointer (location)
this is same as:
int* p = static_cast<int*>(0x229352A);

here new operator returns memory addres (hex value) and assign it to int pointer
int* p = new int

so there is no diffrerence will you do that with new operator or manulay assing an address.
Last edited on
int* p = static_cast<int*>(0x229352A);

Last time I tried, a reinterpret_cast<> was needed.
Last time I tried, a reinterpret_cast<> was needed.

yeah, you're right.

that why "C stile" casting always success,
cos (int*) is static_cast and reinterpret_cast in same time, when staic does not work then it uses reinterpret.

correct me if I'm wrong.
here is the code and its not working either
its keep crashing.
1
2
3
4
5
6
7
8
9
#include<conio.h>
#include<stdio.h>

int main()
{
    int x=8,*p=reinterpret_cast<int*>(0x2293532);
    printf("%d",*p);
    return 0;
}


i want to get the data from the given memory location.

You do know that every time you run this, you're not guaranteed to have 'x' stored in the same memory location? Address 0x2293532 could belong to any running process, not just yours. It's also likely to be unallocated.

If you want to access the memory where x is stored, use

1
2
3
int x = 8;
int* p = &x; // Address of x
printf("%d", *p);


Cheers,
Jim
i know that the address is keep changing.
to prevent that kind of error i first check the address of the variable stored in and after that i use the pointer to get the data from there.

the example you given above is something i know.
i want to do this because i want to learn how to do it and i need it.

so please help.
Last edited on
How are you checking the address of x?
Thank you jim80y you solved my problem XD.
That's cool - although I'm not sure how ;-)

Jim
Topic archived. No new replies allowed.