0

While I studying cJSON source code, I don't understand why they use different malloc functions, instead of using a wrapper.

typedef struct internal_hooks
{
    void *(CJSON_CDECL *allocate)(size_t size);
    void (CJSON_CDECL *deallocate)(void *pointer);
    void *(CJSON_CDECL *reallocate)(void *pointer, size_t size);
} internal_hooks;

#if defined(_MSC_VER)
/* work around MSVC error C2322: '...' address of dllimport '...' is not static */
static void * CJSON_CDECL internal_malloc(size_t size)
{
    return malloc(size);
}
static void CJSON_CDECL internal_free(void *pointer)
{
    free(pointer);
}
static void * CJSON_CDECL internal_realloc(void *pointer, size_t size)
{
    return realloc(pointer, size);
}
#else
#define internal_malloc malloc
#define internal_free free
#define internal_realloc realloc
#endif

Obsidian
  • 3,719
  • 8
  • 17
  • 30
  • Are you asking about why the library uses the `internal_hooks` instead of calling `malloc` and `free` directly, or why the different code is needed when compiling with MSVC? – aschepler Oct 26 '20 at 14:01
  • Yes,the `malloc` function are different ? – amwps290 Oct 26 '20 at 23:29

0 Answers0