This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

What should I pay attention to when using the memcpy function dst_address parameter and the src_address parameter?

I have a question about how to safety use memcpy funtion.

I find some answer about memcpy, im sure that`s safe about src_address.But I want to know if the usage of dst_addr(memcpy funcation dst parameter) is correct.

I found some information as follows:

#include <string.h>
unsigned int * const dest;
void example (unsigned int * const unaligned_ptr)
{
__packed unsigned int * packed_ptr = unaligned_ptr;
char * temp_ptr = (char *)unaligned_ptr;
memcpy(dest, unaligned_ptr, 32); /* Unsafe */
memcpy(dest, (void *)packed_ptr, 32); /* Safe */
memcpy(dest, temp_ptr, 32); /* Safe */
}

In this example we can only see memcpy how parameter Src_address is safe to use, but I also want to know how parameter
Dst_address.

So please help me see if these usages are correct based on the above situation?

Example:
unsigned int * const src_ptr;
void example2 (unsigned int * const unaligned_dst_ptr)
{
__packed unsigned int * packed_dst_ptr = unaligned_dst_ptr;
char * temp_dst_ptr = (char *)unaligned_dst_ptr;

memcpy(unaligned_dst_ptr,src_ptr, 32); /*1. Is safe?*/
memcpy(packed_dst_ptr , (void *)src_ptr, 32); /*2. Is Safe? */
memcpy(temp_dst_ptr ,src_ptr, 32); /*3. Is Safe? */
}


Which of the above methods are safe memcpy destination address usage? Why?

If you are convenient, please give some examples and information, thank you ~!

I hope to get your answer.
Thanks everyone reader.Thank U~!

0