Bug 58451 - gcc-2.96 misallocates autosized array of structs
Summary: gcc-2.96 misallocates autosized array of structs
Status: CLOSED CURRENTRELEASE
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: gcc (Show other bugs)
(Show other bugs)
Version: 7.2
Hardware: i686 Linux
medium
medium
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact: Brian Brock
URL:
Whiteboard:
Keywords:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2002-01-16 23:19 UTC by Mark Maimone
Modified: 2007-04-18 16:39 UTC (History)
0 users

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


Attachments (Terms of Use)

Description Mark Maimone 2002-01-16 23:19:11 UTC
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 16:56:08 UTC
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 19:08:45 UTC
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.