Program crashes upon trying to access a double pointed Eigen matrix

Hello, I'm currently working on a neural network program (for playing tetris).
The following bit of code is what causes me problems: (taken from the weights part of the program)

The program crashes upon trying to access W[a][0] if a>=3
Crashes with the error:
Process returned -1073741819 (0xC0000005)

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
#include <iostream>
#include "Eigen/Dense"

using namespace std;
using namespace Eigen;
int main()
{
    int pop=10;
    const int nSize=4;
    ArrayXi Size(nSize);
    Size << 200, 20, 10, 6;

    MatrixXd** W =new MatrixXd*[pop];
    for(int i=0;i<nSize-1;i++){
        W[i]=new MatrixXd[nSize-1];
        for(int j=0;j<nSize-1;j++){
            W[i][j]= MatrixXd::Random(Size[j+1],Size[j]);
        }
    }

    int a=3;

    cout << "This will get printed";
    
    cout << W[a][0];

    cout << "this won't";
    return 0;
}


I presume this comes from a problem with memory ( since it stores ( 200*20 + 20*10 + 10*6 ) * 10 = 42'600 doubles ), but how do i overcome it ?

( I'm using Code::Blocks on win10 )
Last edited on
I presume this comes from a problem with memory ( since it stores ( 200*20 + 20*10 + 10*6 ) * 10 = 42'600 doubles ), but how do i overcome it ?
It is less than 1/2 mega byte, so this shouldn't be the problem on a current pc.

There might be an out of bounds access somewhere.
Line 13:
MatrixXd** W =new MatrixXd*[pop];
This creates a set of lines up to pop-1 (which is 3)

Lines 14 and 15
1
2
    for(int i=0;i<nSize-1;i++){
        W[i]=new MatrixXd[nSize-1];

This will only create the corresponding rows up to nSize - 2 (which is 2)

Line 25:
cout << W[a][0];
With a = 3, W[3][0] doesn't exist, because you only created the rows up to a=2.



I suggest you change line 14 to
for(int i=0;i<nSize;i++){


I was a bit taken aback by line 11, but it appears from the Eigen website that it's legal.
Last edited on
Well, that was a rookie mistake ...

Everything works now ! Thank you !

Do you want me to keep you updated on my project ? (a Neural Network that learns to play tetris)
If you do, how should i contact you ?


PS: If I'm not mistaken, line 11 is the only way to initiate an Eigen array (other than element by element or with another array)

PPS: I'll put it as solve when i get your answer, or tomorrow
Last edited on
Topic archived. No new replies allowed.