array initialization in C++

March 1st, 2010 by xrigher Leave a reply »

An important point about array initializaiton in C++ is that a static array won’t assign default values to it’s elements, but a dynamic one will. At least it’s like this with g++.

Here is an example:

int main(int argc, char* argv[]) {
  size_t n(100000);

  // This works
  unsigned short *ords;
  ords = new unsigned short[n];
  // or
  //ords = new unsigned short[n]();

  // This doesn't work
  //unsigned short ords[n];

  bool noProblem = true;
  for(size_t i = 0; i != n; ++i) {
    if(ords[i] != 0) {
      noProblem = false;
    cout << "No problem" << endl;
    cout << "It doesn't work" << endl;



Leave a Reply