2

How can I cast a binary array to a known class?

Essentially I have a byte array of data like so:

unsigned char * buff[sizeof(MyClass)];

I'm using unsigned char for each byte as I assume it is a length of 1 byte.

How can I cast this array to what I know the data represents? I have taken the data from memory of MyClass, and put it in this buffer - now I need to cast it back to MyClass.

I've seen reinterpret_cast but I'm not sure if it would apply here.

4
  • What you have is an array of character pointers. I doubt that's what you intended. And yes, you can use reinterpret_cast, but why do you want to do this in the first place? Commented Jan 3, 2014 at 23:13
  • @TaylorBrandstetter By an array of character pointers, I actually meant to have a pointer to an array of characters. How can I fix this? Commented Jan 3, 2014 at 23:15
  • unsigned char * buff; buff = new unsigned char[sizeof(MyClass)]; Commented Jan 3, 2014 at 23:16
  • You don't need a pointer to an array, you just need an array. If you declare unsigned char buff[sizeof(MyClass)]; and do reinterpret_cast<MyClass *>(buff), buff will end up acting like an unsigned char *. Commented Jan 3, 2014 at 23:17

1 Answer 1

3

unsigned char* buff[sizeof(MyClass)] is an array of unsigned char*, which is likely to be 4 or 8 times bigger than what you're expecting. (32 and 64bit platforms respectively, although, there are architectures that have even different pointer sizes - nitpick protection)

To answer the casting part of the question, reinterpret_cast<T*>(buff) will handle the chunk as T*.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, I've changed unsigned char* to just unsigned char.
@ChristianStewart Also if you specified what you're using it for, I would likely tell you you're doing it wrong and suggest a better way of doing it, whatever it is :). (In general, it is rarely the case you need to handle memory as a chunk of bytes rather than a type-rich "environment".)
Well, I've loaded a known data structure (of classes) from another process using ReadProcessMemory into that buffer (unsigned chars as they are size 1 byte right?) and am now re-casting it to the known data structure (a single class). A couple issues I see with this are there are POINTERS in the data, which point to absolutely nothing within MY process, so I have to resolve those when called..
@ChristianStewart ah, right, then such buffers aren't a terrible idea, and yeah, unsigned char will be 1 byte, however-many bits that may be in arcane corners of the universe
Yeah, now the problem is, all of the pointers point to things in ANOTHER PROCESS, so I have to go through all of them and load them int o the local process.. oh lord..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.