Thread: new And delete help. (Yup, me again >.<)

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    new And delete help. (Yup, me again >.<)

    First off I'm sorry I'm so damn thick, but I cant really figure this out.

    Ok, I'm at a standstill. I'm using some code to detect objects in a file, I have no way of knowing how many objects of a certain type are in it. But for every object of a certain type, I must allocate a block of memory.

    So, I turned to the new statement:

    Code:
    MyClass *C = new MyClass[x+1];
    The problem with this (correct me if I'm mistaken) Is that I'm now allocating my pointer new blocks of memory every time I find a new object, rather than incrementing the previous block? So if I have C[2], I actualy have C[2], OldBlock[1] right? Then incrementing it again, C[3], OldBlock[2], OldBlock[1]. After awhile I'm going to end up with alot of unused memory, so I wanted to delete them.

    Code:
    delete C;
    MyClass *C = new MyClass[x+1];
    My problem is, I still need the old memory. I cant just go and delete it like that, It contains data I need. So I'm at a conundrum... How do I allocate memory safely, but not end up leaving behind unused chunks? And without using huge ammounts of memory aswell.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Use nodes like Linked Lists and BSTs or if you want to be more modern use vectors. Don't delete memory and create bigger blocks of memory. Just keep all the memory and as you add more connect the pieces.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    MyClass *C = new MyClass[x+1];
    That creates a pointer to an array of MyClass objects that can store x+1 objects.

    Do all the objects have a common base type? Presumably, since a bunch of characters in a file isn't an object, you have to create the objects as you "see" them in the file. Then, you can store pointers to the objects you create in an STL collection like a <vector>, which will grow in size automatically to accomodate the number of pointers you need to store. If all the objects have a common base class, you only need 1 <vector>. If they don't have a common base class, then you need to create multiple vectors to hold the various types of pointers.

    However, you still have to allocate memory for each object using new. In addition, when you use new to create an array, you need to use delete [] on the pointer to the array rather than delete.
    Last edited by 7stud; 02-01-2006 at 03:11 AM.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by 7stud
    Code:
    MyClass *C = new MyClass[x+1];
    That creates a pointer to an array of MyClass objects that can store x+1 objects.
    I think his idea was that when he finds an object he creates an array MyClass[x+1] where x is initialized to 0 then he increments x. As he finds new objects he deletes his old memory and creates a new array continually getting 1 object size bigger each time.

    Look into more advanced data structures. This is a sloppy way of doing what you're doing and it's probably not a great idea to constantly be taking and returning memory to the OS so much.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I think his idea was that when he finds an object he creates an array MyClass[x+1] where x is initialized to 0 then he increments x. As he finds new objects he deletes his old memory and creates a new array continually getting 1 object size bigger each time.
    Blackroot,

    The STL containers do all that for you. In addition, it is very inefficient to only increase the memory by 1 every time. You have to create the new block of memory, copy the elements from the old block, and delete the old block of memory. So, with STL containers, once you add a certain number of elements, they start multiplying the existing memory by 2 when they allocate new memory. That cuts down on the number times they have to allocate new memory and do all the necessary copying and deleting which that entails.

    You should also note that you are going to be storing pointers to your objects in the STL container--not the objects themselves, so the memory that the STL container allocates for an element only has to be big enough to hold a pointer.

  6. #6
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    This process is preformed once. After its done I can just pass class data. I realize allocating x+1 memory is a bad idea because its slow, but because this is run once, I dont need to much speed. However vectors decomplicate things, I'll look into them, I remember glancing at a tutorial about them on this site, I'll look back into them.

    Thank you guys.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    To start using <vectors>, you only really need to know 3 things:

    1) How to create a vector.
    If you need a vector that stores ints, you create it like this:

    vector<int> myInts;

    If you need to create a vector that holds pointers to MyClass, you do this:

    vector<MyClass*> myPtrs;

    2) How to add elements to a vector.
    You can do that with the push_back() member function, which adds elements to the "back", or end of the vector:

    myInts.push_back(10);
    myInts.push_back(20);

    3) How to access the elements of a vector.
    The easiest way to access the elements is using array notation:

    cout<<myInts[0]<<endl;

    Don't forget to include <vector>.
    Last edited by 7stud; 02-01-2006 at 04:22 AM.

  8. #8
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Mmm then if Push_Back adds to the end, how do I insert something at the start/middle/wherever else?

    Thank you though, I was wondering how to declare vector pointers. (Can you declare void vector pointers?)
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Mmm then if Push_Back adds to the end, how do I insert something at the start/middle/wherever else?
    I listed just the basics. There are many operations you can perform on each of the STL containers. However, if you need to insert elements into the middle of an STL container, then a <vector> is not the best choice for your container because all the succeeding elements have to be shuffled over to the right to make room for the new element. That can involve lots of copying and therefore is inefficient. In that case, a <list> would be more appropriate.
    Last edited by 7stud; 02-01-2006 at 04:30 AM.

Popular pages Recent additions subscribe to a feed