Re: sizeof on arrays and pointers

From: Doug Harrison [MVP] (dsh_at_mvps.org)
Date: 01/17/05


Date: Mon, 17 Jan 2005 10:29:10 -0600

BRG wrote:

>The following program produces different results on the five different
>compilers I am using:
>
>---------------------------------
>#include <stdio.h>
>typedef char ctype[100];
>int main()
>{ char x[100], *y;
> ctype c;
> printf("\n%3ld %3ld", sizeof(x), sizeof(&x));
> printf("\n%3ld %3ld", sizeof(y), sizeof(&y));
> printf("\n%3ld %3ld", sizeof(c), sizeof(&c));
> return 0;
>}
>---------------------------------
>
>These results are as follows:
>
>Compiler 1 Compiler 2 Compiler 3 Compiler 4 Compiler 5
>100 100 100 100 100 4 100 2 100 100
> 4 4 4 4 4 4 2 2 4 4
>100 100 100 100 100 4 100 2 100 100
>
>VC++ produces the first of these and is claimed to be wrong by some who
>think that the right answer is number three.
>
>Is this a bug in VC++?

Yes. The type of the expressions &x and &c above is char (*)[100], or
pointer to array of char of size 100. This bug also affects the Whidbey
beta, so you might want to go here and report it:

http://lab.msdn.microsoft.com/vs2005/default.aspx

It might also be interesting to print typeid(&x).name(), e.g.

// a.cpp

#include <stdio.h>
#include <typeinfo>

int main()
{
   char x[100];
   printf("sizeof(%s) = %d\nsizeof(%s) = %d\n",
         typeid(x).name(),
         (int) sizeof(x),
         typeid(&x).name(),
         (int) sizeof(&x));
   return 0;
}

Here is a copy of my Whidbey beta compile session and output for this
program:

C>cl -W4 a.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.41202 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

a.cpp
Microsoft (R) Incremental Linker Version 8.00.41202
Copyright (C) Microsoft Corporation. All rights reserved.

/out:a.exe
a.obj

C>a
sizeof(char [100]) = 100
sizeof(char (*)[100]) = 100

-- 
Doug Harrison
Microsoft MVP - Visual C++


Relevant Pages

  • Re: a little mistake
    ... Also the use of "inline" for that function is redundant. ... compilers will do it anyways and it isn't portable across all compilers ... int main ... Calling function would be part of the users code. ...
    (comp.lang.c)
  • Re: It works without stdio.h !!
    ... most C compilers will assume that a function that has no prototype is an ... extern int, and since you only specify functions that live in the C library, an unresolved ... never declare a variable of type char unless you well and truly believe you need to be ... strcpy, strcat, sprintf etc. with it. ...
    (microsoft.public.vc.mfc)
  • Re: Hundreds of cases in a switch, optimization?
    ... > In terms of generated machine code, how does hundreds of cases in a switch ... Do compilers or processors do any ... extern int f; ... compiled both with and without optimisation. ...
    (comp.lang.c)
  • Re: K&R-Style Function Declarations: Good or Bad?
    ... > short parameters getting promoted to int. ... all pre-ANSI compilers widened char and short ... declarations -- some compilers converted the widened types back to the ...
    (comp.lang.c)
  • Re: Bit-related question
    ... Subtraction is addition of a number that has been negated. ... complement signed ints, negation consists of adding one to the one's ... int main ... Copyright Microsoft Corporation 1984-2002. ...
    (microsoft.public.vc.language)