Call to new to create objects that go into a vector

This is a discussion on Call to new to create objects that go into a vector within the C++ Programming forums, part of the General Programming Boards category; I don't have the code in front of me & I'm using a SDK. I will try to reproduce the ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    20

    Call to new to create objects that go into a vector

    I don't have the code in front of me & I'm using a SDK. I will try to reproduce the code from memory. I need to know the best way release memory back from when I create new objects from calling new.

    I have a public member variable (vector of pointers), that is passed by address to a different class member function. There its filled with the address of a new object.

    Code:
     
    for (unsigned int h = 2; h < ArenaSize; h+=3){
    if(ArenaBuffer[h] > THRESHOLD){
    Rx = h%WIDTH;
    Ry = h/HEIGHT;
    RedXY *XYobject = new(nothrow) RedXY;
    XYobject->iX = Rx;
    XYobject->iY = Ry;
    (*vPixels).pushback(XYobject);
    }
    }
    After it is filled in that class member function, it is used in another member function WITHIN its class.

    Code:
    pBitmap->drawline(RGB(255,255,255), (*it)->iX, (*it)->iY, (*it)->iX+1, (*it)->iY+1);
    Do I step through the vector and use delete on the iterator?
    Code:
    delete (*it);
    or can I use
    Code:
    (*vPixels).clear();
    This is used for real-time video processing and objects are being created at 30Hz What is the best way to release memory that was used in making an object with new?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    What is the best way to release memory that was used in making an object with new?
    There is only one way: delete. It doesn't matter whether a pointer to an object created with new is stored in a vector, in an array, or in a single variable, you need to use delete. You might want to take this approach:

    1) Define a destructor for your class. Have your destructor output the message: "destructor called".

    2) Practice deleting a pointer that is stored in a single variable.

    3) Practice deleting an array of pointers. Look at the number of messages you get from your destructor, and check if you get the right number of messages.

    4) Try using delete on the pointers stored in your vector.
    Last edited by 7stud; 02-11-2006 at 02:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  2. Inline asm
    By brietje698 in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2007, 01:54 PM
  3. Custom Vector template: Want to create erase/clear function
    By Bird Killer in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:37 AM
  4. API Call to create folder
    By cboard_member in forum Windows Programming
    Replies: 2
    Last Post: 12-01-2005, 11:08 AM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 06:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21