c++/4181: mismatch in size of empty class

kirk@kaybee.org kirk@kaybee.org
Thu Aug 30 13:46:00 GMT 2001


>Number:         4181
>Category:       c++
>Synopsis:       mismatch in size of empty class
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Aug 30 13:46:01 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     kirk@kaybee.org
>Release:        2.96 20000731
>Organization:
>Environment:
Red Hat Linux 7.1
>Description:
The size of an empty class is given to be 1 byte.
However, an object derived from the empty class that contains
a 4-byte variable, has a reported size of 8 bytes.

This disparity in object sizes might be a bug in itself.
However, I noticed one severe problem as a result of this problem.
Looking at a running program, I found that 8 bytes of memory
is indeed allocated for the object.  However, I had a perfectly 
valid line of code that accessed a member of the derived
class, but it counted the empty base class (located first
in the memory allocated for the derived object) as 1 byte,
therefore accessing 3 bytes earlier in memory than it should
have, and returning corrupt memory.

Unfortunately, I tried to make a program that simulates 
the exact problem as it occurred, but I can't get it to
happen.  Unfortunately, the original program is much
too big and complicated to submit here.
>How-To-Repeat:
Unfortunately, I tried to make a program that simulates 
the exact problem as it occurred, but I can't get it to
happen.  Unfortunately, the original program is much
too big and complicated to submit here.

However, I hope that the information I provided and
the attached code will help you track down the problem.

Somewhere in gcc, it assumed in one place that the empty object was
only one byte when 4 bytes was actually allocated.  This was
when accessing a data element from a pointer to the derived
object, such as obj->member.  
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: application/octet-stream; name="test2.cpp"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="test2.cpp"

I2luY2x1ZGUgPHN0ZGlvLmg+CgpjbGFzcyBEdW1teSB7Cn07CgpjbGFzcyBUZXN0T2JqIDogcHVi
bGljIER1bW15IHsKICAgIHB1YmxpYzoKICAgICAgICBpbnQqIHB0cjsKCn07CgppbnQgbWFpbiAo
dm9pZCkgewogICAgVGVzdE9iaiogdGVzdDsKICAgIGludCB4OwogICAgdGVzdC0+cHRyID0gJng7
CiAgICBwcmludGYgKCJBY3R1YWwgYWRkcmVzcyBvZiB4OiAlcFxuIiwgJngpOwogICAgcHJpbnRm
ICgiUmV0dXJuZWQgYWRkcmVzcyBvZiB4OiAlcFxuIiwgdGVzdC0+cHRyKTsKICAgIHByaW50ZiAo
InNpemVvZihEdW1teSk6ICVkXG4iLCBzaXplb2YoRHVtbXkpKTsKICAgIHByaW50ZiAoInNpemVv
ZihUZXN0T2JqKTogJWRcbiIsIHNpemVvZihUZXN0T2JqKSk7CiAgICByZXR1cm4gMDsKfQoK



More information about the Gcc-bugs mailing list