Compiler bug?

Discussion in 'C Programming' started by Enrico `Trippo' Porreca, Jun 17, 2004.

  1. It seems to me that the Visual Studio C compiler (both 6.0 and 2003),
    which claims ANSI/ISO conformance, has a nasty bug involving sizes of
    pointers to arrays.

    The following code:

    #include <stdio.h>

    int main(void)
    {
    int a[10];
    int (*p)[10];

    printf("%lu\n", (unsigned long) sizeof &a);
    printf("%lu\n", (unsigned long) sizeof p);
    return 0;
    }

    compiled with the "disable extensions" flag, produces this output:

    40
    4

    I'm pretty sure that a conforming compiler should produce a program
    which prints the same number two times, since &a and p are of the same
    type. Right?

    --
    Enrico `Trippo' Porreca
    Enrico `Trippo' Porreca, Jun 17, 2004
    #1
    1. Advertising

  2. Enrico `Trippo' Porreca <> scribbled the following:
    > It seems to me that the Visual Studio C compiler (both 6.0 and 2003),
    > which claims ANSI/ISO conformance, has a nasty bug involving sizes of
    > pointers to arrays.


    > The following code:


    > #include <stdio.h>


    > int main(void)
    > {
    > int a[10];
    > int (*p)[10];


    > printf("%lu\n", (unsigned long) sizeof &a);
    > printf("%lu\n", (unsigned long) sizeof p);
    > return 0;
    > }


    > compiled with the "disable extensions" flag, produces this output:


    > 40
    > 4


    > I'm pretty sure that a conforming compiler should produce a program
    > which prints the same number two times, since &a and p are of the same
    > type. Right?


    Yes, I think there is something wrong here. sizeof a should be 10 *
    sizeof(int), which is most likely 40 on your system, but sizeof &a
    should be identical to sizeof(int(*)[10]), which is equivalent to
    sizeof p.
    I tried it on gcc on my Linux system and it printed 4 for both.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "My absolute aspect is probably..."
    - Mato Valtonen
    Joona I Palaste, Jun 17, 2004
    #2
    1. Advertising

  3. Joona I Palaste wrote:
    > Enrico `Trippo' Porreca <> scribbled the following:
    >
    >>It seems to me that the Visual Studio C compiler (both 6.0 and 2003),
    >>which claims ANSI/ISO conformance, has a nasty bug involving sizes of
    >>pointers to arrays.

    >
    >>The following code:

    >
    >> #include <stdio.h>

    >
    >
    >> int main(void)
    >> {
    >> int a[10];
    >> int (*p)[10];

    >
    >
    >> printf("%lu\n", (unsigned long) sizeof &a);
    >> printf("%lu\n", (unsigned long) sizeof p);
    >> return 0;
    >> }

    >
    >>compiled with the "disable extensions" flag, produces this output:

    >
    >> 40
    >> 4

    >
    >>I'm pretty sure that a conforming compiler should produce a program
    >>which prints the same number two times, since &a and p are of the same
    >>type. Right?

    >
    > Yes, I think there is something wrong here. sizeof a should be 10 *
    > sizeof(int), which is most likely 40 on your system, but sizeof &a
    > should be identical to sizeof(int(*)[10]), which is equivalent to
    > sizeof p.
    > I tried it on gcc on my Linux system and it printed 4 for both.


    Yes, gcc prints it "right". We had a discussion on it.comp.lang.c about
    the results of that code when compiled with Visual Studio, and I'm
    wondering about an incapacity of cl to distinguish array_name from
    &array_name (at least when one applies sizeof to them).

    --
    Enrico `Trippo' Porreca
    Enrico `Trippo' Porreca, Jun 17, 2004
    #3
  4. Enrico `Trippo' Porreca wrote:
    > It seems to me that the Visual Studio C compiler (both 6.0 and 2003),
    > which claims ANSI/ISO conformance, has a nasty bug involving sizes of
    > pointers to arrays.


    It's been reported here before as a bug in both Visual C and Borland
    compilers. See, e.g.:

    http://groups.google.com/groups?selm=
    http://groups.google.com/groups?selm=

    Jeremy.
    Jeremy Yallop, Jun 17, 2004
    #4
  5. Jeremy Yallop wrote:
    > Enrico `Trippo' Porreca wrote:
    >
    >>It seems to me that the Visual Studio C compiler (both 6.0 and 2003),
    >>which claims ANSI/ISO conformance, has a nasty bug involving sizes of
    >>pointers to arrays.

    >
    > It's been reported here before as a bug in both Visual C and Borland
    > compilers. See, e.g.:
    >
    > http://groups.google.com/groups?selm=
    > http://groups.google.com/groups?selm=


    Thanks.

    --
    Enrico `Trippo' Porreca
    Enrico `Trippo' Porreca, Jun 17, 2004
    #5
  6. Enrico `Trippo' Porreca

    Dan Pop Guest

    In <> Enrico `Trippo' Porreca <> writes:

    >It seems to me that the Visual Studio C compiler (both 6.0 and 2003),
    >which claims ANSI/ISO conformance, has a nasty bug involving sizes of
    >pointers to arrays.
    >
    >The following code:
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > int a[10];
    > int (*p)[10];
    >
    > printf("%lu\n", (unsigned long) sizeof &a);
    > printf("%lu\n", (unsigned long) sizeof p);
    > return 0;
    > }
    >
    >compiled with the "disable extensions" flag, produces this output:
    >
    > 40
    > 4
    >
    >I'm pretty sure that a conforming compiler should produce a program
    >which prints the same number two times, since &a and p are of the same
    >type. Right?


    Right. The Microsoft compiler can't tell the difference between a and &a
    in this context.

    OTOH, I can't see this bug as having any relevance in real life code:
    if you *really* need to know the size of a pointer to array, sizeof p is
    the most natural way of obtaining it: i.e. you use an object of the right
    type as the operand of sizeof, rather than an expression of the right
    type. The usual counterexample is malloc calls, but they involve
    expressions using the unary * operator:

    int *p = malloc(10 * sizeof *p);

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Jun 18, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yan
    Replies:
    0
    Views:
    1,130
  2. Jack Wright
    Replies:
    5
    Views:
    621
    Shiv Kumar
    Jan 19, 2004
  3. Ram
    Replies:
    0
    Views:
    2,827
  4. Andrey Batyuck

    Compiler compiler with C++ as output

    Andrey Batyuck, May 11, 2004, in forum: C++
    Replies:
    3
    Views:
    436
    Frederik Hertzum
    May 17, 2004
  5. RickMuller
    Replies:
    4
    Views:
    702
    Alexey Shamrin
    Mar 26, 2005
Loading...

Share This Page