C++ delete vector, objects, free memory

You can call clear, and that will destroy all the objects, but that will not free the memory. Looping through the individual elements will not help either (what action would you even propose to take on the objects?) What you can do is this: vector<tempObject>().swap(tempVector); That will create an empty vector with no memory allocated … Read more

How do you ‘realloc’ in C++?

Use ::std::vector! Type* t = (Type*)malloc(sizeof(Type)*n) memset(t, 0, sizeof(Type)*m) becomes ::std::vector<Type> t(n, 0); Then t = (Type*)realloc(t, sizeof(Type) * n2); becomes t.resize(n2); If you want to pass pointer into function, instead of Foo(t) use Foo(&t[0]) It is absolutely correct C++ code, because vector is a smart C-array.