Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Compiler bug?

Reply
Thread Tools

Compiler bug?

 
 
Enrico `Trippo' Porreca
Guest
Posts: n/a
 
      06-17-2004
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

 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      06-17-2004
Enrico `Trippo' Porreca <(E-Mail Removed)> 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 ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"My absolute aspect is probably..."
- Mato Valtonen
 
Reply With Quote
 
 
 
 
Enrico `Trippo' Porreca
Guest
Posts: n/a
 
      06-17-2004
Joona I Palaste wrote:
> Enrico `Trippo' Porreca <(E-Mail Removed)> 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

 
Reply With Quote
 
Jeremy Yallop
Guest
Posts: n/a
 
      06-17-2004
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...ing.google.com
http://groups.google.com/groups?selm...mindspring.com

Jeremy.
 
Reply With Quote
 
Enrico `Trippo' Porreca
Guest
Posts: n/a
 
      06-17-2004
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...ing.google.com
> http://groups.google.com/groups?selm...mindspring.com


Thanks.

--
Enrico `Trippo' Porreca

 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      06-18-2004
In <(E-Mail Removed)> Enrico `Trippo' Porreca <(E-Mail Removed)> 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: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiler Error Message: The compiler failed with error code -1073741819 Ram ASP .Net 0 09-13-2005 09:52 AM
Why is a JIT compiler faster than a byte-compiler RickMuller Python 4 03-26-2005 04:30 PM
Compiler compiler with C++ as output Andrey Batyuck C++ 3 05-17-2004 08:17 PM
Can we use <compiler> tag to avoid RunTime Compiler error? Jack Wright ASP .Net 5 01-19-2004 04:36 PM
Compiler Error Message: The compiler failed with error code 128. Yan ASP .Net 0 07-21-2003 10:49 PM



Advertisments