int *ptr;
ptr = (int*) malloc( sizeof(int)*5 );
assert(0==1);
free(ptr);
If so, how to use ASSERT without having to worry about memory leak
issue? Thanks.
You've already got three good replies, the only thing I think missing
from those is if the "assert" function above is not the version from
cassert / assert.h, but rather something you've written that throws an
exception, then - if you have some reason to need it on the heap
rather than using std::vector<> as already suggested, you can use make
ptr a smart pointer type so it will automatically deallocate heap
memory (e.g. the Standard includes the quirky std::auto_ptr<>, or see
the freely downloadable Boost library for more safely and generally
usable versions, or the book Modern C++ Design by Alexandrescu for
some pretty comprehensive tips on writing your own, or his Loki
library for downloadable source code).
Cheers,
Tony