This code crashes, unless the indicated line is commented out, then it works beautifully. I cannot understand this because it is the 3rd in a series of 4 repetitions of the same thing. Any help is appreciated. Thanks

int x=0;
    int y=0;
    int lp=0;
    for(lp=1;lp<=4;lp++)
    {
        y=0;
        x++;
        y++;
        A[x][y]=rand() % 9 + 0;
        cout << x << "-" << y<< " A=" <<A[x][y] << " " << "\n";
        y++;
        A[x][y]=rand() % 9 + 0;
        cout << x << "-" << y<< " A=" <<A[x][y] << " " << "\n";
        y++;
        A[x][y]=rand() % 9 + 0;//comment out this line and it works
        cout << x << "-" << y<< " A=" <<A[x][y] << " " << "\n";
        y++;
        A[x][y]=rand() % 9 + 0;
        cout << x << "-" << y<< " A=" <<A[x][y] << " " << "\n";

Expected output

1-1 A=6
1-2 A=6
1-3 A=5
1-4 A=3
2-1 A=3
2-2 A=7
2-3 A=8
2-4 A=0
3-1 A=4
3-2 A=4
3-3 A=8
3-4 A=2
4-1 A=7
4-2 A=2
4-3 A=6
4-4 A=5

Dani AI

Generated

The crash is a textbook out‑of‑bounds write that was hidden until the program touched adjacent memory. As and suggested, the code declares int A[4][4] but indexes it with 1..4; C++ arrays are 0‑based, so valid indices are 0..3. Changing the array to A[5][5] only masks the bug by giving those out‑of‑range writes somewhere to land — it does not fix the underlying logic error.

Out‑of‑bounds writes cause undefined behavior: the program may crash immediately, later, or not at all; inserting a cout can change stack/heap layout and make the fault appear at a different point. For an authoritative description see the cppreference page on undefined behaviour (Undefined behavior).

Practical fixes and improvements:

  • Follow ’s safer pattern: iterate rows and columns with indices from 0 to size‑1 (or compute A[x-1][y-1] if you insist on 1‑based logic). Prefer explicit size constants so the limits are obvious.
  • Prefer standard containers and bounds-checked access in debug: for example std::vector<std::vector<int>> A(4, std::vector<int>(4)); and A.at(i).at(j) will throw on out‑of‑range access instead of silently corrupting memory.
  • Use tools to catch this class of bug: run under Valgrind (valgrind.org) or compile with AddressSanitizer (-fsanitize=address -g) to get immediate diagnostics (AddressSanitizer). Also enable warnings (-Wall -Wextra) and build a debug configuration.

Summary: the symptom (crash that disappears when code is changed) is consistent with undefined behavior from out‑of‑bounds indexing. Fix the indices or the allocation, and use container/member checks and sanitizers to detect similar mistakes early.

Recommended Answers

All 10 Replies

my guess is that you are going out of the array bounds. Whats the size of the
array?

heres the full code

int main()
{
    srand(time(NULL));
     int A[4][4];


    int x=0;
    int y=0;
    int lp=0;
    for(lp=1;lp<=4;lp++)
    {
        y=0;
        x++;
        y++;
        A[x][y]=rand() % 9 + 0;
        cout << x << "-" << y<< " A=" <<A[x][y] << " " << "\n";
        y++;
        A[x][y]=rand() % 9 + 0;
        cout << x << "-" << y<< " A=" <<A[x][y] << " " << "\n";
        y++;
        A[x][y]=rand() % 9 + 0;//comment out this line and it works
        cout << x << "-" << y<< " A=" <<A[x][y] << " " << "\n";
        y++;
        A[x][y]=rand() % 9 + 0;
        cout << x << "-" << y<< " A=" <<A[x][y] << " " << "\n";
    }

    return 0;
}

..what truly baffles me, is that if

cout << "Fine till here";

is added on line 27, it will print that then fail...... I dont understand what is failing..

Ok, so I figured it out with the help from the post above about array bounds.

i changed

int A[4][4];
to
int A[5][5];

But i dont understand why it works, could anyone explain?

may i know which compiler you are using?
You surely have array out of bounds in line 24.

Also may i know the headers you are including??

HmmMMMmmm... IMHO, I think this can be done in a simpler way, by using two loops instead of one, that iterates over the entire range of your 2D array. If you have worked with matrices in C++, then you'll know what I'm talking about. Now in C++, array indices start from 0, not 1, so you'll need to fix your loop or your array dimensions. Try this:

for(int i = 0; i < 4; ++i) { // To traverse the rows like so: ((0,0), (1,0)... etc
    for(int j = 0; j < 4; ++j) { // To traverse a column like so: (0, 0), (0,1)...etc
        A[i][j] = rand() % 9;
        cout << i+1 << "-" << j+1 << " A=" <<A[i][j] << " " << "\n"; // I guess its cheating, but you get your output :)
    }
}

(For this code, you wouldn't have to change your array's dimensions)

Hope this helps!


From the functions the OP has used, I should think that he has used the following headers:

#include <iostream>
#include <cstdlib>
#include <ctime>


glad that you figured it out.

Explanation:
An array ALWAYS starts from 0. So if you have something like int A[4] the first element will be at A[0]. Then A[1],A[2] and A[3]. Note we have already reached 4 elements as we wanted (0-3) hence A[4] is meaningless and when you try to assign something to A[4] we say that you are going out of bounds.

Hope that helps.

@amrith
Yep he should include all the three.

His main problem i guess was the out of bounds thing.

The above explanation was for tendavola.

:

You must also take a deeper look into what amrith92 posted. his code is generally the way you should do these things. And if you follow, his loops iterates from 0 to
"less than" 4. That is, it effectively goes 0,1,2 and 3.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.