maybe a problem with pointers

M

mrDumbass

The following 'minimal example' code works(to my knowledge) but it
envokes some strange behaviour of my computer, not fatal, but some
strange lag.
i have a feeling i did something wrong with a pointer or two.
Did i make an error in this code ?? or is it my imagination.

int ***neuralNetwork;
int width;
int hight;
int depth;

// making
neuralNetwork = new int **[width];
for (int i=0; i < width; i++)
{
neuralNetwork = new int *[hight];
for (int j=0; j < hight; j++)
neuralNetwork[j] = new int[depth];
}
// filling
for (int i = 0; i < depth; i++)
for (int k = 0; k < hight; k++)
for (int j = 0; j < width; j++)
neuralNetwork[j][k] = 0;
// deleting
for (int i=0; i < width; i++)
{
for (int j=0; j < hight; j++)
delete [] neuralNetwork[j];
delete [] neuralNetwork;
}
delete [] neuralNetwork;
 
V

Victor Bazarov

mrDumbass said:
The following 'minimal example' code works(to my knowledge) but it
envokes some strange behaviour of my computer, not fatal, but some
strange lag.
i have a feeling i did something wrong with a pointer or two.
Did i make an error in this code ?? or is it my imagination.

The code as is cannot be compiled, nor can it be used even after being
wrapped in a simple function -- neither of the integer values are
initialised, so it has undefined behaviour on the line after "// making"
comment.

If we look at it as two disconnected fragments (declarations first and
then the loops next), then the code seems OK, and I presume it has no
errors that your compiler would report. The "lag" could be attributed
to the optimiser taking a shot at the nested loops.
int ***neuralNetwork;
int width;
int hight;
int depth;

// making
neuralNetwork = new int **[width];
for (int i=0; i < width; i++)
{
neuralNetwork = new int *[hight];
for (int j=0; j < hight; j++)
neuralNetwork[j] = new int[depth];
}
// filling
for (int i = 0; i < depth; i++)
for (int k = 0; k < hight; k++)
for (int j = 0; j < width; j++)
neuralNetwork[j][k] = 0;
// deleting
for (int i=0; i < width; i++)
{
for (int j=0; j < hight; j++)
delete [] neuralNetwork[j];
delete [] neuralNetwork;
}
delete [] neuralNetwork;



V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top