A heap-allocated texture class for a console renderer I am working on a console renderer and I want a heap-allocated dynamic texture class. Since I am quite new to C++, I just want to check the memory management, if I destroy all pointers correctly etc.

class Texture {
private :

char** pixels; // heap allocated
int width, height;

public :

Texture(int width, int height, const char* texture) {
    this-&gt;width = width;
    this-&gt;height = height;

    pixels = new char*[width];
    for (int x = 0; x &lt; width; x++) {
        pixels[x] = new char[height];

        for (int y = 0; y &lt; height; y++)
            pixels[x][y] = texture[y*width+x];  

    }

}

Texture(int width, int height, char fill) {

    this-&gt;width = width;
    this-&gt;height = height;

    pixels = new char*[width];
    for (int x = 0; x &lt; width; x++) {
        pixels[x] = new char[height];

        for (int y = 0; y &lt; height; y++)
            pixels[x][y] = fill;    

    }

}

~Texture() {
    for (int x = 0; x &lt; width; x++)
        delete[] pixels[x];

    delete pixels;
}

Texture getSubTexture(int x, int y, int width, int height) {

    Texture newtex(width, height, '?');

    for (int xx = 0; xx &lt; width; xx++)
        for (int yy = 0; yy &lt; height; yy++) {
            newtex.setPixel(xx, yy, getPixel(x+xx, y+yy));
        }

    return newtex;

}

void setPixel(int x, int y, char c) {
    pixels[x][y] = c;
}

char getPixel(int x, int y) {
    return pixels[x][y];
}

int getWidth() { return width; }
int getHeight() { return height; }

};
getHeight and getWidth should be const.

int getWidth() const { return width; }
int getHeight() const { return height; }


Your destructor does not properly delete the pixels pointer.  Since you allocate it with new[] you need to use delete[].

delete [] pixels;


You're storing your textures in column-major order.  Depending on how you access them, this can cause performance issues with caching.  For example, in the constructor, when you copy in the initial texture values you jump thru memory (one byte every width bytes) rather than reading it sequentially (where x would be the inner loop).

Then there's the inevitable question of why you're using manual memory management, rather than using vector.