Hi there,
I have a problem with dynamic memory allocation. I know that it would have been easier to use vectors methods, but i want to know what i do here wrong.
This is one of my methods in t_Item class - I use it to store Item Objects (which are classes too).
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
class t_Item {
public:
t_Item();
Allocate_Item();
private:
Item **collection;
int item_number;
};
t_Item::Allocate_Item()
{
if(item_number == 0)
{
collection[item_number] = new Item;
item_number++;
}
else
{
// creating buffer - temporary space where ill hold pointers to already existing Items
Item **buffor;
buffor = new Item*[item_number];
for(int yz=0; yz <= item_number; yz++)
buffor[yz]=NULL;
//Copying pointers from collection to buffor
for ( yz=0; yz < item_number; yz++)
{
buffor[yz] = collection[yz];
}
// Here is the problem - when i try to delete 'collection' memory corruption error occurs when i skip it it will proceed further without any problems but thats not the way it should be. WHen i remove it, program runs without any problems, but but isnt that memory leak ?
delete [] collection;
//Creating bigger collection - to store another Item
collection = new Item*[item_number];
for(yz=0; yz <= item_number; yz++)
collection[yz]=NULL;
// Copying pointers from buffor to collection, which can now have one Item more, we create after copying
for (yz=0; yz<item_number; yz++)
{
collection[yz] = buffor[yz];
}
collection[litem_number]=NULL;
// here we create new item - function is correct. We add one item more
collection[item_number]= new Item;
item_number++;
// Again the same problem - when i try to delete 'buffor' memory corruption error occurs
delete [] buffor;
}
}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Anyone knows where is the problem ??
BTW1: This is my first post so HELLO WORLD to all programing maniacs out there
BTW2: Sorry for my english - i know its very very bad.... :/