+ 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?
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
+ 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
+ 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?
+ 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.
0
Thanks guys.
Can one of you see if this method works properly? If not, I can do it after work.
0
Not sure if it's me, everything else is working fine, but that code won't load. lul
0
It's almost as if it's been deleted or something. 🤔
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