470,815 Members | 1,324 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

Dynamic memory allocation - memory corruption error

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).


class t_Item {

Item **collection;
int item_number;


if(item_number == 0)
collection[item_number] = new Item;

// 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++)

//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++)

// 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];

// here we create new item - function is correct. We add one item more
collection[item_number]= new Item;

// Again the same problem - when i try to delete 'buffor' memory corruption error occurs
delete [] buffor;


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
4 2643
Help me :)
Nov 20 '06 #2
I will really really appreciate help in this task
Nov 20 '06 #3
9,065 Expert Mod 8TB
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
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. }
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.

Similar topics

5 posts views Thread by RoSsIaCrIiLoIA | last post: by
10 posts views Thread by eyh5 | last post: by
8 posts views Thread by ranjeet.gupta | last post: by
11 posts views Thread by toton | last post: by
14 posts views Thread by vivek | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.