Dynamic memory allocation - memory corruption error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tomassus
    New Member
    • Nov 2006
    • 5

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

    xxxxxxxxxxxxxxx xxxxxxxxxxxxxxx xxx

    class t_Item {
    public:
    t_Item();
    Allocate_Item() ;

    private:
    Item **collection;
    int item_number;
    };

    t_Item::Allocat e_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;
    }
    }

    xxxxxxxxxxxxxxx xxxxxxxxxxxxxxx

    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.... :/
  • Tomassus
    New Member
    • Nov 2006
    • 5

    #2
    Help me :)

    Comment

    • Tomassus
      New Member
      • Nov 2006
      • 5

      #3
      I will really really appreciate help in this task

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        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];

        Comment

        • Tomassus
          New Member
          • Nov 2006
          • 5

          #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(collect ion).

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

          Code:
          t_Item::t_Item()
          {
          	liczba_item=0;
          	zbior = new Item*[liczba_item];
          	cout << "n\ninicjalizacja tablicy\n" << this;
          }
          The solution you subimted isn't working too... for any other ideas i will be very thankfull?

          Comment

          Working...