The pervasive effects of C's malloc() and free() on C APIs

This page summarizes the projects mentioned and recommended in the original post on news.ycombinator.com

Our great sponsors
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • SaaSHub - Software Alternatives and Reviews
  • zig

    General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.

    > This is unavoidable in any language that (supports dynamic memory allocation and) moves dynamic memory allocation into a library.

    Not necessarily. The Zig[1] standard library forces callers to provide at runtime an allocator to each data structure or function that allocates. Freeing is then handled either by running deinit() on the datastructure returned (a standard convention), or, if the function returns a pointer, using the same allocator you passed in to free the returned buffer. C's problem here is it doesn't have namespaces or member functions, so there's a mix of conventions for what the freeing function should be called.

    C++ allows this as well for standard library containers, although I've rarely seen it used.

    > Also, say a DLL function returns a char pointer containing a string. How would you know whether to call free or delete on it? Or, maybe, the equivalent of free in Frob, the language that DLL happens to be written in?

    I have to concede this one. I can't see a way out of this other than documentation.

    [1]: https://ziglang.org/

  • sciter

    Sciter: the Embeddable HTML/CSS/JS engine for modern UI development

    Do we need to free the string? And if "yes" then how? It is not realistic to provide free** for each such API function...

    In Sciter API (https://sciter.com) I am solving this by callback functions:

        typedef void string_receiver(const char* s, size_t slen, void* tag);

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts