d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Game Of Life > Assistance Please
Add Reply New Topic New Poll
Member
Posts: 4,625
Joined: Jul 3 2009
Gold: 6,520.00
Jun 3 2014 04:56pm
If anyone could help me with reading over my program to see where I have an error it would be much appreciated.
I already have the entire program written (teacher provided some come and then student -me- fills in rest of code and finishes particular functions), but are error(s).
It has to do with creating multiple dynamic 2d arrays.
Any help would be appreciated, please PM or leave a comment. Will tip FG for assistance.

I believe the error is within where I create the 2d array in a method:
//row and col are private members of the class. row = 5, col = 5.

Code
int ** Life::buildGrid()
{
int **ngrid;

ngrid = new int*[row + 2];
for (int i = 0; i < row + 2; i++)
{
ngrid[i] = new int[col + 2];

for (int j = 0; j < col + 2; j++)
{
ngrid[i][j] = 0;
}
}
return ngrid;
}


This post was edited by SleepyUnit on Jun 3 2014 05:00pm
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Jun 3 2014 05:47pm
That part looks ok to me, but boy, do my eyes hurt. Using raw (owning) pointers is a very bad practice in new code (except for exceptional cases, and no, this isn't one of them) and you should avoid them whenever possible. This may be your professor's fault, but that whole function could be a one liner:
Code
vector<vector<int>> v(row+2,vector<int>(col+2))
This is not only easier to read, but also safer in both exception safety and memory leak meanings.
Member
Posts: 4,625
Joined: Jul 3 2009
Gold: 6,520.00
Jun 4 2014 12:02pm
Quote (KrzaQ2 @ Jun 3 2014 07:47pm)
That part looks ok to me, but boy, do my eyes hurt. Using raw (owning) pointers is a very bad practice in new code (except for exceptional cases, and no, this isn't one of them) and you should avoid them whenever possible. This may be your professor's fault, but that whole function could be a one liner:
Code
vector<vector<int>> v(row+2,vector<int>(col+2))
This is not only easier to read, but also safer in both exception safety and memory leak meanings.


thank you for the help this far, pm'd with more info to help.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll