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

Tip: Using typedef to create aliases of pointer types is generally frowned upon, especially when the name doesn't make it clear it's a pointer type.

Better:

typedef struct acme { int data; } acme;

Taking this one step further, I wouldn't use an all-lowercase name so I could do something like Acme acme;.

Replies are listed 'Best First'.
Re^2: OT: Why does malloc always give me 24?
by karlgoethebier (Abbot) on Aug 18, 2024 at 19:18 UTC

    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.

      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.