Thread: 2D Array of Objects

  1. #1
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195

    2D Array of Objects

    I have a class called Image:

    Code:
    class Image
    {
    ...
    public:
    .....
    }
    when I want to instanciate it i just do this:

    Image *i = new Image();

    what I want to do is hav an array of those classes. How would i do that using the new keyword?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Image *i = new Image[20];
    ...
    ...
    delete [] i;

  3. #3
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195
    I guess i should have mentioned this. im passing parameters to the constructor:

    Image *i = new Image(filename, flags);


    should i be doing something like this:

    Code:
    Image **i;
    
    *i = new (Image *) Image[30];
    for (int x = 0; x < 30; x++)
    {
       i[x] = new Image(filename, flags);
    }
    ?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    This seems to work:

    Code:
    Image* (*p)[3] = new Image*[2][3];
    p[0][0] = new Image(x, y);
    cout<<p[0][0]->member_name<<endl;

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    For the delete, I think you have to cycle though each array position with a double for loop, and in the inner loop delete each pointer, e.g.:

    delete p[outer][inner];

    and in the outer loop delete each row of the array:

    delete [] p[outer];

    EDIT:I think that last line is wrong. After you delete all the individual pointers, and both for loops have finished, then I think you should do this instead:

    delete [] p;
    Last edited by 7stud; 02-26-2005 at 01:02 PM.

  6. #6
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195
    OK, thanks for the help. Ill try it out tomorrow.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    If you don't know the number of columns ahead of time, e.g.

    Image* (*p)[3]

    then you will have to do something like this:
    Code:
    int rows, cols;
    cout<<"Enter the rows and colums: \n";
    cin>>rows>>cols;
    
    Image*** p = new Image**[rows];
    for(int i=0; i<rows; i++)
    {
    	p[i] = new Image*[cols];
    }
    
    p[0][0] = new Image(x, y);
    cout<<p[0][0]->member_name<<endl;
    If rows=2 and cols=3, that sets up a structure like this:

    Code:
    ***p ------>**p0--------->*p0, *p1, *p2
                **p1--------->*p0, *p1, *p2
    EDIT:
    Deleting: in this case you have multiple pointers to arrays: ***p points to the first row of an array, and **p0 and **p1 point to the first element of an array. So, you need to start off deleting all the individual pointers using two for loops, and at the bottom of the outer for loop, you need to delete the arrays pointed to by **p0 and **p1,

    delete [] p[outer_index];

    Finally, after both for loops terminate, you need to delete ***p:

    delete [] p;

    Hopefully someone will verify that.
    Last edited by 7stud; 02-26-2005 at 01:10 PM.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    If you want to eliminate one level of indirection by storing objects in your array rather than pointers to objects, you can do this:
    Code:
    int rows, cols;
    cout<<"Enter the rows and colums: \n";
    cin>>rows>>cols;
    
    Image** p = new Image*[rows];
    for(int i=0; i<rows; i++)
    {
    	p[i] = new Image[cols];
    }
    
    p[1][0] = Image(x, y); 
    cout<<p[1][0].member_name<<endl;
    That would set up a structure like this:
    Code:
    **p-------->*p0----->Image0, Image1, Image2
                *p1----->Image0, Image1, Image2
    Deleting:
    Code:
    for(int i=0; i<rows; i++)
    {
    	delete [] p[i];
    }
    
    delete [] p;

  9. #9
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195
    I dont think this is exactly what i need. Here is basically what im trying to do:

    Code:
    **p-------->*p0----->Image0
               *p1----->Image1
               *p2----->Image2
               *p3----->Image3
    there is a pointer to an image object and what i want is an array of those pointers.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    **p-------->*p0----->Image0, Image1, Image2
                *p1----->Image0, Image1, Image2
    Code:
    **p------->*p0----->Image0
               *p1----->Image1
               *p2----->Image2
               *p3----->Image3
    Describe the differences between those two arrays.
    Last edited by 7stud; 02-26-2005 at 04:50 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing/pointing to a 2d array
    By jamie85 in forum C Programming
    Replies: 7
    Last Post: 10-28-2005, 10:16 PM
  2. Copying from one 2d array to another....with a twist
    By Zildjian in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2004, 07:39 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM