Dynamic Array Help

LichKing

New member
Joined
Mar 18, 2005
Messages
2
Ok so I wrote a program for my Algorithms class and Im a bit rusty on my c++
Basically it has a main .cpp file which calls all teh functions from a different file sorts.cpp (program runs mergesort, heapsort, quicksort and insertionsort) using dynamic arrays which are declared globally in the sorts.cpp file and they are initalized in a function, now if I dont call delete[] arrayname I get a CrtMemory() or something like that error if I input a large array size so I believe thats a memory leak or my freestore is being used up. Ok so my question is where do I need to call delete in order for it to work correctly, Ive tried making a function in sorts.cpp and calling it from my main to dlete the arrays but I get a damage at normal block .... at some mem address. Can someone help me out???? I would like to have it work as is, but if thats not an option would it work better beeing a class or struct and creating an overloaded destructor?
 
instead of having all those functions in one file, try creating a class for each type of sort. Then in the destructor of each class have it free up any memory used, that should take care of your problem (if I am reading it correctly). This way, you can sort one way then free up the memory. Sort another and again free it up.
 
now if I dont call delete[] arrayname

You must free up any memory that you have dynamically allocated or it will be viewed as a memory leak. Especially if you are using large amount of data as you said.

Ok so my question is where do I need to call delete in order for it to work correctly
You need to call delete after you are done working with all the memory and have your final answer. Once you no longer need the array, delete it.
 
Back
Top