Thread: C Library API design (memory management)

  1. #1
    Registered User
    Join Date
    May 2022
    Posts
    2

    C Library API design (memory management)

    Hi,

    Can anyone guide me on the following question. What is the best practices concerning designing C library API which works with structures, in terms of memory management and initialization.

    I.e. for structure "foo", which one (and why) is more preffered:

    1.
    struct foo* foo_create(...);
    void foo_destroy(struct foo* p);

    2.
    void foo_init(struct foo* p);
    void foo_cleanup(struct foo* p);

    3. Combination of 2 & 3.

    Thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,439
    Unless you have a good reason for separating create from init, then "create is init" and "cleanup is destroy" is a much simpler interface.

    For a start, you don't have to worry about garbage instances of foo that were allocated, but the programmer forgot to call init.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2022
    Posts
    2
    Quote Originally Posted by Salem View Post
    Unless you have a good reason for separating create from init, then "create is init" and "cleanup is destroy" is a much simpler interface.
    No, I have no such reasons. My only concern is usability and (may I call it as) "a common approach".
    Is it alright to force clients create foo on heap? I don't know how they'll use it. I mean these use-cases:
    1.
    struct foo* my_foo = foo_create(...);
    ...
    foo_destroy(my_foo)

    2.
    struct foo my_foo;
    foo_init(&my_foo);
    ...
    foo_cleanup(&my_foo);

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,466
    I would say the second is better since then the user can choose to create the object on the stack or the heap.
    It should have a return type of int or possibly bool to indicate success/failure.
    Better names would be foo_create and foo_destroy.
    The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts. - Bertrand Russell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ library management!!
    By [Student] in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2011, 09:53 PM
  2. memory management
    By hansigarten2 in forum C Programming
    Replies: 10
    Last Post: 08-19-2010, 01:22 PM
  3. Memory management - How/why/when
    By micke_b in forum C Programming
    Replies: 29
    Last Post: 11-07-2007, 12:26 PM
  4. Library Management Coding
    By sunilplayer in forum C++ Programming
    Replies: 2
    Last Post: 07-28-2007, 08:14 AM
  5. how to design a CMS (content management system) in C++ ?
    By maulikpatel in forum C++ Programming
    Replies: 1
    Last Post: 06-07-2003, 08:37 PM

Tags for this Thread