1

I have an address or pointer to an object.

How do I convert the address to unsigned char array? I am using it as a reference to transport using sockets. Thanks.

Van Nguyen
  • 682
  • 3
  • 11
  • 21
  • 1
    The address, or the pointer?? – Lockhead Dec 16 '10 at 12:28
  • There is no point in transporting an address over a socket. Please clarify your question. What exactly do you need to transport? – Sergei Tachenov Dec 16 '10 at 12:36
  • I am attempting to build an online game. It's just a reference to know locations of where to attack stuff. – Van Nguyen Dec 16 '10 at 12:36
  • @Kaizoku, if I understand your question correctly, you want to use pointers as unique identifiers in your game. You shouldn't do that, for different computers can (and will) allocate objects at the same address, and your program will end up registering different objects created by different clients under the same id. – Frédéric Hamidi Dec 16 '10 at 12:43
  • I was thinking of "re-creating" the object on client side, but the client-side object has an extra field which also holds the address of original object on the server. So on the client, when a character attacks a monster, it will transport the command, and it also knows the address on the server. – Van Nguyen Dec 16 '10 at 12:51

5 Answers5

3
Object * pObject = ...;
unsigned char * pSz = reinterpret_cast<unsigned char *>(pObject);
// thru pSz you can see your object blindly as a pointer of unsigned char
// (the first char is pointed)

//and only if you know the size of your object (sizeObject) you can do this:
unsigned char pArray[sizeObject] = pSz;

Be really carefull if you ever use polymorphism, inherited objects may have totally differents sizes than there mother class Object;

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
  • Thanks, what I was looking for, but is there a better way to do these things? – Van Nguyen Dec 16 '10 at 12:42
  • @Kaizoku, you should be more precise. Do you transport only one type, I mean only one class ? If yes, you can do this its is OK. If not, I mean if you transport object of different sizes, of different implementation final class ( daugthers of Object), then you should really consider a really proper way of binary or xml serialization. – Stephane Rolland Dec 16 '10 at 12:45
  • I am designing protocol. something like 0x123123 "do stuff" 0x313131 but it will be all in byte array, using special character as deliminator. – Van Nguyen Dec 16 '10 at 12:53
  • if you use a particular word as a delimitator, just the way we may use \0 as a delimitator for char*, well I kind of feel you can go this way. – Stephane Rolland Dec 16 '10 at 12:56
  • May I advise you to use std::vector everywhere you can, using the STL is a better C++ practice than sticking to C memory management. However it depends on the time you have, learning STL containers and iterator can take some time, but know its well worth the effort. – Stephane Rolland Dec 16 '10 at 12:58
  • I know about stl, but how do I use the read() function on stl? I think it only accepts arrays. – Van Nguyen Dec 16 '10 at 13:01
  • Okay, I see. It won't be useful to you, however: when you have a vector and want to convert it to a C style array, you can always use std::vector<>::data() which gives a pointer to the underlying C array. However in your case, I'm not sure it's a good idea to construct a vector if your only aim is at feeding the transport protocol. – Stephane Rolland Dec 16 '10 at 13:09
2

If the type are completely unrelated (which apparently they are), you probably need a reinterpret_cast.

Community
  • 1
  • 1
icecrime
  • 74,451
  • 13
  • 99
  • 111
1
void *your_pointer;
unsigned char *new_pointer = (unsigned char*) your_pointer;

Since your question is tagged C++, may I also suggest that you don't convert pointers around, but try using some high-level library instead? Qt provides an excellent framework for sockets, and boost probably too.

Septagram
  • 9,425
  • 13
  • 50
  • 81
  • I need very optimized code as possible, also learning myself as I go along. Using a library doesn't provide these, but I look look at them for reference, thanks. – Van Nguyen Dec 16 '10 at 12:35
  • Optimization is often overvalued. 99% of the time it's better to quickly write easily readable code at the cost of performance. Later you can identify bottlenecks and fix them, but on the earlier stages of development you'll most likely see bottlenecks where they don't exist and miss the real ones. – Septagram Dec 16 '10 at 15:11
1
unsigned char *array = reinterpret_cast<unsigned char*>(my_obj);

However, you shouldn't send your objects simply by treating them as byte array. There is some data over there, like method addresses which will be useless after sending over network.

x13n
  • 4,103
  • 2
  • 21
  • 28
  • I just need reference on the client side, so that it can send back some commands to the server. Perhaps a lookup kind of thing, I am new to sockets, do you have any recommendations? – Van Nguyen Dec 16 '10 at 12:34
  • What do you mean by reference? If you want client to create some changes on server, you probably should develop some kind of protocol. If you want to send just some const-sized structure, declare one (just plain fields) and convert like in my answer. Network data, however, should be send in network order, because in general you can't assure that architecture on the other side is same as yours (See htons/ntohs functions manuals for more info). – x13n Dec 16 '10 at 12:39
-2
void* ptr; // Could be any type of course
ptr = something();
int n = sizeof(ptr);
unsigned char arr = new unsigned char[n];
memcpy((void*)arr[0], (void*)ptr, n);
// Remember to delete [] the array
Viktor Sehr
  • 12,825
  • 5
  • 58
  • 90