save memory address into an int

Nov 4, 2008 at 12:11am
Hi, I think this problem should be pretty easy, but I can't find the answer anywhere. In my assignment we have to get the address of a private class member string.

We've been told to use sprintf, so

I should be able to save the string's address into an int

and then I can have sprintf put it into a string for me.

Can anyone enlighten me?

Nov 4, 2008 at 1:00am
OK, maybe I wasn't very clear or something.

I have a class with a private member and it's a string.

I'm supposed to save the address of that string into an integer so that I can use in the sprintf function and save the address back as a string.

ex:

string tag;
string answer;
int tagaddress;

tagaddress =

sprintf_s(answer,"%x",tagaddress)

I've setting tag address = to &tag, but it doesn't work. Anyone know how to do this?
Nov 4, 2008 at 2:13am
Hmm, well I haven't actually tried to compile this, but you could try making a pointer to the data and then storing the data address of the pointer to the string? No idea if it will actually work though...

And also, usually people don't respond in <1 hr...

EDIT: Another possible idea...try reading the data address into a stringstream, then making the string = the stringstream.
Last edited on Nov 4, 2008 at 2:18am
Nov 4, 2008 at 2:45am
cool, I appreciate the comment and didnt mean for my last one to sound irritated lol.
This is the example that was laid out in the base program.
It's suggesting that the hex number would be an integer, I'm pretty sure at least. So, it would seem that somehow I should be able to save the address of a string into an integer...

// Example how to convert hexadecimal number
// (the tag address) to string
//sprintf_s(pg,"%x",hex number to convert);

I might be making this harder than it seems, since when I spoke to someone else about it, and didn't get the details, they said they pretty much just took out the "//" and it worked, but obviously the "hex number to convert" has to be a variable or some sort of value.
Nov 4, 2008 at 3:57am
Hrm, well I see two ways that might be wanted...

1.) The way you stated
2.) Change a hex number into a integer and store it to a string

For 1, I don't really have any more ideas.
For 2, you could just get the hex number and just store it to the string (IDR how it is actually stored though, so it could possible store 0xa to the string)...i.e.:

1
2
int hex = 0xa;
string String = hex; //maybe? 
Nov 4, 2008 at 4:27am
I tried this to no avail

// Example how to convert hexadecimal number
// (the tag address) to string
//sprintf_s(pg,"%x",hex number to convert);

// where tag is the address I need saved
// the sprintf function was given by the professor and I'm pretty sure
// it takes an integer in that hex number position
string* temp;
*temp = tag;
int add;
add = *&temp; // this doesn't work either, it's just one combination that I
// keep going over.
// the string = hex didn't work either I'm afraid
// would appreciate any help, thanks again
Nov 4, 2008 at 5:25am
i don't really think you need to do any hex converting, but either way, that's the easy part. also, there the code below should work to get the address... so here is a sample that may solve your problem:
1
2
3
4
5
6
7
8
9
10
11
#include <string>

// store the address in an int (notice the cast, to make sure the compiler isn't confused.)
int str_address = (int) &tag;

// now put the address in a string
char* add_str = new char [ NEEDED_SIZE ];
sprintf ( add_str, "%x", str_address );

// print out the hexadecimal representation of the address
cout << "address: " << add_str;


that should do it. the code above would basically allow you to print out the address of the string object 'tag' in hex representation. since an int is 32 bits, that means 8 hex characters, so the code above should produce something similar to:

address: 007bc013

Nov 4, 2008 at 1:36pm
Ok there's no reason to go through an int at all. I'm not 100% sure, but I believe that the C++ standard only mandates that sizeof( void* ) == sizeof( int ), which would mean that all of the above code snippets are not portable and happen to work because most compilers will make non-member pointers the same size as int.

1
2
3
4
5
6
string some_string;
string* ptr_to_string = &some_string;
char buf[ 2 * sizeof( string* ) + 3 ];
sprintf( buf, "%p", ptr_to_string );
some_string = buf;
cout << some_string << endl;


(I personally would go through stringstream though.
Nov 6, 2008 at 10:15pm
Agreed. While x86 and SPARC and many other architectures work fine, it is entirely possible for different pointer types to have different sizes and internal formats.

The only things the standard guarantees (IIRC) are:

sizeof( void* ) == sizeof( int ) [just like jsmith said], and

The integer zero is properly translated to the proper bit-pattern for a null-pointer of the given type on your hardware. (So that things like if (myPtr != NULL) work.)

Abuse of pointer/integer casting is common on x86 processors. It is less common on others.
Last edited on Nov 6, 2008 at 10:16pm
Nov 6, 2008 at 11:39pm
Thanks for confirming my recollection, Duoas!

Good to know I wasn't in left field on this one.
Topic archived. No new replies allowed.