-3

EDIT: ^^^ "duplicate" doesn't mention arrays at all
EDIT2: Hold on that's in C, not C++, isn't there a difference between 2 languages ?!

This question has been bugging me for some time lately. Google search revealed nothing.

So I have this snippet of example C++ code:

int factors[100]; /* note this is not initialized */
int number = /* less than 100 */ 10;
for (int i = 0; i < number; i ++) {
    factors[i] = 1;
}
for (int i = 0; i < 100; i ++) {
    std::cout << factors[i] << std::endl;
}

The output is (scroll down to bottom)

1
1
1
1
1
1
1
1
1
1
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
0
0
0
0
0
1640775680
32767
114023525
624860211
174064279
236792104
-1027703263
587262357
1599638600
32767
17
0
1
0
6778984
1
1640935824
32767
1599638352
32767
1640780406
32767
1599638384
32767
1599638384
32767
1
0
1599638408
32767
6778880
1
1640776264
32767
1599638424
32767
0
0
0
0
0
0
0
0
0
0

Why isn't it either ten 1s or ten 1s and ninety 0s, and why are there so many seemingly random (maybe related to powers of 2?) numbers? I think it may have something to do with memory allocation or something but I'm just a beginner and I've not gotten into this stuff yet.

raumaan kidwai
  • 356
  • 1
  • 4
  • 14
  • Just `int factors[100] = {0}`; – 101010 Jun 08 '15 at 21:21
  • @raumaan kidwai Did you show all 100 elements in your post neither forget? – Vlad from Moscow Jun 08 '15 at 21:22
  • 3
    Don't let it bug you. Don't worry, use `std::vector`, and be happy! – Jerry Coffin Jun 08 '15 at 21:22
  • @Jerry Ok, I'll look up a tutorial for that too – raumaan kidwai Jun 08 '15 at 21:23
  • @raumaan kidwai Try to output them in reverse order and see what you will get! – Vlad from Moscow Jun 08 '15 at 21:24
  • @raumaankidwai `Strange numbers when array is not initialized in C++` Those numbers are just integers. They are not strange -- they may be random junk numbers, but each are valid integers. That is what you get when you don't initialize your variables -- random junk numbers that could be anything each and every time you run your app. – PaulMcKenzie Jun 08 '15 at 21:30
  • 1
    @PaulMcKenzie: Even that's not guaranteed. Although the C++ standard doesn't use the term, the C standard discusses values that can do strange things (like cause a signal or shut down your program) just by trying to read an uninitialized value. – Jerry Coffin Jun 08 '15 at 21:34
  • The link you posted had 4 different answers listed to this question. Im sure you could have found the answer .... – Fantastic Mr Fox Jun 08 '15 at 21:38
  • @Ben yes. 1 I did check. The other two have nothing about arrays. – raumaan kidwai Jun 08 '15 at 21:38
  • @raumaankidwai ummmm, [**this one**](http://stackoverflow.com/questions/11812687/why-is-int-array-not-initialized-to-zeros-in-c) ... Is exactly the same as your question. Why an array is not initialized to 0 ... – Fantastic Mr Fox Jun 08 '15 at 21:40
  • @JerryCoffin Yes, you're right. I came across this situation a long time ago, but in that case, it was an array of uninitialized doubles being copied. – PaulMcKenzie Jun 08 '15 at 21:40
  • 2
    @raumaankidwai *""duplicate" doesn't mention arrays at all"* And your observed behavior has nothing to do with arrays and is explained in the dupe. – Baum mit Augen Jun 08 '15 at 21:41
  • @BaummitAugen it isn't explained in the duplicate. – raumaan kidwai Jun 08 '15 at 21:41
  • @Ben No it's not. It's asking why it doesn't work with pointer arrays and the number 10 and whatnot. – raumaan kidwai Jun 08 '15 at 21:42
  • @raumaankidwai Yes it is. You even wrote yourself that the integers are not initialized, then you read them. What happens then is explained in the dupe. – Baum mit Augen Jun 08 '15 at 21:43
  • @raumaankidwai Did you actually read the question and answer ... Or the Duplicate? Both answer your question, your problem has nothing to do with arrays, it is default initialization that you need to worry about. – Fantastic Mr Fox Jun 08 '15 at 21:43
  • 2
    @BaummitAugen, i think it is time to give up here. Those who wont be taught cannot be taught .... – Fantastic Mr Fox Jun 08 '15 at 21:44
  • *EDIT2: Hold on that's in C, not C++, isn't there a difference between 2 languages ?!* ... Not in this case. – Fantastic Mr Fox Jun 08 '15 at 21:46
  • @BaummitAugen no it doesn't. The duplicate is asking about pointers and number 10 and why doesn't it work ONLY with specific numbers and the answer to it goes explaining about signals and mallocs and compiler crashes and whatnot. When I run the situation in the duplicate, my compiler gives an error. – raumaan kidwai Jun 08 '15 at 21:46
  • @Ben _isn't there a difference between 2 languages ?! ... Not in this case._ What? – raumaan kidwai Jun 08 '15 at 21:49
  • @raumaankidwai c++ and c share commonalities and have differences. In this case c and c++ both default initialize in the same way. – Fantastic Mr Fox Jun 08 '15 at 21:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79995/discussion-between-ben-and-raumaan-kidwai). – Fantastic Mr Fox Jun 08 '15 at 21:52

3 Answers3

4

If you have the declaration

int factors[100]; /* note this is not initialized */

there are two situations:

  1. When declared as a global (file scope) variable, the entire array will be initialised to zeros before your program starts.

  2. When declared as a local (function scope) variable, the array is not initialised and will contain unpredictable numbers.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Ok, so if I want to prevent this I can just move all my arrays to the outside of `int main () { }` ? – raumaan kidwai Jun 08 '15 at 21:22
  • Yes, that would make them global variables, and therefore initialised to zeros. – Greg Hewgill Jun 08 '15 at 21:22
  • 1
    @raumaankidwai Please don't do that. You should explicitly initialize them to zeros if that's what you need. – Apples Jun 08 '15 at 21:23
  • @AlchemicalApples yeah that's what I usually do, but I was wondering why this is the case – raumaan kidwai Jun 08 '15 at 21:24
  • @raumaankidwai I will add that doing `int factors[100] = {0};` is sufficient to initialize the whole array to 0s. In C++, when you initialize even one element of array, the whole array will get initialized to default values for given type – Creris Jun 08 '15 at 21:24
  • @Creris yeah that's what I usually do, but I was wondering why this is the case – raumaan kidwai Jun 08 '15 at 21:27
  • @raumaankidwai don't try Creris's trick to initialize to anything but zero. `int factors[100] = {1};` will give one 1 and I'm pretty sure the other 99 are set to zero, but I don't know if the standard guarantees this. – user4581301 Jun 08 '15 at 21:30
  • @user4581301 I've tried it myself and `int factors[100] = {0};` does set everything to zero. – raumaan kidwai Jun 08 '15 at 21:31
  • 2
    C++ committee decided that they will not require the compiler to make things that take time when it doesnt need to. so it doesnt need to generate code to initialize your array if you didnt specify the initialization, because it may increase the binary size and tamper the speed of execution a little bit. The same reason as to why new int() returns garbage, and why stack space isnt cleaned when execution goes out of function's scope – Creris Jun 08 '15 at 21:31
  • @user4581301 I've used that trick myself countless times, it worked every single time, and there is reason it works, it is guaranteed behaviour, no UB there – Creris Jun 08 '15 at 21:32
  • 2
    `int factors[100] = {};` default initialises everything to `0`. Having that first `0` there only determines what the first number will be. You could do `int factors[100] = {5};` which would make `factors[0] == 5` and everything else `0` – Tas Jun 08 '15 at 21:33
  • Thanks @Tas . That what I saw when I played around with it, but wasn't sure if it was non-standard and GCC being nice. – user4581301 Jun 08 '15 at 21:37
3

The uninitialized arrays are filled with garbage values.Garbage values are those values present in that specific memory location before the user requests for it.The memory location have always existed.In many cases the output is 0 as compiler explicitly writes defualt values before returning these locations.But this behaviour is not always exhibited by C/C++ compilers,hence the presence of a varied output.

Anony-mouse
  • 2,041
  • 2
  • 11
  • 23
1

Thats just the thing, if you don't initialize your arrays, C++ does not guarantee it will be blank

Ishay Peled
  • 2,783
  • 1
  • 23
  • 37
  • But why is that the case? – raumaan kidwai Jun 08 '15 at 21:20
  • Because changing their values takes time, valuable and wasted time if you were gonna overwrite them anyways – Dleep Jun 08 '15 at 21:22
  • It is important to note that variables in static storage will be initialized to zeros. – Apples Jun 08 '15 at 21:22
  • Because the OS allocated memory for you code. It does not set any value to it. Say you had the value 3 in memory address 0xf000000 in use for a previous running software, then that software stops and your software gets the same address. The value there is still 3 – Ishay Peled Jun 08 '15 at 21:22