Creating A Multidimentional Array Of Classes

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
davidthefat
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 529
Joined: Mon Nov 10, 2008 3:51 pm
Current Project: Fully Autonomous Robot
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: California
Contact:

Creating A Multidimentional Array Of Classes

Post by davidthefat »

I feel like a total noob asking this, but I was unsuccessful with making a 2d array of classes. I would get runtime errors, but not compiler errors. I tracked down the error using a debugger to a initialization of a 2d array of classes using pointers. I will post the code as soon as I can get my hands on it (its on a school computer)
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Creating A Multidimentional Array Of Classes

Post by K-Bal »

Without code there is not much to say. You could use std::vector though:

Code: Select all

std::vector< std::vector< xyz > > myDynamic2dArray;
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Creating A Multidimentional Array Of Classes

Post by Ginto8 »

K-Bal wrote:Without code there is not much to say. You could use std::vector though:

Code: Select all

std::vector< std::vector< xyz > > myDynamic2dArray;
Unless you NEED it to be dynamic, you don't want to use vectors. And even if you need it to be dynamic, you only want to use vectors if they have a significant advantage over simple dynamic arrays.

As for the OP's problem, without more informationt there isn't anything to do. To quote Sherlock Holmes, "One should never theorize until he has data."
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Creating A Multidimentional Array Of Classes

Post by K-Bal »

Ginto8 wrote: Unless you NEED it to be dynamic, you don't want to use vectors. And even if you need it to be dynamic, you only want to use vectors if they have a significant advantage over simple dynamic arrays.
There are many ways to do it. I like it for it's simplicity and would never use it in high performance programs.
User avatar
davidthefat
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 529
Joined: Mon Nov 10, 2008 3:51 pm
Current Project: Fully Autonomous Robot
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: California
Contact:

Re: Creating A Multidimentional Array Of Classes

Post by davidthefat »

Code: Select all

//Image.cpp

#include "Image.h"

Image::Image(int H, int W)
{
    Height = H;
    Width = W;
    **Points = *new Location[Height];  //This is the problem
    for(int c = 0; c < H; c ++)
    {
        *Points[c] = *new Location[Width];
    }
    for(int h = 0; h < H; h ++)
    {
        for(int w = 0; w < W; w ++)
        {
            Points[h][w].setLocation(*new Location((float)w / 3 , (float)h / 3, 0));
        }
    }
}

Image::~Image()
{
    //dtor
}

float Image::getX(int H, int W)
{
    return Points[H][W].getX();
}
float Image::getY(int H, int W)
{
    return Points[H][W].getY();
}
float Image::getZ(int H, int W)
{
    return Points[H][W].getZ();
}
int Image::getHeight()
{
    return Height;
}
int Image::getWidth()
{
    return Width;
}

Code: Select all

//Image.h

#include "Location.h"

class Image
{
public:
    Image(int H, int W);
    virtual ~Image();
    float getX(int H, int W);
    float getY(int H, int W);
    float getZ(int H, int W);
    int getHeight();
    int getWidth();
protected:
private:
    Location **Points;
    int Height;
    int Width;
};
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Creating A Multidimentional Array Of Classes

Post by Falco Girgis »

You reeeeeaally might want to have a look here:
http://www.codeproject.com/KB/cpp/arrayDinamic.aspx

Sorry to be a cheap ass, but I've answered this question about 3 times this month in C and C++.
davidthefat wrote:

Code: Select all

Points[h][w].setLocation(*new Location((float)w / 3 , (float)h / 3, 0));
THAT is a very bad idea. I'm pretty sure this is memory leak city, unless you are explicitly freeing the new locations when they're done being used. I'm fairly certain that you just want to instantiate the temporary variable on the stack.

ALWAYS beware of instantiating temporary variables on the stack--unless whatever is receiving them is also deleting them, you are losing your reference to the pointer and can never free it.
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: Creating A Multidimentional Array Of Classes

Post by Bakkon »

davidthefat wrote:

Code: Select all

**Points = *new Location[Height];  //This is the problem
No shit, it's a problem. The syntax is entirely wrong.

Code: Select all

Location*** Points = new Location**[COLS];

for(int i = 0; i < COLS; i++)
     Points[i] = new Location*[ROWS];

for(int y = 0; y < COLS; y++)
     for(int x = 0; x < ROWS; x++)
          Points[x][y] = new Location(params);

for(int y = 0; y < COLS; y++)
     for(int x = 0; x < ROWS; x++)
          delete Points[x][y];

for(int i = 0; i < COLS; i++)
     delete [] Points[i];

delete [] Points;
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Creating A Multidimentional Array Of Classes

Post by K-Bal »

GyroVorbis wrote: ALWAYS beware of instantiating temporary variables on the stack--unless whatever is receiving them is also deleting them, you are losing your reference to the pointer and can never free it.
You wanted to say heap ;)
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Creating A Multidimentional Array Of Classes

Post by Falco Girgis »

K-Bal wrote:
GyroVorbis wrote: ALWAYS beware of instantiating temporary variables on the stack--unless whatever is receiving them is also deleting them, you are losing your reference to the pointer and can never free it.
You wanted to say heap ;)
Ah, shit. You're right. :oops:
User avatar
davidthefat
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 529
Joined: Mon Nov 10, 2008 3:51 pm
Current Project: Fully Autonomous Robot
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: California
Contact:

Re: Creating A Multidimentional Array Of Classes

Post by davidthefat »

Bakkon wrote:
davidthefat wrote:

Code: Select all

**Points = *new Location[Height];  //This is the problem
No shit, it's a problem. The syntax is entirely wrong.

Code: Select all

Location*** Points = new Location**[COLS];

for(int i = 0; i < COLS; i++)
     Points[i] = new Location*[ROWS];

for(int y = 0; y < COLS; y++)
     for(int x = 0; x < ROWS; x++)
          Points[x][y] = new Location(params);

for(int y = 0; y < COLS; y++)
     for(int x = 0; x < ROWS; x++)
          delete Points[x][y];

for(int i = 0; i < COLS; i++)
     delete [] Points[i];

delete [] Points;
:| Unfortunately, that has the same error... :nono: IDK what to do... Its due on Wednesday and I still have to get the socket connection working too...
Post Reply