Deleting Dynamic array's of an object

This is a discussion on Deleting Dynamic array's of an object within the C++ Programming forums, part of the General Programming Boards category; I'm trying to dynamically create an array of objects, then have them delete later on but for some reason i'm ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    45

    Deleting Dynamic array's of an object

    I'm trying to dynamically create an array of objects, then have them delete later on but for some reason i'm getting an error with the delete line. I'm still confused with the whole dynamic object thing, but hopefully this will help me get the hang of it. Here is what I did let me know where I am going wrong...

    Inside the constructor of one file I call anther class and create it dynamically. (inside the constructor)

    Score * list = new Score[10];

    I'm not having any errors with that but when I try to delete the same thing using this code. (Inside the deconstructor)

    delete [] list;

    I get an error saying that
    15: error: `list' undeclared (first use this function)


    Here is my code for both the constructor and deconstructor...

    Storage::Storage(int dis)
    {
    Score * list = new Score[10];
    }

    Storage::~Storage()
    {
    delete [] list;
    }

    Any idea?

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,203
    1. Read the sticky post about posting code!!!

    Score * list = new Score[10]; wont be saved anywhere. to fix this do something like this:
    Code:
    class Storage
    {
    private:
    
    Score* list;
    public:
    Storage(int dim); ~Storage();
    }; Storage::Storage(int dim) { list = new Score[10]; } Storage::~Storage() { delete[] list; }
    Last edited by Shakti; 11-14-2004 at 02:28 PM.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Score * list = new Score[10];
    You allocate a dynamic array of 10 Score objects and store them in list.

    >}
    You exit the function and list is destroyed because it goes out of scope. Your reference to the memory is gone even though the memory was never released. That's called a memory leak.

    The problem is a surprisingly common one, accidentally declaring and initializing a variable instead of assigning to a data member.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help With Custom Dynamic Arrays
    By feso4 in forum C++ Programming
    Replies: 3
    Last Post: 06-29-2006, 01:05 PM
  2. Deleting object after list removal (C++ visual studio)
    By RancidWannaRiot in forum Windows Programming
    Replies: 2
    Last Post: 10-20-2005, 06:06 PM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  4. Array of Pointers + Deleting An Object = Problems
    By Nereus in forum C++ Programming
    Replies: 3
    Last Post: 03-04-2004, 11:16 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

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