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

Yes sure.

Unfortunately e.g. ctl then ends up with something like:

… struct _host { int status; str name; }; typedef struct _host Host; // <- … deq_Host_push_back(&gizmo, (Host){301, str_init("joearms.github.io")}) +;

Instead of deq_host_push_back in case of lowercase.

Lowercase looks less perverted to me for the moment - but I am capable of learning.

Replies are listed 'Best First'.
Re^3: OT: Why does malloc always give me 24?
by ikegami (Patriarch) on Aug 19, 2024 at 04:09 UTC

    First of all, that's not what I recommended. You still naming the type struct _host which makes no sense.

    As for putting an uppercase "Host" in the middle of a function name, why would would do something silly like that. Or do you mean Host_push_back akin to Perl's Host::push_back? Definitely a gain since there's no equivalent to ::. Different things should look different.

    And you didn't say what you'd use instead of Host host;. What would the following look like in your system?

    Host host = Host_new();

      ”… 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.

        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)