4

In C++11 when I allocate a dynamic array using T *array = new T[n]; it's already zeroed (using gcc 4.7.2, Ubuntu 12.10 64bit).

  1. Is this forced by the C++11 specification?
  2. How can one allocate an array without zeroing its items? This should be probably a little bit faster.

Edit: I've checked that for T = int.

gcc cxx-flags: -std=gnu++11 -O3 -ffast-math -fno-rtti

Cartesius00
  • 23,584
  • 43
  • 124
  • 195
  • Might be compiler flags? In Visual Studio this would happen in a debug build but not in release – emartel Nov 27 '12 at 21:15
  • @emartel Well, I checked that, and I build in the release mode. I'll put the flags into the question. – Cartesius00 Nov 27 '12 at 21:19
  • What type is T? The default constructor will be fired for each T; if that constructor just 'zeroes it out' then you'd see this. If T is a POD you're not guaranteed zero initialization with new. – HerrJoebob Nov 27 '12 at 21:20
  • 1
    It is not guaranteed to be zero-initialized. See [this recent related question](http://stackoverflow.com/questions/13529289/how-can-i-free-memory-in-matrix-class/13529735#13529735). – juanchopanza Nov 27 '12 at 21:47

4 Answers4

6

§ 5.3.4

If the new-initializer is omitted, the object is default-initialized (8.5); if no initialization is performed, the object has indeterminate value.

new-initializer is the () in new T[] (), which you have omitted.

§ 8.5 / 6

To default-initialize an object of type T means:

— if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

— if T is an array type, each element is default-initialized;

— otherwise, no initialization is performed.

int[] is default initialized -> each element is default-initialized.

"Is this forced by the C++11 specification?": "no initialization is performed", so no, zeroing is not forced if T has no zeroing constructor (i.e. T is a POD). For T=int, no zeroing has to be performed.

Why is it zero anyway? If your program allocates new memory from the operating system, the OS zeroes the new memory for you. It would be very dangerous, if you could read memory of another program, which possibly stores sensible data. However, if you write into that memory, free it and allocate some of it again, it should not be zeroed.

ipc
  • 8,045
  • 29
  • 33
4

It will not be initialized (still calling the constructor if T is a class, of course). To force value-initialization (which is a name that to newbies can be misleading - let's call it zero-initialization which it is for primitive types like int), do

new T[N]()

That is, just put a pair of parenthesis.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
3

Depends on your actual type T. It might be initialized with zeros under certain conditions. See here: Default initialization in C++

Community
  • 1
  • 1
SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85
2

Even if you can use a C++ feature to dynamically allocate uninitialized memory (std::get_temporary_buffer?), the underlying implementation of malloc() and ::new in your OS's libc is to use an anonymous mmap() for large allocation blocks (where large is a tunable). And anonymous mmap() are always zero initialized.

BatchyX
  • 4,986
  • 2
  • 18
  • 17