Failed to access dynamic arrays c ++

2

Good afternoon. I spent the whole afternoon going around revising my code since I have been using the DEV C ++ and I have compiled a matrix whose declaration had variables, however, I knew that it could not be and when I passed it to other compilers like the one in Visual Studio jumps an error.

After reading documentation of how to create dynamic matrices, I no longer remembered, and with the purpose of having the code run in any compiler, I have implemented what I have read in my code but I can not make the first operations in the first one. step and I do not know what is happening (once solved this would be an iteration of the same thing)

I leave the code here in case you can lend me a hand but first of all I explain in summary form because I want to use the dynamic matrices. Depending on some input data. This program will generate an output matrix always in the same way a huge number of rows (can be from 100 to 10,000 for example, from that number would be a bit exaggerated but it would not be unreasonable to obtain calculations with 30,000, 50,000 ... ptos why I need the dynamic calculation) and only three columns (which coincides with the typical canonical base x, yz)

From "dimensions" is when I start to prepare the dynamic matrix or in the line of this comment // Creating the pointers to create the matrices.

I have also read about the vector library but I do not know how to use C ++ and I have also read that it is one-dimensional. I know that with pointers you can do what I want because I have done it for years but I do not remember. Greetings and thanks.

EDIT WITH A MORE SIMPLE AND ENTENDABLE PROGRAM FOR ANYONE WHO CAN HAVE THIS DOUBT IN THE FUTURE:

#include "iostream"
#include "cstdlib"
using namespace std;
int main()
{
    int i;
    int a = 4;
    int crea1=4;
    double aux1[3] = { 1,5,7 };
    double **p1;
    p1 = new double *[a]; //Numeros de filas
    **for (i = 0; i < 3; i++)** **Aqui es donde esta el error**
    {
        p1[i] = new double[3]; //Numero de columnas
    }
    for (i = 0; i<a; i++)
    {
        p1[i][0] = aux1[0];
        p1[i][1] = aux1[1];
        p1[i][2] = aux1[2];
    }
    delete[] p1;
    cin.get();
    return(0);
}
    
asked by Alvaro 26.05.2017 в 20:02
source

1 answer

2
Matriz p1[crea1][3];

Indeed this is not legal in C ++. This declaration corresponds to a VLA ( V ariable L ength A rray), a characteristic considered in the C standard, not in the C ++ one .

Now, your alternative has an important gap

double** p1;
p1 = new double *[crea1];
for (i = 0; i < 3; i++)
//              ^ AQUI!!!!
{
    p1[i] = new double[3];
}

That 3 should be crea1 since as you have indicated a few lines back ...

++crea1; // 3+1=4 <<--- OJO 4, no 3!!!!

Not initializing that fourth row is what causes the program to crack here:

//ERROR salta en el compilador aqui
for (i = 0; i<crea1; i++)
{
    escvec(i, DES, aux1);
    sumavector(ptoi, aux1, aux1);
    p1[i][0] = aux1[0]; // <<--- 
    p1[i][1] = aux1[1];
    p1[i][2] = aux1[2];
}

Since when i==crea1-1==3 the program tries to access an invalid pointer.

    
answered by 26.05.2017 / 23:48
source