How to store memory address in a pointer?

Hey,

I 'm not even sure what I'm asking is even possible, but if it is I would really appreciate the syntax;

What I'm trying to do is:

1
2
3
4
5
6
7
8
9
int *p;
someType memoryLocation;
cout<<"Enter your memory location: ";
cin >> memoryLocation;

p = memoryLocation;

cout << *p;


I was just messing around with some code, and was curious to if this was possible.

-Thanks
You mean like someone inputs 0FD34...and you get whatever is there?

Just get the information into an integer or something then cast it to a pointer. That said, you won't get anything useful unless you are using some ancient system with no virtual memory system.
I am wondering if that is possible as well.
firedraco, yes, that is exactly what I want.
of course for the actually program I have already allocated and stored data into memory, and after I do so, I display the address.

Then I simply would like to type in the address, and access what is inside of it. (of course I already know what's inside) but I was just wondering if this was at all possible and if so how?

Anmol444,
I'm starting to think you are right :-(, but I will give it a few more tries ^_^
> Then I simply would like to type in the address, and access what is inside of it.
> I was just wondering if this was at all possible and if so how?

Use a variable of type std::uintptr_t to hold the value of the pointer.
And then perform a reinterpret_cast<>.
(This won't prevent the bad things that could happen if you type in a wrong value for the address.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cstdint>
#include <sstream>

int main()
{
    int i = 1234567 ;
    const int* p = &i ;

    std::string address ;
    {
        std::ostringstream ostm ;
        ostm << reinterpret_cast<std::uintptr_t>(p) ;
        address = ostm.str() ;
    }

    std::uintptr_t n ;
    {
        std::istringstream istm(address) ;
        istm >> n ;
    }

    const int* q = reinterpret_cast<const int*>(n) ;

    std::cout << "the int at address " << p << " is " << *p << '\n'
              << "the int at address " << q << " is " << *q << '\n' ;
}


http://ideone.com/FXpusv
Last edited on
Oh wow,
Thanks JLBorges
for the most part, this actually works, the only thing is, on line 13, I'm kinda being forced to store in address, rather then having the user input the value.

but over all this still works, I can work with this. Thanks ^_^
> on line 13, I'm kinda being forced to store in address, rather then having the user input the value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstdint>

int main()
{
    int i = 1234567 ;
    const int* p = &i ;

    std::cout << "address: " << reinterpret_cast<std::uintptr_t>(p) ;

    std::uintptr_t n ;
    std::cin >> n ;
    const int* q = reinterpret_cast<const int*>(n) ;

    std::cout << "the int at address " << p << " is " << *p << '\n'
              << "the int at address " << q << " is " << *q << '\n' ;
}


This is a bad program; it will cause undefined behaviour if the user enters a wrong value.
Last edited on
Topic archived. No new replies allowed.