Just to get this straight, suppose I have a loop that iterates some number of times. In each loop I allocate some array memory by declaring a pointer p and using the malloc function. Now, do I have to call free(p) at the very end of each iteration? Or is it that, when the loop finishes, memory automatically becomes deallocated?
The former - the number of mallocs in a program should equal the number of frees. Those are typical C functions though - in C++ we tend to use 'new' and 'delete' instead. I'm sure there are edge cases, but this is the general rule.
Also, for ease of use, learn the STL:
1 2 3 4 5 6
for(int i = 0; i < 100; i++)
{
unique_ptr<Shape> p = new Shape("Circle");
// Do something with shape
} // auto deleted by unique_ptr! Handy, huh?