Is
memcpy( ptr, &value, sizeof(char *) );
the same as
ptr = &value;
.....
I've never seen memcpy()
used to do this before.
It seems like I should respond to the developer of this code with the phrase "Well, you're not wrong." It seems like a bit wordier than it needs to be.
Edit: It seems important to note that ptr
is void * ptr
. value
is double value
.
Edit2: For those who are interested in following this problem down its rabbit hole with me read along. This bit of code was flagged by static analysis as a potential buffer overflow. The static analysis was worried that the destination buffer ptr
might not have enough space to hold a sizeof(char *)
worth of information.
Rather than deal with the problem it gave me I tried to understand exactly what this memcpy()
did (thank you for helping me with that). Once I understood what was happening, I had to understand why the size of memory given to memcpy()
was sizeof(char *)
; the function this memcpy()
is in is called one time, and that one time it looks like this:
SdmJniGetPointer(blah, blah, &ptr)
Meaning that the actual value of ptr
within this function, and in the memcpy()
call, is the address of another pointer!
It was bizzare to me that the previous developer would memcpy()
a sizeof(char *)
amount of data because this system is compiled on 32 and 64 bit machines. However, because he passes in the address to a pointer (&ptr
) into the function as ptr
, he not only circumvents this shifting from 4-byte pointers to 8-byte pointers depending on the system architecture but wields it exactly as he meant too (I think). In the end, on a 64-bit system, all of the double value
is copied into the address pointed to by ptr
which is really an address to a different pointer (&ptr
), and on 32-bit machines, half of it is copied to the location ptr
which is really &ptr
. Now I just need to figure out what that double value
really represents, and why he would be ok bisecting it.
If that didn't make sense, I'll try again, but it feels a like he was Inception-ing the pointer. =)
This changes the address that the pointer
ptr
points to:This changes the data at the address that the pointer
ptr
points to:In the second case,
ptr
still points to the same address as before the call. So in no way are they the same.