I did some searching on this topic but couldn't find anything definitive.

Are basic types automatically initialized to 0 in C++ when allocated via new ? For example, are all values of nums guaranteed to be 0 in the code below? int *nums = new int[10];

Dani AI

Generated

Short answer: no — plain new without explicit value-initialization leaves built-in types with indeterminate contents, so the “garbage” saw is normal. The language defines a separate concept, value-initialization, which (for arrays) ensures each element is value-initialized and, for scalar types, that means zero-initialized. See the C++ rules for the new-expression and value-initialization for the formal statement: new (expression) and value initialization.

Reading or using an indeterminate value is undefined behavior, so don’t rely on whatever bytes happen to be there; tools like ASan/Valgrind will catch uses of uninitialized memory. ’s suggestion to initialize after allocation (for example with std::fill_n) is correct and portable.

Prefer RAII and explicit initialization in real code. A clear, idiomatic option is to use a standard container with an explicit initializer:

#include <vector>

std::vector<int> v(10, 0);  // ten zeros, guaranteed

That avoids manual new[]/delete[] and expresses intent.

Regarding ’s caution about compiler dependence: the behaviors above are specified by the standard — conforming compilers implement them the same way. If a build shows surprising values, check for debug-run memory fills (debug runtimes sometimes poison or clear memory), nonconforming toolchains, or real memory-corruption bugs. For authoritative details consult the linked pages on value-initialization and the new-expression.

Recommended Answers

All 7 Replies

I guess not - it still has garbage.

Did this test.

#include <cstdlib>
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    int *nums = new int[10];
    
    for(int i=0;i<10;++i) cout << i << ": " << nums[i] << endl;
    
    delete [] nums;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

but like everything in c/c++ I'm sure it's compiler-dependant.

In C and C++, nothing is automatically initialized. (I'm pretty sure.)

You can use the STL fill_n() function to initialize it for you:

#include <algorithm>  // fill_n() and copy()
#include <iostream>   // cout, of course
#include <iterator>   // ostream_iterator<>
#include <limits>     // numeric_limits<>

using namespace std;

int main()
  {
  int *nums = new int[ 10 ];

  cout << "Before initialization: ";
  copy( nums, nums +10, ostream_iterator <int> ( cout, " " ) );
  cout << endl;

  fill_n( nums, 10, 42 );

  cout << "After initialization: ";
  copy( nums, nums +10, ostream_iterator <int> ( cout, " " ) );
  cout << endl;

  cout << "Press ENTER to finish";
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

  delete[] nums;
  return 0;
  }

Hope this helps.

It needs always to be initialized otherwise its containt will be undefined.

When you define an array. The compiler only initializes the starting address of it on the stack. It only reserves the memory space as per the size specified.

>Are basic types automatically initialized to 0 in C++ when allocated via new ?
No, automatic default initialization of built-in objects doesn't happen most of the time. You'll see it with objects that have static storage duration, typically. But you can force default initialization with an explicit empty parameter list:

int *nums = new int[10]();

But you can force default initialization with an explicit empty parameter list:

int *nums = new int[10]();

That is a neat trick. Thanks.

That is a neat trick. Thanks.

No!!

It may not work properly on all the compilers. Don't do something which is undefined it may create problems in the long run.

>It may not work properly on all the compilers.
Please quote chapter and verse from the standard that proves this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.