Thread: dynamic array of objects

  1. #1
    mrukok
    Guest

    dynamic array of objects

    im dynamically creating an array of 'player' objects for a game.
    where the number of objects created (and therefore the number of array elements) is determined by the user.

    heres what i've got:
    Code:
    player * Players = new player[numberOfPlayers]; 
    for(int p=0; p<numberOfPlayers; p++)
    Players[p] = new player;
    where player is the class name
    and Players is the name of the array
    and numberOfPlayers is an integer determined by the user

    why wont this work?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    There are two options: create an array of pointers and then create instances for those pointers (what you code is trying to do), or create an array of instances of objects straight up. Code:
    Code:
    int **pa = new int*[10]; //array of 10 pointers (to integers)
    for (int n=0; n<10; n++)
       pa[n] = new int;
    
    //or
    
    int *a = new int[10]; //done, array of 10 integers
    You can do the same with class objects, but the first method requires a default constructor in the object your are creating, where the second method does not. Also, you have more to delete using the second method

    Clear anything up?

    gg

  3. #3
    mrukok
    Guest
    maybe.. i'll check it in a bit..

    why is there more to clear up? dont you just delete and reset a?

    also.. what was wrong with the way i was trying to do it?

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Read my code, whenever you see "int", substitute in "Players" - thats the correct way of doing it.

    Your code is mixing option 1 and 2 - remove your last 2 lines and you have option 2, replace your first line and you have option 1.

    "Clear anything up?" == "Do you understand?"

    gg

  5. #5
    mrukok
    Guest
    sorry.. i meant to write "why is there more to delete"!

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Every "new" should have a cooresponding "delete" - somewhere. So using option 1, you would free all the memory with:
    Code:
    for (int n=0; n<10; n++)
       delete pa[n]; //delete individual elelments (pointers)
    delete[] pa; //delete the array of pointers now that the pointers have been deleted
    Where using option 2, you only have to delete the array with:
    Code:
    delete[] a;
    gg

  7. #7
    mrukok
    Guest
    with option 1, couldnt you just delete the array, and then wouldnt the individual elements go with it?

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Oh, they would go with it alright! It's call a memory leak.

    Every "new" should have a cooresponding "delete".
    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading Array Objects for use with Classes
    By ibleedart in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2007, 06:48 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM