in reply to Re^3: OT: Why does malloc always give me 24?
in thread OT: Why does malloc always give me 24? [SOLVED]

”… why…do something silly like that”

It’s not me, the library works like this:

struct _host { int status; str name; }; typedef struct _host host; void host_free(host *); host host_copy(host *); #define T host // <- custom type #include <deq.h> // functions to implement void host_free(host *h) { str_free(&h->name); } host host_copy(host *h) { host _ = *h; _.name = str_copy(&h->name); return _; }

The library then provides:

container_type_push_back container_type_pop_back container_type_push_front container_type_pop_front

…and some more, depending on the container.

If the container is a deq (dequeue) and its type named fubar this would result in deq_fubar_push_back and so on. Instead of a simple push in a well know post-modern language. One may regret this but it is like it is. And I don't know how else to do it. Apart from the fact that I can't write such macros.

Other libraries, like m*lib work similiar - m*lib does this excessively.

Replies are listed 'Best First'.
Re^5: OT: Why does malloc always give me 24?
by NERDVANA (Priest) on Aug 20, 2024 at 06:43 UTC
    Maybe what ikegami meant was that the underscore is not needed. Struct names live in a different namespace, so you can write
    typedef struct host host;

    Of course, I recommend much longer struct names since C namespace is so limiting. I usually name my stuff with a project identifier prefix, like struct myproj_host. I find CamelCase and lowercase equally appealing for the prefix and object names, but tend to stick to one pattern per project. I often put a suffix of _t on my typedefs, and _p on pointer typedefs, but that would also make the CTL library generate ugly code. Whoops I just looked it up, and "_t" are reserved. So I guess I won't do that anymore. Sometimes I don't declare any typedefs and just live with 'struct' declared everywhere. It isn't pretty, but C is never really pretty and it helps document the code for people less familiar with it. I suppose the typedefs are required for that library you're using though, and with it putting prefixes on your type names for the container name that is only going to look OK with lowercase...

    Maybe if the CTL library were written in cpppp it would be easier to parameterize the output :-)

    (that vector class isn't finished; but maybe CTL would make a good model to base my eventual set of standard templates on)

        If you mean multiple threads calling into the same data structure, I don't blame them. That's a mess of platform complications and potential runtime overhead. I've pulled out enough hair over that model of programming to permanently switch to "each thread gets its own data, and they exchange ownership of the whole structure via passing a pointer through a pipe". This model plays very nicely with Perl, too.

        Thanks for the tips on additional libraries to investigate.