KKG KKG - 2 months ago 25
C++ Question

c++ memcpy read memory

I'm trying to use memcpy to read memory.

BYTE test[] = {0x01};
BYTE test2[] = {0x00};
memcpy (test, test2, sizeof(test));
if (test == test2){
MessageBox::Show("Same");
}else{
MessageBox::Show("Different");
}


Why the test and test2 always different?

Thanks for your help.

Answer

You're comparing addresses of test and test2. Use memcmp instead.

Your code is equivalent to

if (&test[0] == &test2[0]){

Changing it to

if (memcmp(test, test2, sizeof(test)) == 0)

should work as you expected.