Page 1 of 1
Creating A Multidimentional Array Of Classes
Posted: Thu Jun 10, 2010 6:48 pm
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)
Re: Creating A Multidimentional Array Of Classes
Posted: Fri Jun 11, 2010 3:59 am
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;
Re: Creating A Multidimentional Array Of Classes
Posted: Fri Jun 11, 2010 9:57 am
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."
Re: Creating A Multidimentional Array Of Classes
Posted: Fri Jun 11, 2010 11:40 am
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.
Re: Creating A Multidimentional Array Of Classes
Posted: Fri Jun 11, 2010 6:46 pm
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;
};
Re: Creating A Multidimentional Array Of Classes
Posted: Fri Jun 11, 2010 7:54 pm
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.
Re: Creating A Multidimentional Array Of Classes
Posted: Fri Jun 11, 2010 9:01 pm
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;
Re: Creating A Multidimentional Array Of Classes
Posted: Sat Jun 12, 2010 4:17 am
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
Re: Creating A Multidimentional Array Of Classes
Posted: Sat Jun 12, 2010 8:53 am
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.
Re: Creating A Multidimentional Array Of Classes
Posted: Mon Jun 14, 2010 5:50 pm
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...
IDK what to do... Its due on Wednesday and I still have to get the socket connection working too...