gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Blank int's have random numbers..

#118902 - Dox-999 - Sun Feb 18, 2007 2:39 am

Hi guys I am still learning C++ and I am doing arrays so I decied to practise this by making a table where the user could input 1 number (I know it's not very practical but anyway). When I ran the program the number I input worked but all the other spaces in the table have random numbers in there. This is my code:

Code:
#include <iostream>
using namespace std;
int main()
{
    int x;
    int y;
    int array[2][2];
    cout<<"Insert a number into this table:\n\n  0 1 2\n0[][][]\n1[][][]\n2[][][]";
    cout<<"\n\nEnter (in this order) the x value the y value and the number you wish to input  (e.g. 1 2 62): " ;
    cin>> x >> y >> array[x][y];
    cin.ignore();
    cout<<"["<< array[0][0] <<"]["<< array[0][1] <<"]["<< array[0][2] <<"]\n["<< array[1][0] <<"]["<< array[1][1] <<"]["<< array[1][2] <<"]\n["<< array[2][0] <<"]["<< array[2][1] <<"]["<< array[2][2] <<"]\n";
    cout<<"\nAgain: ";
    cin>> x >> y >> array[x][y];
    cin.ignore();
    cout<<"["<< array[0][0] <<"]["<< array[0][1] <<"]["<< array[0][2] <<"]\n["<< array[1][0] <<"]["<< array[1][1] <<"]["<< array[1][2] <<"]\n["<< array[2][0] <<"]["<< array[2][1] <<"]["<< array[2][2] <<"]\n";
    cin.get();
}


is there a way for all the integers I don't use to just stay blank?

Thanks :)

#118903 - KayH - Sun Feb 18, 2007 3:53 am

1) if you make your variable (array) global they are initialized with zero (but that is only sometimes usefull)
2) you can use a list to initialize while creating (you can use every value you want as start value)
Code:
int m_Array[2][2] = {{0,0},{0,0}},

3) you use a method to fill the arry with values you whish
4) you use:
Code:
__attribute__((section (".sbss"))) int m_Array[SIZE];

5) you can use memset (see also cearn's website or look at the beginners forum)

HTH
KaY

#118917 - HyperHacker - Sun Feb 18, 2007 5:47 am

KayH wrote:
1) if you make your variable (array) global they are initialized with zero (but that is only sometimes usefull)

I don't think this is reliable. In my experience if you've never assigned a value, you can't predict what the value will be.
_________________
I'm a PSP hacker now, but I still <3 DS.

#118922 - tepples - Sun Feb 18, 2007 6:29 am

HyperHacker wrote:
KayH wrote:
1) if you make your variable (array) global they are initialized with zero (but that is only sometimes usefull)

I don't think this is reliable.

Then you're working with unreliable libraries. ISO C specifies that all global variables that aren't given an explicit value will be given the value 0 before main() is called. In GCC, this happens when the startup code zeroes section ".bss". Perhaps you were working with early startup code that didn't properly clear ".bss", or maybe you were using a startup code that skipped some steps if you used AgbMain() instead of main().
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#118932 - Touchstone - Sun Feb 18, 2007 9:58 am

Dox-999 wrote:
Hi guys I am still learning C++ and I am doing arrays so I decied to practise this by making a table where the user could input 1 number (I know it's not very practical but anyway). When I ran the program the number I input worked but all the other spaces in the table have random numbers in there.

The reason for this is because the array you get is simply pointing to somewhere in memory where other variables used to be and since you are not explicitly setting every entry in your array to 0 they will remain whatevery value is in that memory location.

I think the easiest way to set every entry in your array is by doing:
Code:
int MyArray[2][2] = {0};


On the other hand, it's not necessarily a problem that the array isn't initialized. If you allocate an array and every entry is a random value to begin with, but your code iterate through every entry and set it to a sensible value, then it doesn't matter that the array had random numbers to begin with. I guess this is an optimization by the compiler, because it does take time to clear memory and you don't want the compiler to do things you haven't asked it to do.
_________________
You can't beat our meat

#118939 - Cearn - Sun Feb 18, 2007 12:28 pm

Dox-999 wrote:
Hi guys I am still learning C++ and I am doing arrays so I decied to practise this by making a table where the user could input 1 number (I know it's not very practical but anyway). When I ran the program the number I input worked but all the other spaces in the table have random numbers in there. This is my code:

Code:
#include <iostream>
using namespace std;
int main()
{
    int x;
    int y;
    int array[2][2];
    cout<<"Insert a number into this table:\n\n  0 1 2\n0[][][]\n1[][][]\n2[][][]";
    cout<<"\n\nEnter (in this order) the x value the y value and the number you wish to input  (e.g. 1 2 62): " ;
    cin>> x >> y >> array[x][y];
    cin.ignore();
    cout
        <<"["<< array[0][0] <<"]["<< array[0][1] <<"]["<< array[0][2]
        <<"]\n["<< array[1][0] <<"]["<< array[1][1] <<"]["<< array[1][2]
         <<"]\n["<< array[2][0] <<"]["<< array[2][1] <<"]["<< array[2][2]
         <<"]\n";
}


is there a way for all the integers I don't use to just stay blank?

Thanks :)


If you want to get technical about it, it's not that the values are random but that C doesn't initialize them itself at creation, so whatever value was in that memory previously will be used. Though that probably seems random enough :P

There is another problem with the code, namely that array[2][2] is a 2x2 matrix, but you're using it as a 3x3 matrix. When defining an array, you indicate the size, not the number of the last valid index.

Code:

int array[8];    // 8 elements, using indices 0, 1, 2, 3, 4, 5, 6, 7

#118942 - KayH - Sun Feb 18, 2007 12:58 pm

an array declared as :
Code:
typ array[SIZE];

contains the elements array[0] ... array[SIZE-1]

In contras to Touchstone I would say:
From my experience a not correctly initialized variable (in your case an array) is not only a little optical mistake, it is in fact a seed for a lot of serious issues and unforeseable behaviour of your application. Of course for a _very_ small testapp this isn't a big issue, but sometimes your projects get larger. Then you have learned the wrong way (it's not needed to initialize a var) and your project is near the end.
So I suggest you strongly: please learn it right and initialize your vars!

Not to mention that your debug capabilities on GBA are not so good as e.g. on a PC.

Often you use a special value (e.g. 0 or -1) to indicate an invalid content or the end of valid data etc. Therefore it is good and save programming style to initialize your vars.
Also this is mostly done on startup. Rightly done, the drawbacks are small in contrast to the benefit: reliable behaviour of your next cool game! ;-)

There could be some special cases where you can do it other way, but it seems you should learn the normal way.

#118953 - Touchstone - Sun Feb 18, 2007 4:10 pm

KayH wrote:
In contras to Touchstone I would say:
From my experience a not correctly initialized variable (in your case an array) is not only a little optical mistake, it is in fact a seed for a lot of serious issues and unforeseable behaviour of your application.

I didn't mean that you should read from uninitialized variables. My point was that the compiler should not initialize variables unless you tell it to. Sorry for the confusion.

There's no point in the compiler adding code to set a variable to 0 and then you go and add your code to set that variable to 14, or whatever.

So, when you don't initialize a variable you need to know that it can be any random value, and if you're fine with that then it's cool, but if you need the variable to have a default value, you have to set that value yourself including when the default value should be 0. If you have an array where all elements have to be zero, it's easily done, like so:
Code:
int MyArray[2][2] = {0};


Then we have the whole thing about how some crt0 code clear the BSS segment and how that still doesn't help with variables on the stack..
_________________
You can't beat our meat

#118967 - KayH - Sun Feb 18, 2007 6:26 pm

Sorry Touchstone, I don't want to offend you! It seems I missed the point in your entry :-(
But after reading the second I do agree with you! :-)