top | item 23021474

(no title)

joejev | 5 years ago

Interesting idea, but this implementation has UB:

    typedef void* var;

    struct Header {
      var type;
    };

    // ...

    #define alloc_stack(T) header_init( \
      (char[sizeof(struct Header) + sizeof(struct T)]){0}, T)

    var header_init(var head, var type) {
      struct Header* self = head;
      self->type = type;
      return ((char*)self) + sizeof(struct Header);
    }
The section "struct Header* self = head" is UB. The alignement requirement of the local char array is 1 but the alignment requirement of struct Header is that of void* which is probably 8.

discuss

order

tomp|5 years ago

That's just what I was wondering, are "magic" libraries like this still safe to use considering modern compiler's UB shenanigans?

asveikau|5 years ago

Not only that but you have a pointer to a parameter returned back and used outside its scope ...

nitrogen|5 years ago

It's not a pointer to a parameter, it is just the parameter itself.

var is a typedef for void* and no & appears in the function.