The signature of memcpy function is
void *memcpy (void* destination, const void* source, size_t num);
so as you can see it doesn't assume anything about pointers involved with the copy, they are just pointers. So if for example you want to copy a range of ints
to a range of floats
compiler won't complain about that.
Type Safety is a tool that helps developers to avoid certain errors by preventing some kind of erroneous code being compiled (and lately executed). It analyzes semantic aspect of source code to check if conversion between types and types in general are cohoerent.
What does that mean? It means that if your program passes the type checking phase you can be sure not to generate CERTAIN KIND of errors at run-time.
Of course sometimes you need to force this check not to be done, that's why you can use casts to force things to be what you want. Think about another example, malloc
: it is defined to be
void* malloc (size_t size);
so when you want to allocate a pointer to floats
for example you do:
float* ptr = (float*)malloc(sizeof(float*)*COUNT);
You are forced to cast the result of the function to be float*
otherwise the typecheck will find an assign of a void*
to a float*
but void*
is too generic to be assigned so: TYPE CHECK FAIL!
That's why memcpy
is not type-safe. It doesn't check anything, it just copy from a pointer to another pointer.