I've been advised that using malloc in a c++ program shouldn't be done. How can I convert this to a non-malloc code? Thank you!
https://www.geeksforgeeks.org/union-find-algorithm-set-2-union-by-rank/
I've been advised that using malloc in a c++ program shouldn't be done. How can I convert this to a non-malloc code? Thank you!
https://www.geeksforgeeks.org/union-find-algorithm-set-2-union-by-rank/
In C++ you should use new
operator instead of malloc
. E.g. (struct Edge*) malloc( graph->E * sizeof( struct Edge ) );
should be new Edge[graph->E];
. It reduces the boilerplate and makes code less error-prone.
Don't forget to use delete
or delete[]
instead of free
. Otherwise the behaviour is undefined.