Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.

Public Report
Report From: Delphi-BCB/Compiler/C++/Front End/Initialization    [ Add a report in this area ]  
Report #:  51854   Status: Closed
Value-initialization: POD struct should be zero-initialized
Project:  C++Builder Build #:  Borland C++ 5.9
Version:    11.0 Submitted By:   Niels Dekker
Report Type:  Crash / Data loss / Total failure Date Reported:  9/11/2007 11:24:03 PM
Severity:    Serious / Highly visible problem Last Updated: 3/20/2012 2:24:39 AM
Platform:    All platforms Internal Tracking #:   255377
Resolution: Fixed (Resolution Comments) Resolved in Build: : 12.0.3140.16150
Duplicate of:  None
Voting and Rating
Overall Rating: (1 Total Rating)
5.00 out of 5
Total Votes: 20
Description
When testing boost::value_initialized on a Borland compiler, it appeared to me that the compiler does not correctly implement value-initialization.

Suppose we have a very simple POD struct, TData:

  struct TData {
    int i;
  };

Suppose TData is itself wrapped inside another struct, TWrapper, defined as follows:

  struct TWrapper {
    TData data;
    TWrapper() : data() {}
  };

The data should be zero-initialized by the constructor of TWrapper.  Unfortunately, when using Borland C++, the following assert typically fails:

  TWrapper wrapper;
  assert(wrapper.data.i == 0);

Nicola Musatti (http://bcbboost.sourceforge.net) did the test on C++Builder 2007 Update 2 (compiler version 5.91).  I tested the free Turbo C++ Explorer (compiler version 5.82).  Both compiler versions appear to have the bug.

--
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Scientific programmer at LKEB, Leiden University Medical Center
Steps to Reproduce:
bcc32 qc51854.cpp
qc51854

Exp: Run without error
Act: asssert fails


#include <cassert>

  struct TData {
    int i;
  };

  struct TWrapper {
    TData data;
    TWrapper() : data() {}
  };

  int main() {
    TWrapper wrapper;
    // Assert should not fails, but it does!
    assert(wrapper.data.i == 0);
    return wrapper.data.i;
  }
Workarounds
Boost developer Fernando Cacciola and I hope to have a Borland-specific workaround for boost::value_initialized, in the near future. http://www.boost.org/libs/utility/value_init.htm
Attachment
qc51854.zip
Comments

None

Server Response from: ETNACODE01