Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

    This:

    struct Vec3* v = malloc(sizeof(struct Vec3));

    is better written as:

    struct Vec3 * const v = malloc(sizeof *v);
I don't love this. Other people are going to think you're only allocating a pointer. It's potentially confusing.




I also personally find totally confusing leaving the * in the middle of nowhere, like flapping in the breeze.

Where would you put it? The const of the pointer is not the main point, it's just extra clarity that the allocated pointer is not as easily overwritten which would leak the memory.

I put it attached to the variable name when possible, if not attached to the type.

Uh, okay, but if you need to constantly write code as if people reading it don't understand the language, then ... I don't know how to do that. :)

It's not possible to know C code and think that

    sizeof *v
and

    sizeof v
somehow mean the same thing, at least not to me.

no, but you can misread the two interchangeably no matter how familiar you are with the language.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: