Converting the 'this' pointer

Hi,
I need to store a 'this' pointer of an object to a string and restore it from the string later in the program flow.

The first task is the easy one:
sprintf(szBuffer, "%p", this)

But how I will retrieve the 'this' pointer later from the szBuffer?

Thanks
Last edited on
closed account (z05DSL3A)
1
2
I need to store a 'this' pointer of an object to a string and restore it 
from the string later in the program flow.

Why?
Thanks Grey Wolf for your response. The answer to why its a long, long story... :)
I am working with a 3D game engine and coding a world editor consisting from 3D meshes.
Each 3D mesh has its own memory handle, an INTEGER, and it is a protected member of a class name CActor.
During the creation of the 3D mesh I have the ability to store a string into mesh and I can retrieve this string having the INTEGER before as an argument.
When the user selects a 3D mesh with the mouse then my 3D engine returns the mesh INTEGER. I must write code to locate quickly which object is selected which means I have to locate the address of the proper CActor instance.
closed account (z05DSL3A)
I don't know if this would be of any help, When you get the pointer address back out it would have to be given to a pointer of the correct type.
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
28
29
30
31
32
#include <stdio.h>
#include <iostream>


struct Test{
	int a;
};


int main(int argc, char *argv[])
{
	Test test;
	Test *pTest; 
	pTest = &test;

	pTest->a = 9;

	char buffer [50];
	sprintf(buffer, "%p", pTest);

	Test *pTest2;
    sscanf(buffer,"%x",&pTest2);

	
	printf("the value of pTest is:  %p\n", pTest);
	printf("\tthe value of pTest->a is:  %d\n", pTest->a);
	printf("the value of buffer is: %s\n", buffer);
	printf("the value of pTest2 is: %p\n", pTest2);
	printf("\tthe value of pTest2->a is:  %d\n", pTest2->a);
	
	return 0;
}
I tested and it works, but I think it is not safe because the %x type in sscanf function reads an integer which is a smaller size than an instance address

Thanks any way!
closed account (z05DSL3A)
Yep, you could try somthing like:
sscanf(buffer,"%lu",&pTest2);
I can't remeber the exact code , but you should be able to look it up.
Last edited on
Yea,
This is good for a 32bit application. As for the returned address type casting works both for VC and Mingw compilers. I mean that I got no error when coding:
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
28
29
30
31
32
33
#include <stdio.h>
#include <iostream>


struct Test{
	int a;
};


int main(int argc, char *argv[])
{
	Test test;
	Test *pTest; 
	pTest = &test;

	pTest->a = 9;

	char buffer [50];
	sprintf_s(buffer, "%p", pTest);

	unsigned long pTest2;
  sscanf_s(buffer,"%x",&pTest2);

	
	printf("the value of pTest is:  %p\n", pTest);
	printf("\tthe value of pTest->a is:  %d\n", pTest->a);
	printf("the value of buffer is: %s\n", buffer);
	printf("the value of pTest2 is: %lu\n", pTest2);
	printf("\tthe value of pTest2->a is:  %d\n", ((Test *)pTest2)->a);
	getchar();
	
	return 0;
}


I don't see any issue any more.
Thanks Grey Wolf, you helped me!
Last edited on
Topic archived. No new replies allowed.