4 ms·
But how do you know whether a JSON string is a pointer to static data (like in the example with the `main` function) or was dynamically allocated? The same goe
by tempodox 2y ago
But how do you know whether a JSON string is a pointer to static data (like in the example with the `main` function) or was dynamically allocated? The same goes for arrays and objects.
- threeducks 2y agoYou could tag dynamically allocated objects with some magic bytes. To then check whether a string is dynamically allocated, you can simply compare the first few bytes against the magic bytes. This is a probabilistic method, but if you use enough magic bytes, it becomes basically impossible to go wrong. See e.g. https://github.com/99991/dynamic/blob/a423a04061ee44bad0720fbd29f2321cc276564a/src/dynamic_gc.c#L21 https://github.com/99991/dynamic/blob/a423a04061ee44bad0720f... for an example (incidentally also a C JSON parser, but with automatic garbage collection).
- xnacly 2y agoHi, author here, i use the 'type' field of the json_value struct, strings, object keys, object values and array members are allocated, i use this info for freeing the memory
- UncleEntity 2y agoSo, umm, abusing the C switch statement to implement polymorphism? I kid, I kid... I am curious about how much this really saves over just using C++ classes and something like the curiously recurring template pattern while turning off runtime type checking.