public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/32141]  New: default init doesn't work in ctor initializer list
@ 2007-05-29 16:50 james dot kanze at gmail dot com
  2007-06-10  2:18 ` [Bug c++/32141] " pinskia at gcc dot gnu dot org
  0 siblings, 1 reply; 2+ messages in thread
From: james dot kanze at gmail dot com @ 2007-05-29 16:50 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2440 bytes --]

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/32141] default init doesn't work in ctor initializer list
  2007-05-29 16:50 [Bug c++/32141] New: default init doesn't work in ctor initializer list james dot kanze at gmail dot com
@ 2007-06-10  2:18 ` pinskia at gcc dot gnu dot org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2007-06-10  2:18 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2007-06-10 02:18 -------


*** This bug has been marked as a duplicate of 30111 ***


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |DUPLICATE


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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2007-06-10  2:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-05-29 16:50 [Bug c++/32141] New: default init doesn't work in ctor initializer list james dot kanze at gmail dot com
2007-06-10  2:18 ` [Bug c++/32141] " pinskia at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).