+ 1

Am I deleting the grids properly?

https://code.sololearn.com/cKWke2IqVJtv/?ref=app I should probably stop coding with no sleep, but how would I properly delete the grid?

21st Jan 2019, 9:23 PM
Daniel Cooper
Daniel Cooper - avatar
9 Respostas
+ 2
That's the right way, you're allocating with the new[] operator so you need to free with the delete[] operator By the way you can call deleteGrid in the deconstructor to factorize the code
21st Jan 2019, 10:20 PM
Abel Louis
Abel Louis - avatar
+ 2
A good way to do this is to set the char** to nullptr after deleting it So you can check if the grid value is equal to nullptr And that's a good thing to set member to null after deleting it And test if the grid is null before deleting it
21st Jan 2019, 11:03 PM
Abel Louis
Abel Louis - avatar
+ 1
Thanks. Always good to have feedback because I don't trust my brain with no sleep. One more question How can I print something like "Grid does not exist" if ya know it doesn't exist?
21st Jan 2019, 11:00 PM
Daniel Cooper
Daniel Cooper - avatar
+ 1
If I understand your extra question correctly, I've checked a few forums and it seems like there is no standard way to check if delete actually freed the memory block, or if a pointer was indeed deleted. A debugger / memory analyzer can help in identifying if the block is still allocated to the program, or you can purposely crash the program. Abel Louis and many others were certainly right in setting a pointer to null/nullptr immediately after deleting it since that allows you to check and rightfully assume that you have freed the memory using delete.
22nd Jan 2019, 4:05 AM
Hatsy Rei
Hatsy Rei - avatar
0
Thanks guys. Can one of you see if this method works properly? If not, I can do it after work.
22nd Jan 2019, 5:54 AM
Daniel Cooper
Daniel Cooper - avatar
22nd Jan 2019, 5:59 AM
Abel Louis
Abel Louis - avatar
0
Not sure if it's me, everything else is working fine, but that code won't load. lul
22nd Jan 2019, 7:07 AM
Daniel Cooper
Daniel Cooper - avatar
0
It's almost as if it's been deleted or something. 🤔
22nd Jan 2019, 8:42 AM
Daniel Cooper
Daniel Cooper - avatar
0
Just try what I said 😉 Set to nullptr after delete Englobe the whole delete by an if to check if it's equal to nullptr And the deconstructor only call deleteGrid
22nd Jan 2019, 4:22 PM
Abel Louis
Abel Louis - avatar