I have just run into ridiculous problems with arrays, and it appears I have no idea how they work, so I need some help.

I have this class:

class pixelmap{

public:
    pixelmap(const uint& width, const uint& height);
    pixelmap();
    ~pixelmap();

    uint width;
    uint height;
    uint *values;
    pixel *pixels;

    void setValues();
    void set(const uint& width, const uint& height);
private:
};

I'm trying to use the pixels pointer to put a multidimensional array on the heap, like in the constructor:

pixelmap::pixelmap(const uint& width, const uint& height){
    this->width = width;
    this->height = height;
    this->values = new uint[width][height];
    this->pixels = new pixel[width][height];
}

First of all, am I initializing this right? And, how do I access each r, g, and b in the pixels array? Ive tried many different ways, and I just keep getting errors:

p.pixels[x, y].r;  //I get a warning about the left hand of the operand not having an effect
p.pixels[x][y].r;  //no match for operator[] error

Also, can I just delete[] it, or do I have to delete[] it for every row? I'm at a loss here, and pretty frustrated.

pixelmap::pixels is NOT a multi-dimensional array -- it's a single dimentional array. You have to add one star for each dimension. For example, for a 2 dimension array you declare it as pixel** pixels; Then allocate it like this:

this>pixels = new pixels*[height];
for(int i = 0; i < height; i++)
   this->pixels[i] = new pixels[width];

pixelmap::pixels is NOT a multi-dimensional array -- it's a single dimentional array. You have to add one star for each dimension. For example, for a 2 dimension array you declare it as pixel** pixels; Then allocate it like this:

this>pixels = new pixels*[height];
for(int i = 0; i < height; i++)
   this->pixels[i] = new pixels[width];

All right, thanks. So I access the data with:

p.pixels[x][y].r;

right?

Ancient Dragon is nearly perfectly correct in what he said, but can I say that you have to think about the deletion of the memory as well.

First off, he has made a couple of typos, however, I think that is caused by fiolet.
The AD code should be:

this->pixels = new pixel*[height];
for(int i = 0; i < height; i++)
   this->pixels[i] = new pixel[width];

Now I strongly suspect that AD put that there deliberately to see if we are all awake. :) But it illustrates a key point, you should not name variables and classes by similar names, especially names that are just one tiny typo away from each other.
It just makes reading the code SO difficult. [and leads to errors].

The deletion should then be this:

for(int i=0;i<height;i++)
  delete [] pixels[i];
delete [] pixels;

However, I slightly prefer to allocate like this:
NOTE: I have swapped width and height because I noticed that you were using [x][y] implying that y is the height.

// pixels defined as : pixel** pixels;
pixels=new pixel*[width];
pixels[0]=new pixel[height*width];
for(int i=1;i<width;i++)
  pixels[i]= pixels[0]+i*height;
// ....
// Deletions:
delete [] pixels[0];
delete [] pixels;

My preference for this is that you don't have an extra variable in the delete sequence and you get a continuous block of memory. However, I appreciate that there is epsilon difference overall between the two methods.

Finally, both methods allow this:

struct pixel
{
   // other stuff...
   int r;
};

// access can be had by using
pixelmap PMap;
// set pixels:
int value = PMap.pixels[x][y].r;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.