generally you want to avoid allocating with new as much as possible anyways. in addition to sven's suggest of returning the real pointer to the entry in the table which wouldn't need a delete, two other coherent options:theturk1234 wrote:Hi Edsel,
1) Because I'm done with the TTEntry, so it has to be deleted or else it's a memory leak. I just realized, though, that if I call delete on it and then ask for a member, I'm going to get undefined behavior. Maybe I should use:It's better C++, anyway.Code: Select all
if(tt->depth > depth) { int score = tt->score; delete tt; return score; }
- return a copy of the structure itself instead of a pointer if you want a copy and not the raw entry. the optimising compiler will handle making sure it's not copied twice so long as you use normal constructs (i.e. if you create a local variable to contain the copy in your probe function and always return that local variable the compiler will switch that local variable with the variable that is to receive the return value)
- create a local TTEntry variable tt in your search and pass it in as a parameter to your probe either as a reference or pointer. Pointer is syntactically clearer at the point where you call the function that what you're passing in may be changed.
In addition to wanting to avoid new whenever possible inside the inner loop of your program, separating the responsibility for new and delete into different places is dangerous as it becomes easy to forget one of the deletes or delete something too soon if you start using it from multiple places.