Reputation: 1023
I've made a simple program that counts matrices, here's the code:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int result[3] = {0,0,0};
int matrixc[3][6] = {
{0,0,0,0,0,1},
{0,0,0,1,1,1},
{1,0,1,0,0,1}
};
for(int x=0;x <3;x++)
{
for(int y=0;y < 6;y++)
{
result[x] += (matrixc[x][y] * pow(2,6-y));
}
cout << result[x] << endl;
}
}
The output is what I wanted, it is: 2,14,and 82
.
But, when I delete the initialization in the integer array of result
:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int result[3]; //deleted initialization
int matrixc[3][6] = {
{0,0,0,0,0,1},
{0,0,0,1,1,1},
{1,0,1,0,0,1}
};
for(int x=0;x <3;x++)
{
for(int y=0;y < 6;y++)
{
result[x] += (matrixc[x][y] * pow(2,6-y));
}
cout << result[x] << endl;
}
}
I got odd outputs: 1335484418,32618, and 65617
.
Would you like to explain me why would the output be different between an array with and without an initialization?
Actually, I don't want to initialize all result
array, because I have a huge data of matrices.
Is it possible if I use std::vector
without initializing all of the result
array?
Upvotes: 0
Views: 158
Reputation: 109289
When you delete the initialization of the result
array, those locations are initially set to unspecified, arbitrary values. You then add to this arbitrary value within the for
loop, and end up those unexpected results. The results may be completely different the next time you run your program.
Also, since result[x] += ...
reads from an uninitialized variable, deleting the initialization results in your code having undefined behavior.
If you switch over to using a vector
you can zero initialize it as
std::vector<int> result(count); // zero initialized with `count` elements
or
std::vector<int> result; // contains zero elements
result.resize(count); // now it contains `count` elements, all set to 0
Upvotes: 1
Reputation: 171433
Would you like to explain me why would the output be different between an array with and without an initialization?
Seriously? If you don't initialize the array elements they are not initialized!
That means they contains junk data, so when you do result[x] += xxx;
you are adding to junk, so you get more junk.
Actually, I don't want to initialize all "result" array, because I have a huge data of matrices.
Then you shouldn't rely on their initial value being zero.
You can do this:
int result[3] = { }; // initialize all elements to zero
Is it possible if I use std::vector without initializing all of the "result" array?
std::vector
always initializes its members.
Upvotes: 1
Reputation: 158599
As I mentioned in my comment if you do not initialize result
it will have undetermined values. Later on your are then adding a value to an unknown value which will still be an unknown value. In this situation you need to initialize your data, can zero
initialize like so:
int result[3] = {} ;
Upvotes: 1
Reputation: 254731
Would you like to explain me why would the output be different between an array with and without an initialization?
Without initialisation, automatic variables aren't initialised. They will have an indeterminate value, depending on what happened to be in the memory they occupy.
Actually, I don't want to initialize all "result" array, because I have a huge data of matrices.
You can zero-initialise the whole array, even if it's huge, like this:
int result[huge] = {};
although, if it is huge, then it shouldn't be an automatic variable. These are typically kept on the stack, which is typically not huge and liable to overflow if you put too much stuff on it.
Is it possible if I use std::vector without initializing all of the "result" array?
Yes, a vector will zero-initialise its elements by default.
Upvotes: 6
Reputation: 227558
Without the initialization, the result
array contains undetermined values, i.e. values that could be anything that fits into an int
.
Upvotes: 1