[cfe-dev] sizeof(sizeof(...))

Ted Kremenek kremenek at apple.com
Fri Dec 14 16:43:50 PST 2007


On Dec 14, 2007, at 4:27 PM, Pierre Habouzit wrote:

> On Fri, Dec 14, 2007 at 08:30:59PM +0000, Ted Kremenek wrote:
>> Does anyone have any insights on the semantics of expressions of the
>> form sizeof(sizeof(...))?  For example, the following is legal code:
>>
>> int baz(int x) {
>>  typedef int a[f()];
>>  return sizeof (sizeof(a[bar(x)]));
>>         + sizeof(sizeof(x));
>> }
>
> I don't get the reason why someone would do that, but afaict sizeof  
> is a
> size_t (not an unsigned int) so sizeof(sizeof(a[bar(x)])) is just
> sizeof(size_t). I don't have my C norm at hand, but at least gcc
> believes that sizeof "returns" a size_t since its printf format  
> checker
> complains if I use "%d" and definitely wants "%zd".

Right.  IIRC, size_t can be target specific.  On Mac OS X (x86), clang  
reports the type of sizeof() as being unsigned long (I believe I may  
have previously said unsigned int).



More information about the cfe-dev mailing list