79124304

Date: 2024-10-25 04:59:42
Score: 1.5
Natty:
Report link

#define MY_SIZEOF(type) (unsigned int)( (type*)0 + 1)

cast the NULL(0) ptr to the type, and add one. Because of pointer arithmetic, the compiler would increment the size according the type. Then cast it back to an unsigned integer, as we want to represent the size.

Also:

#define my_sizeof(x) ((char *)(&x + 1) - (char *)&x)

Relies on the variable of type being defined.

Credit: @Neo21 on discord. Mine just has the cast to an unsigned int.

Reasons:
  • Has code block (-0.5):
  • User mentioned (1): @Neo21
  • Low reputation (1):
Posted by: Ashwath G