News for hackers. Stuff that matters.
Re:How to find size of an array in C without sizeof by Tempest1981 on Monday December 26, 2016 @04:16PM (#13259804)
A more detailed article here: http://www.g-truc.net/post-0708.html

with a cleaner way to do _countof using a template in C++ 11.

You can also use the template technique to pass a fixed size array to a function, and have the function determine the array size (without needing a 2nd length param, or null terminator element). Similar to strcpy_s(): http://stackoverflow.com/questions/23307268/how-does-strcpy-...

MSVC has a built in _countof: http://stackoverflow.com/questions/4415530/equivalents-to-ms...

Re:How to find size of an array in C without sizeof by Stratoscope on Monday December 26, 2016 @06:08PM (#13260264)
Thanks for the interesting references!

While we're talking macros, anyone who reads the g-truc.net article should feel itchy after seeing the countof macro in their example:

  #define countof(arr) sizeof(arr) / sizeof(arr[0])
Two problems here:

1. The last use of 'arr' doesn't have 'arr' wrapped in parenthesis.

2. The entire expression is not wrapped in parentheses either.

If you write a macro that does any calculation like this, play it safe and put parens around every macro argument and parens around the entire expression too. Otherwise you never know what operator precedence will do to you.

Re:How to find size of an array in C without sizeof by Tempest1981 on Tuesday December 27, 2016 @03:20PM (#13265891)
Great point, about proper macro defines.

And use do/while wrappers (without a trailing semicolon) where needed: https://kernelnewbies.org/FAQ/DoWhile0