This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/32141] New: default init doesn't work in ctor initializer list


A POD struct is not correctly initialized when default
initialization is requested in the initialization list of a
constructor, and it is a base class.  Consider the following
program:

-------------------------------------------------

#include <iostream>
#include <memory>
#include <stdlib.h>
#include <string.h>

void*
operator new( size_t n ) throw (std::bad_alloc)
{
    void*               p = malloc( n ) ;
    if ( p == NULL ) {
        throw std::bad_alloc() ;
    }
    memset( p, 0xAA, n ) ;
    return p ;
}

void
operator delete( void* p ) throw ()
{
    free( p ) ;
}

struct POD
{
    short               l ;
    unsigned char       s[ 6 ] ;
} ;

class A : public POD
{
public:
    A() : POD() {}
} ;

class B
{
public:
    B() : myPOD() {}
    POD myPOD ;
} ;

int
main()
{
    A* pA = new A ;
    std::cout.setf( std::ios::hex, std::ios::basefield ) ;
    std::cout << "length A = " << pA->l << std::endl ;
    B* pB = new B ;
    std::cout << "length B = " << pB->myPOD.l << std::endl ;
    return 0 ;
}
-------------------------------------------------
(The replacement new and delete are just to ensure that the
memory doesn't accidentally happen to be correctly initialized.)

According to the standard, "An object whose initializer is an
empty set of parentheses, i.e., (), shall be
default-initialized", and default initialization of a POD type
is zero-initialization.  When compiled, however, this program
outputs:

-------------------------------------------------
length A = aaaa
length B = 0
-------------------------------------------------

The default initialization has correctly taken place for the
member object, but not for the base class.

-- 
James Kanze (GABI Software)             email:james.kanze@gmail.com
Conseils en informatique orientée objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34


-- 
           Summary: default init doesn't work in ctor initializer list
           Product: gcc
           Version: 4.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: james dot kanze at gmail dot com
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32141


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]