   new and delete not working as expected
  Hi everyone,I've been using dynamic array and apparently changed something in the code so now after doing "delete []" "new" doesn't work.//in header I create the pointer like this:unsigned int * path;//in function findPath I use it like this:if (resetPath)//because at start I have nothing to delete{delete [] path;path = NULL;//added this out of desperation actuallyresetPath = false;}//code added here still workspath = new unsigned int [pathLenght + 1];//at the end of WinMain I delete it like this:delete [] path;Help please.   
unsigned int * path = 0; // initialize it, always, no excuse, ALWAYS.delete [] path; // works fine, cause you initialized it, thanks for initializingpath = new int[100];delete [] path; // works fine, you allocated it...path = 0; // set it back to 0 so you know it's freedelete [] path; // works fine again, it's 0, not some address you freed beforeSo basically, initialize it, get rid of that resetPath and set it to 0 when deleted. Of course you could just use:std::vector<int> path;But since you say it's a path, it'd make more sense if it was char, in which case you'd just use:std::string path;;  Sorry, I figured it out myself, the problem was that I was taking pathLength from wrong place and it was 0 (so the array size was 1) my code then wrote beyond past the start of the pointer and then "delete []" + "new" was killing the program.Thanks anyway, Rewdew!   ;  In general, there is no good reason to use raw new[] and delete[] in C++ when you have std::vector<>. Learn to use and love it.   ;Quote:Original post by rip-offIn general, there is no good reason to use raw new[] and delete[] in C++ when you have std::vector<>. Learn to use and love it.But it looks scary. All that usage with &(vector[0]) is weird to me also, it stays forever in the memory for me (don't know how to delete it actually :/ ).   ;  vector.clear();   ;Quote:Original post by Rewdewvector.clear();Which doesn't release the claimed memory.Elaboration on topic.Isn't C++ fun?   ;Quote:All that usage with &(vector[0]) is weird to me...Well, I actually find it is rare that I need a raw pointer to the vector's storage, I can usually find the iterator interface more convenient - and works nicely with the Standard functions in <algorithm>. And when I do want to get a pointer, I use &vector.front(), which is a little clearer IMO.You may find SiCrane's article useful.Quote:... it stays forever in the memory for me (don't know how to delete it actually :/ ).That is an optimisation. A vector won't release its memory unless it goes out of scope. In general, if your vector<> instance is defined in the correct scope, you won't want to release its memory.Quote:vector.clear();std::vector<>::clear() doesn't necessarily release the memory held by the vector. The way to do this is the swap trick:std::vector<Foo>().swap(vector);And yes, this one is a little scary. Good news is that it is rarely required, and can be hidden behind a nice function:template<class T>void trim(std::vector<T> &vector){    std::vector<T>().swap(vector);}This trick is shown in the article I linked earlier.   ;  Not necessarily but I've never met an implementation that doesn't do it. So true that doesn't mean it'll always be the case but I find it much more readable :-)+ usually people need a trick to prevent memory from being released, not the other way around (like doing a resize(0) instead of a clear, but yes that's also specific to an implementation).   ;Quote:Original post by RewdewNot necessarily but I've never met an implementation that doesn't do it. So true that doesn't mean it'll always be the case but I find it much more readable :-)+ usually people need a trick to prevent memory from being released, not the other way around (like doing a resize(0) instead of a clear, but yes that's also specific to an implementation).Really?My installation of MSVC 2008 appears not to release memory on clear(), nor does G++ 4.1.2.#include <vector>#include <iostream>int main(){std::vector<int> foo(64);std::cout << foo.capacity() << '\n';foo.clear();std::cout << foo.capacity() << '\n';}