简体   繁体   中英

cJSON_Delete() and cJSON_free()

am still new to the cJSON library and i cant fully understand the uses of cJSON_Delete() and cJSON_free(),

  1. Is there any document that accurately describes what functions should be released, also when to use cJSON_free() and when to use cJSON_Delete().
  2. What is cJSON_InitHooks() purpose and how to use it in my code.
  3. Everytime i declare a variable cJSON *Variable; do i need to free it to minimize memory usage or it will free itself??

Thanks!!

A quick glance in the Readme and the header file shows:

  1. From the letter: No, the project seems not to provide such documents.

    • Anyway, you don't need to call cJSON_free() if you don't call cJSON_malloc() . It's more a helper function to let you call the free() and malloc() hooked functions.

    • You need to call cJSON_Delete() for any cJSON object you receive from any of the allocating functions, like the parsers.

  2. The purpose of cJSON_InitHooks() is to provide your own memory allocation functions to the library. This might be interesting if you don't want to use the default functions or if you use a target without (working) malloc() and free() .

  3. The declaration as such does not allocate memory for a cJSON object. If you don't obtain such an object, you cannot call cJSON_Delete() successfully. By calling cJSON_Delete() , the memory allocated by one of the parsers for example, will be freed.

It seems as if you need to learn about pointers and dynamic memory allocation to use this library correctly. This is fairly basic C stuff independent of this library.

However, reading the provided introduction (especially the examples) and if in doubt the source will help, too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM