# I wonder why initailizing an array got an unexpected result

**URL:** <https://root-forum.cern.ch/t/i-wonder-why-initailizing-an-array-got-an-unexpected-result/52499>\
**Category:** ROOT\
**Tags:** root\
**Created:** [November 24, 2022, 8:12am UTC](https://root-forum.cern.ch/t/i-wonder-why-initailizing-an-array-got-an-unexpected-result/52499 "2022-11-24T08:12:36Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![melodynk](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@melodynk](https://root-forum.cern.ch/u/melodynk)\
**Post date:** [November 24, 2022, 8:12am UTC](https://root-forum.cern.ch/t/i-wonder-why-initailizing-an-array-got-an-unexpected-result/52499/1 "2022-11-24T08:12:37Z")

</div>

Hi ,all  
I would like to initailize an array in root terminal using this way : int d[40]=100  
but I could not get the correct return result [100, 100, 100, 100, 100…]  
instead get the figure below. I wonder it is normal?  
Many Thanks!

 ![9ea440b9066a82f43d1b3d110a5c254](https://root-forum.cern.ch/uploads/default/original/3X/8/f/8f46b835864df18a791ce6fcd9a064a5cf369c01.png)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 24, 2022, 8:19am UTC](https://root-forum.cern.ch/t/i-wonder-why-initailizing-an-array-got-an-unexpected-result/52499/2 "2022-11-24T08:19:45Z")

</div>

[cppreference.com → Array initialization](https://en.cppreference.com/w/c/language/array_initialization)  
[Uncle Google → C++ initialize all elements of an array](https://www.google.com/search?q=C%2B%2B+initialize+all+elements+of+an+array)

---

<div class="post-metadata">

**Author:** ![melodynk](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@melodynk](https://root-forum.cern.ch/u/melodynk)\
**Post date:** [November 24, 2022, 9:04am UTC](https://root-forum.cern.ch/t/i-wonder-why-initailizing-an-array-got-an-unexpected-result/52499/3 "2022-11-24T09:04:54Z")

</div>

Thanks so much. I know the principle . Another question confusing me . I wonder why a[1] a[2] a[3] printing value 2 below? Thanks so much!

 ![8cc4835e848e86befee76792cd99426](https://root-forum.cern.ch/uploads/default/original/3X/9/c/9cc0a09555623e04e3a610093182ca3c4d20a206.png)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 24, 2022, 9:21am UTC](https://root-forum.cern.ch/t/i-wonder-why-initailizing-an-array-got-an-unexpected-result/52499/4 "2022-11-24T09:21:36Z")

</div>

Which operating system, which ROOT version.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 24, 2022, 9:28am UTC](https://root-forum.cern.ch/t/i-wonder-why-initailizing-an-array-got-an-unexpected-result/52499/5 "2022-11-24T09:28:53Z")

</div>

I do not see this effect with recent ROOT versions (on MacOS):

```auto
% root
   ------------------------------------------------------------------
  | Welcome to ROOT 6.27/01 https://root.cern |
  | (c) 1995-2022, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for macosx64 on Nov 24 2022, 06:43:42 |
  | From heads/master@v6-25-02-2828-g33beda7d0d |
  | With Apple clang version 14.0.0 (clang-1400.0.29.202) |
  | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q' |
   ------------------------------------------------------------------

root [0] int a[40] = {2}
(int [40]) { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
root [1] a[0]
(int) 2
root [2] a[1]
(int) 0
root [3] 

```

---

<div class="post-metadata">

**Author:** ![melodynk](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@melodynk](https://root-forum.cern.ch/u/melodynk)\
**Post date:** [November 24, 2022, 9:56am UTC](https://root-forum.cern.ch/t/i-wonder-why-initailizing-an-array-got-an-unexpected-result/52499/6 "2022-11-24T09:56:10Z")

</div>

The version is 6.24.00. OS is windows 10.The compiler provided by visual studio

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 24, 2022, 9:58am UTC](https://root-forum.cern.ch/t/i-wonder-why-initailizing-an-array-got-an-unexpected-result/52499/7 "2022-11-24T09:58:06Z")

</div>

That’s very old.  
[Take the “Latest Release”.](https://root.cern/install/all_releases/)

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [November 24, 2022, 9:59am UTC](https://root-forum.cern.ch/t/i-wonder-why-initailizing-an-array-got-an-unexpected-result/52499/8 "2022-11-24T09:59:05Z")

</div>

That’s a known issue. See [[cling,win32] Read-after-write of variable of type `double` does not yield the correct result · Issue #9809 · root-project/root · GitHub](https://github.com/root-project/root/issues/9809)

---

<div class="post-metadata">

**Author:** ![melodynk](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@melodynk](https://root-forum.cern.ch/u/melodynk)\
**Post date:** [November 25, 2022, 1:55am UTC](https://root-forum.cern.ch/t/i-wonder-why-initailizing-an-array-got-an-unexpected-result/52499/9 "2022-11-25T01:55:40Z")

</div>

OK,thanks!

---

<div class="post-metadata">

**Author:** ![melodynk](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@melodynk](https://root-forum.cern.ch/u/melodynk)\
**Post date:** [November 25, 2022, 1:57am UTC](https://root-forum.cern.ch/t/i-wonder-why-initailizing-an-array-got-an-unexpected-result/52499/10 "2022-11-25T01:57:41Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [December 9, 2022, 1:58am UTC](https://root-forum.cern.ch/t/i-wonder-why-initailizing-an-array-got-an-unexpected-result/52499/11 "2022-12-09T01:58:34Z")

</div>

Closed on December 9, 2022, 1:58am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
