Bug 58451 - gcc-2.96 misallocates autosized array of structs
gcc-2.96 misallocates autosized array of structs
Status: CLOSED CURRENTRELEASE
Product: Red Hat Linux
Classification: Retired
Component: gcc (Show other bugs)
7.2
i686 Linux
medium Severity medium
: ---
: ---
Assigned To: Jakub Jelinek
Brian Brock
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2002-01-16 18:19 EST by Mark Maimone
Modified: 2007-04-18 12:39 EDT (History)
0 users

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2002-12-15 14:08:45 EST
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)

  None (edit)
Description Mark Maimone 2002-01-16 18:19:11 EST
From Bugzilla Helper:
User-Agent: Mozilla/4.72 [en] (X11; U; SunOS 5.7 sun4u)

Description of problem:
Vanilla Redhat-7.2 gcc-2.96 on a Dell Latitude C610 notebook
miscompiles an implicitly-sized array of structs when internal braces are
omitted in the initializer.  Instead it allocates too much space, zero pads it,
and causes sizeof() to return an improper response.


Version-Release number of selected component (if applicable):
2.96

How reproducible:
Always

Steps to Reproduce:
CREATE THE SOURCE FILE
> cat foo.c 
#include <stdio.h> 
 
typedef struct { 
  int i, char *s; 
} RecordT; 
 
RecordT foo[] = { 
  1, "1", 
  2, "2", 
  3, "3", 
}; 
 
main() { 
  printf ("Foo is %d bytes, i.e. %d entries each of size %d", 
           sizeof(foo), sizeof(foo) / sizeof(RecordT), sizeof(RecordT)); 
} 
BUILD IT ONCE AS C CODE, THEN AS C++ CODE
RUN BOTH VERSIONS TO SEE THE DIFFERENCES

Actual results:

> gcc foo.c 
> ./a.out 
Foo is 24 bytes, i.e. 3 entries each of size 8 
> cp foo.c foo.cc 
> gcc foo.cc 
> ./a.out 
Foo is 48 bytes, i.e. 6 entries each of size 8. 


Expected Results:  C++ output should be identical to C output.  Instead, the C++
array is sized based on the number of *raw initializers* instead of the number
of *structs* being initialized.  C compiler handles it fine, C++ does not.

Can be "fixed" by adding braces to the initializer, but much old code will break
in the meantime.

Additional info:

http://gcc.gnu.org/gcc-2.96.html makes me wonder why 2.96 is being
provided as the default compiler at all.  Surely 3.0 or even 2.95 would have
been better choices?
Comment 1 Jakub Jelinek 2002-01-17 11:56:08 EST
This was fixed in gcc 3.1 by:
http://gcc.gnu.org/ml/gcc-patches/2001-11/msg00850.html
g++ 3.0.x does get it wrong the same way.
Comment 2 Alan Cox 2002-12-15 14:08:45 EST
We now use g++ 3.2 so closing

Note You need to log in before you can comment on or make changes to this bug.