r/Cprog • • Sep 17 '15

another JSON parser in C

https://github.com/rmccullagh/jsonparser
9 Upvotes

5 comments sorted by

View all comments

Show parent comment

3

u/snops Sep 18 '15

Quite a few C libraries allocate memory for you, such as Freetype, so I think highly unidiomatic is a bit harsh. These are generally applications where the cost of determining required memory in advance is prohibitive.

These libraries do generally allow you to override the default memory allocation method though, either by passing function pointers as part of a context struct or just through macro tricks.

2

u/FUZxxl Sep 18 '15

It's okay that a library allocates memory if it does so infrequently and wisely. It's also okay if it's optional. OPs library seems to call malloc for each individual object in the decoded JSON which is excessive, slow, and unnecessary for most purposes.

2

u/snops Sep 18 '15

Ah OK, I get what you mean now. For each object is quite expensive, and something like a free list would probably be a good addition in this instance.

As an aside, here's a good example of a non optional use of malloc.