1

What it the initial values of an array declared in the heap. it's a zero or random values in memory?

int* ptrAr;
ptrAr = new int[5];
Omar Sherif
  • 649
  • 9
  • 26
  • 1
    Related: https://stackoverflow.com/q/2468203/2602718 – scohe001 May 07 '19 at 19:37
  • 2
    Random values in memory. Although it can be 0 the first time because your OS probably zero's new pages it gives your application (for security purposes not to leak information from some other application). If you reuse memory from the heap it will contain garbage values from your application. – drescherjm May 07 '19 at 19:37
  • 2
    Unfortunately none of the "duplicate answer" are pertinent. In the case of dynamic initialization, default initialization does not cause zero initialization. The consequence is that your array hold something much more horrible than a random value: an **indeterminate value**. Trying to operate on such values causes undefined behavior see [c++-standard/\[dcl.init\]](https://timsong-cpp.github.io/cppwp/n4659/dcl.init#12) – Oliv May 07 '19 at 19:47
  • @πάντα None of the "duplicate answers" sites **indeterminate value** which what holds the array after this initialization! Which is the answer of this clear question. – Oliv May 07 '19 at 19:54

0 Answers0