Array bug? Explanation for what happens?

Mar 10, 2009 at 9:37am
Well, I was trying to remember how to do multidimensional arrays.

I typed in what I felt I'd memorized, but I had a little important typo.
See code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>


using namespace std;

int main()
{
    const int ROWS = 3;
    const int COLUMNS = 3;


    char board[ROWS][COLUMNS] = {{'X', 'O', 'X'}, 
                                 {'O', 'X', 'O'}, 
                                 {'X', 'O', 'X'}};
    
    cout << "Printing the board\n";
    
    for(int i = 0; i < ROWS; ++i)
    {
            for(int j = 0; i < COLUMNS; ++j)
            {
            cout << board[i][j];
            cout << endl;
            }
    }        
    
system("PAUSE");
return 0;
}
    





Please ignore the system"(PAUSE"); it's just there so I can see what it does. Besides, it's not a program for use for anything. I was just trying to remember the array like I said.

But anyway the error is in the second for-loop.

instead of:

1
2
3
4
5
for(int j = 0; j < COLUMNS; ++j)
            {
            cout << board[i][j];
            cout << endl;
            }


I added "i" instead of "j" at a rather crucial point.
for(int j = 0; i < COLUMNS; ++j)

Running this with the "j" in place, it works fine.However, with the "i" there it produces a very heavy crash. This isn't so much a call for help, since I know how to fix it. But I'm curious about what happens when it compiles like that and runs, but produces such wierd results.

I hope someone understands what I mean by my question.
Thanks for any replies.




Mar 10, 2009 at 10:09am
The variable i never changes, the test (i < COLUMNS) will never be false, and the for loop keeps going forever. Eventually, the app tries to use memory outside that allocated to the app by the OS.

Also, you should be using postfix (i++ instead of ++i). Right now you're only printing out four elements of the array.
Mar 10, 2009 at 11:13am
Thanks.

Why postfix and not pre? Pro's vs. cons? Or just a big no-no?
How am I only printing out 4 with the prefix?
Mar 10, 2009 at 12:27pm
No, you should be using prefix. ++var is always more efficient than var++ (unless some idiot programmer wrote a stupid pre-increment operator for the class).

Your code should be printing 9 elements as is.
Mar 10, 2009 at 1:09pm
stick with prefix -- it's better.

prefix returns the value after increment/decrement
postfix returns the value before increment/decrement
other than this difference, the two are exactly the same.

1
2
3
4
5
int a = 5;
int b = a++;  // a=6, b=5

a = 5;
b = ++a;  // a=6, b=6 


The reason this makes the prefix form preferable is because the postfix form may require one or more additional variables/objects to be created. This is usually only significant with complex classes that overload these operators where copying the object has significant overhead -- but for basic types like int, it doesn't really matter (though it can't hurt to get in the habit of using prefix form by default for form a good habit):

1
2
3
4
5
6
7
8
9
10
11
12
SomeClass& SomeClass::operator ++()   // prefix increment operator
{
  Increment();  // increment it
  return *this;  // return new value
}

SomeClass SomeClass::operator ++(int) // postfix increment operator
{
  SomeClass r(*this);  // copy object's current state
  Increment();         // increment this object
  return r;            // return OLD value (read:  this requires *another* copy)
}


here... prefix requires no copies. postfix requires 2 copies. If copying is computationally expensive, this could be a problem.

But like I say -- for types like 'int' it doesn't really matter.
Last edited on Mar 10, 2009 at 1:18pm
Mar 10, 2009 at 3:26pm
Thanks a lot guys.
I know this is totally basic, but stuff just slips my mind all the time and I find myself having to reread old chapters and books.

I'm a bit of a slow learner. When I really get something it sticks forever, but it's getting to that part which is really annoying. Anyway, thanks again.
Mar 10, 2009 at 4:40pm
You shouldn't try to memorize how to work with multi-dimensional arrays. Rather, if you understand how they are laid out in memory, you will easily be able to figure out how to work with them.

Just a suggestion... I find a lot of college students resorting to rote memorization of things that aren't worth the time and effort. Programming is an art form in that programming is problem solving, and often times there isn't an absolute right way to solve a problem. It requires creativity.
Mar 10, 2009 at 5:54pm
In the case of a for loop is the post increment vs. pre increment really an issue? Isn't the compiler going to be smart enough to know that a temporary object isn't needed either way in this case?
Mar 10, 2009 at 6:01pm
You are right.

However, for simplicity's sake, if it does not make a difference, you should always pick preincrement since it will never be less efficient than postincrement, but possibly more
efficient.
Topic archived. No new replies allowed.