how to create a Dynamic Array storing objects?

This is a discussion on how to create a Dynamic Array storing objects? within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a simple question which hasn't been answered by the search function of this forum. I'm a newbie, ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    5

    how to create a Dynamic Array storing objects?

    Hello,

    I have a simple question which hasn't been answered by the search function of this forum. I'm a newbie, so when explaining, be specific and basic please.

    I need to create a dynamic array that can store objects of a class. Then for each object, call functions from class. Once the array ends, i'd like to free the memory.
    Thanks!

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    create an array of pointers to the class. Use new to create the class, the -> operator to call members, and delete to free the memory. Maybe it would be easier to create a function to erase the array, so that it iterates through each element and calls delete.

  3. #3
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Try this:
    Code:
    MyClass *array[2];
    array[1] = new MyClass;
    array[2] = new MyClass;
    array[1]->MyFunc();
    delete [] array;
    array = NULL;
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    it has to be dynamic.

    I don't even know what Indigo said.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    You have to make a dynamic array. The best option in C++ is a container like vector. IMO, C++ newbies should learn vectors before dynamic C style arrays. If you cannot use vector or choose not to, then you must use new to allocate space for your array so that you can have a dynamic size. The downside of using new is that you have to remember to call delete[] on the array when the function ends, no matter how it ends or you leak the memory. The vector class takes care of that for you.

    >> create an array of pointers to the class
    I don't see why you need to create an array of pointers, an array of objects should be fine.

    >> Try this
    That code is not even correct, besides the fact that it doesn't solve the OP's problem.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >array[2] = new MyClass;
    Beautiful example of undefined behaviour.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    Well thanks; I used the NEW command.

    I was hoping you could provide the code, but after a while of messing around I got the solution with your guidance everyone.

    PC *obj = new PC [size];


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. beginner: dynamic array of strings
    By pc2-brazil in forum C++ Programming
    Replies: 10
    Last Post: 04-29-2008, 04:29 PM
  3. need help with dynamic array syntax
    By soldyne in forum C Programming
    Replies: 3
    Last Post: 10-11-2005, 01:59 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Replies: 5
    Last Post: 11-20-2001, 11:48 AM

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