By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,124 Members | 1,569 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,124 IT Pros & Developers. It's quick & easy.

Dynamic memory allocation - memory corruption error

P: 5
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.... :/
Nov 19 '06 #1
Share this Question
Share on Google+
4 Replies


P: 5
Help me :)
Nov 20 '06 #2

P: 5
I will really really appreciate help in this task
Nov 20 '06 #3

Banfa
Expert Mod 5K+
P: 8,950
The problem stems from the declaration

Item **collection;

and how you are using it. Sometimes you use it as though it were a Item **, sometime as though it were Item *.

What construct are you trying to produce? A dynamic array with 2 indexes (in which case Item ** is correct) or a dynamic array with 1 index (in which case collection should be Item *).

The actual reason you are getting the errors is that in on place you allocate by

collection = new Item;

and then you

delete[] collection.

i.e. you allocate a single instance but when you delete you indicate that you allocated an array.

For the current code I think

collection = new Item;

should be

collection = new Item *[1];
Nov 21 '06 #4

P: 5
Collection is a pointer to dynamic array of pointers to Objects - Items. I need it that way because i want to place all objects on the heap (not in the operational memory) and i want to be able to reallocate dynamically size of a table where i hold pointers to objects(collection).

In constructor I'm declaring that variable for the first time - i forgot to copy/paste it.

Expand|Select|Wrap|Line Numbers
  1. t_Item::t_Item()
  2. {
  3.     liczba_item=0;
  4.     zbior = new Item*[liczba_item];
  5.     cout << "n\ninicjalizacja tablicy\n" << this;
  6. }
  7.  
The solution you subimted isn't working too... for any other ideas i will be very thankfull?
Nov 21 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.