Store pointer in a string

Hello.

I need to store a "char *" in a string, and then use that string to access the same pointer.


How can this be done?
1
2
3
4
5
6
7
8
9
10
11
12
template <typename T>
std::string save_pointer(T *p){
    return std::to_string((uintptr_t)p);
}

template <typename T>
T *restore_pointer(const std::string &s){
    std::stringstream stream(s);
    uintptr_t ret;
    stream >> ret;
    return (T *)ret;
}
Hey man.

string v = to_string((int) &b[1]);
int z = stoi(v);
char * x = (char*) z;

If there is no cleaner way to it, i'll go with the above.
But is it valid, or undefined behavior?
Casting a pointer to int is unsafe, because there's no guarantee that an int is big enough to hold the pointer. stoi() is therefore also unsafe.
The only integer types you can use are uintptr_t and intptr_t.
Obvious question; why don't you store the char* in a char*?
"What, and ruin the plot?"
Helios, thank you. I'll think about that.

Repeater:
I have: "map <string, string> request_info", where I store the request info sent to the server. The pointer is the position of where the body starts in a request message, which I have to use later in the program.

So the best place for me to store "Body_Start" would be in "request_info", along with the other information that's also about the request.

It would be good if I could have everything that falls under the same category in one place.
It easily gets messy otherwise.


Feel me bro? :)
Feels like this could be a lot simpler.

For example:

1
2
3
4
5
struct requestMessage
{
  string entireMessage;
  int locationOfBodyStart; // the element in the above string where the body begins
};


stored in a map<string, requestMessage>. Done. The string, and the information about where in that string the body begins, stored safely together. No weird turning memory addresses into strings, and hoping that the memory never changes.

If you store a pointer to part of a string, then you have to make sure that string still exists in the exact same place in memory for as long as you have that pointer. Assuming that the strings inside a map will never ever move is risky; you have no such guarantee. Were you actually storing the location of the body of the message inside the map, or were you getting a memory address inside a string that you were discarding anyway?

Storing memory addresses into these strings (and even worse, storing those memory address themselves AS strings), put simply, is dangerous and unreliable. Inventive, sure, but a complicated and dangerous solution to a simple problem. Added problem; if you're storing the strings in a map, then you'd need to put the string in the map, and then look a the string inside the map and get the memory address of interest to turn into another string. What a pain that is. And still weird. Why store it as a string when you could store it as a char*? You say because you have a map of strings, but you could have changed it to a map of char*.
Last edited on
Didn't know that much about structs, and i'll use it instead. Thanks a lot.

You say because you have a map of strings, but you could have changed it to a map of char*.


The strings fit the design better than char *, on that part of the program. Based on functions etc. that were already in use.

string v = to_string((int) &b[1]);
int z = stoi(v);
char * x = (char*) z;

that seems weird.

char*x = (char*)(&b[1]); //?? is this right? depends on what b is.
now x points to the same location as b[1] but has been recast as bytes. you might do this to use the .write() binary file operation for example, or networking.
Last edited on
Topic archived. No new replies allowed.