8
#include <iostream>
using namespace std;

int main() {
    int arr[10] = {};
    for(auto element : arr)
    {
        cout<<element<<" ";
    }
    cout<<endl;
}

if i write int arr[10] = {}, the elements in arr are all 0. but if i just wrtie int arr[10], the elements in arr are random. So i am confused about int arr[10] = {}, i just declare a array int arr[10], but i don't give it any value, just a {}.

4
  • This is just a syntactic sugar, in many situations you need to create array of integers initialized with zeros, Imagine that array size is 1000 instead, aside that sytax only way is to loop thru array and assign 0 to each element manually. Read more about arrays here Commented Mar 10, 2016 at 6:29
  • Now that you have an answer, for kicks, remove the = (but keep the {}) and try it again. int arr[10]{}; Commented Mar 10, 2016 at 6:45
  • @WhozCraig the result is 0 0 0 0 0 0 0 0 0 0 , the = is maybe useless, what's the reason? Commented Mar 10, 2016 at 6:46
  • That would spoil the fun. Do some hunting on SO when you have some spare time and search for more related C++ questions on different initialization methods. And thanks for trying it. Commented Mar 10, 2016 at 6:49

3 Answers 3

7

if i write int arr[10] = {}, the elements in arr are all 0.

That is just how the syntax of the language works. In your case the array will be zero initialized.

but if i just wrtie int arr[10], the elements in arr are random.

In your case array elements are not initialized. You should not read values of variables which are not initialized; otherwise you will trigger undefined behavior.

Sign up to request clarification or add additional context in comments.

5 Comments

actually int arr[10] = {}; will value-initialize. int arr[10]; is default-initialize which means to do nothing for int. (See [dcl.init/7])
Does it only work for basic type(int, float, dobule)? if i define a custom class Person, does Person arr[10] = {} work?
@BlackMamba In that case instance of your object will be default initialized - whatever default initialization means for your class, AFAIK.
@Giorgi To be more precise, they will also be value initialized. This can make a difference in some cases.
@BlackMamba if Person has a constructor then default-initialize means to call that constructor to initialize a Person
2

according to cpp reference website (http://en.cppreference.com/w/c/language/array_initialization) this is how to initialize all array elements to zero:

int a[3] = {}; // invalid C but valid C++ way to zero-out a block-scope array

Comments

0

int arr[10]; is not initializing anything, it's allocating memory blocks the size of an int and you are getting what ever is in that memory.

int arr[10] = {}; is initializing all the int blocks to null/0

4 Comments

That is true for builtin types like int, but the difference between default and value initialized for types with constructors is more complex.
@MartinBonner but he is using an int array in the example and asking why he is getting all 0 or random data. Am I missing something? I get what you are saying but don't think it applies to the question. Would object arr[10] = {} just use the default constructor of that objects?
If there is a default constructor, object arr[10] = {}; and object arr[10]; will both call it. If it's a POD type then each object will be zero initialized in the first case and left indeterminate in the second case.
initializing int blocks to null?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.