std::array does not 0-initialise its members (per standard)

Bug #1284973 reported by Volodya
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
gcc-defaults (Ubuntu)
New
Undecided
Unassigned

Bug Description

Compiling the following example code:

#include <iostream>
#include <array>

int main(int argc, char* argv[])
{
 char a1[10];
 std::array<char, 10> a2;

 for(int i=0; i<10; i++)
 {
  std::cout << "a1[" << i << "] = " << (int)a1[i] << std::endl;
 }
 for(int i=0; i<10; i++)
 {
  std::cout << "a2[" << i << "] = " << (int)a2[i] << std::endl;
 }
}

compiling with:
g++ -std=c++11 arrays.cpp

produces the following:
$ ./a.out
a1[0] = -36
a1[1] = -101
a1[2] = -13
a1[3] = -65
a1[4] = 85
a1[5] = -118
a1[6] = 70
a1[7] = -73
a1[8] = -16
a1[9] = -43
a2[0] = 113
a2[1] = -73
a2[2] = 0
a2[3] = 0
a2[4] = 0
a2[5] = 0
a2[6] = -37
a2[7] = -119
a2[8] = 4
a2[9] = 8

Somehow at least some of the members of std::array retain their non-zero values, which goes against C++11 standard.

Interesting to note that four values do get zero-filled (regardless of the size of the array) on a reasonably large array, but not others. It may, of course, but just a lucky fluke.

ProblemType: Bug
DistroRelease: Ubuntu 13.10
Package: g++ 4:4.8.1-2ubuntu3
ProcVersionSignature: Ubuntu 3.11.0-17.31-generic 3.11.10.3
Uname: Linux 3.11.0-17-generic i686
ApportVersion: 2.12.5-0ubuntu2.2
Architecture: i386
Date: Wed Feb 26 08:48:34 2014
InstallationDate: Installed on 2013-12-22 (65 days ago)
InstallationMedia: Ubuntu 13.10 "Saucy Salamander" - Release i386 (20131016.1)
MarkForUpload: True
SourcePackage: gcc-defaults
UpgradeStatus: No upgrade log present (probably fresh install)

Revision history for this message
Volodya (volodya) wrote :
Revision history for this message
Volodya (volodya) wrote :

std::array<char, 10> a2 = {}; // this zero-fills the array

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.