Search code examples
iosobjective-ccc-preprocessormemcpy

Some C functions in iOS duplicated as #define


C functions like memcpy and memset are available as C functions as well as #define in iOS:

For example the #define memcpy, under the hood, is:

#define memcpy(dest, src, len)                  \
((__darwin_obsz0 (dest) != (size_t) -1)             \
 ? __builtin___memcpy_chk (dest, src, len, __darwin_obsz0 (dest))   \
 : __inline_memcpy_chk (dest, src, len))
  1. I gather there is some memory checking here but can someone shed some additional details on why it is better than a memcpy alone (where is the value added)?

  2. More importantly, when to use which?


Solution

  • Unless you #undef the macro, or call it like this (memcpy)(args...), it will always use the macro variant.

    I would personally just use the maco - it's intended to be fast and efficient, and will work as you expect.