codeblocks and bloodshed dev c++ issue..

Apr 18, 2012 at 1:34pm
Hi there, I am using a different compiler now, Codeblocks, because I want to try a new environment..
anyways, here's the problem.
whenever I try to run my code in cblocks,

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
#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i, j;
    int number = 0;
    int table[2][2];

    for (i = 0; i < 3;){
        for (j = 0; j < 3; j++){
            cout<<number<<" j = "<<j<<" i = "<<i<<endl;
            table [i][j] =number;
            number++;
        }
        i++;
        cout<<i<<endl;
    }

char y;
    y = getch();

    return 0;
}


the variable "i" at the end of the code is equal to 6 instead of my expected output of 2.
I don't understand the output.. lol
So I have decided to use my dev c++ compiler, and it works fine.. But I want use Cblocks because I need some functions on it in my project..

help me out guys.. :((

Apr 18, 2012 at 1:39pm
table is a 2x2 array. Array indexing starts at 0 so when you access table[2][0], table[2][1], table[2][2], table[1][2] and table[0][2] you are out of bounds. If you want 3x3 elements in table you should declare it as int table[3][3];.
Apr 18, 2012 at 1:42pm
` oh thanks Peter87... LOL.. I feel so stupid now.. xD

but I still wonder why dev c++ creates a different output..
Last edited on Apr 18, 2012 at 1:43pm
Apr 18, 2012 at 1:51pm
If your program is incorrect you can't expect it to work correctly.

The difference is probably because the compilers put the variables at different locations in memory. When you write to the array out of bounds you overwrite memory outside the array. If a variable happens to be stored at that memory location it's value will be changed.
Apr 18, 2012 at 2:05pm
` oh.. thanks mate, I got your point..
Thank you so much for the help!
I can now proceed to my tic tac toe program.. :)
Topic archived. No new replies allowed.