Re: Safer determination of static array size

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Aug 9, 2013 at 8:36 AM, Florian Weimer <fweimer@xxxxxxxxxx> wrote:
> A lot of programs contain a definition like the following one:
>
> #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*a))
>
> If this is applied to a pointer instead of an array, this still compiles and
> the result is wrong.
Microsoft offers _countof, available in Visual Studio 2005 and above
[0], because of this problem. A C++ program using _countof will fail
to compile under C++ if array is a pointer.

Here's Microsoft's definition:

extern "C++"
{
template <typename _CountofType, size_t _SizeOfArray>
char (*__countof_helper(UNALIGNED _CountofType
(&_Array)[_SizeOfArray]))[_SizeOfArray];
#define _countof(_Array) (sizeof(*__countof_helper(_Array)) + 0)
}

In C, it will still produce erroneous results.

Jeff

[0] http://msdn.microsoft.com/en-us/library/ms175773.aspx




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux