When calling memcpy()
, I try to make sure the source/dest pointers do not overlap because the specification says that is undefined behavior. For the following simple example, though, GCC generates a memcpy()
with source == dest.
#include <stdio.h>
#include <string.h>
struct my_struct {
int a[5000] ;
} ;
int main(void)
{
struct my_struct a ;
struct my_struct *p_a = &a ;
memset( &a, 0, sizeof(a) ) ;
*p_a = a ;
printf("%d\n",p_a->a[10] );
return 0 ;
}
Using gcc -O0
, both my local GCC (5.4.0) and godbolt.org GCC x86-64 10.2 produce the following line for *p_a = a
:
call memcpy
(I'm not using -O2
because that would require a more complicated example to demonstrate the problem)
I'm asking because we have an embedded system that generates a warning when memcpy()
source/dest overlap. However, some people want to turn off the warning if source==dest because of the above example. What's reasonable?