First of all, this is not C++. This is ugly C. You're manually allocating a block of memory the size of 1024 bytes, capturing its address in a naked pointer. Also, in the code sample, you never free any of it, leaking a MB of memory. This is considered an extremely bad practice when it comes to C++.
C++ is not C with classes, it's a beautiful language that can only express itself by using it in combination with the standard library and adhering to better design patterns which come naturally with OO programming, especially with the notions of RAII, general encapsulation which can result in better memory management. Even new
is evaded when unnecessary, let alone malloc
.
for (int i=0; i != 8; i++)
Also, you're making dangerous assumptions here which can lead to undefined behavior.
((char*)Buffer)[i] = ((char*)Test)[i];
I am not even going to comment on this. Holy... Not only it's bad, it doesn't work. You're trying to force an instance of struct
A
lying on the stack, a regular "object"/var, into a char*
. You're missing the &Test
.
Use memcpy if you're taking this path, but please consider improving on your C++ approach. memcpy takes three arguments, a destination, a source and the size in bytes. Make sure the two match up and you'll get a quick per-byte copy.