Allocation of base member from derived class

Hello,
I'm working on implementation of container class to hold
image data. What I want is to create a base class pixelContainer
which contains a pointer to pixel data and implements an iterator
to access it.
The memory managment of pixel data shall be
implemented in derived class as I want one derived class to
allocate the data dynamically and other one to use
memory on stack.
I also need to implement a copy constructor
in the base class which would dynamically allocate memory
and copy the pixel data.
Now the problem is how do I deallocate it?
a copy constructor...which would dynamically allocate memory


de-allocate in the destructor

base class -> allocate memory in constructor -> release same memory in base class destructor
child1 -> allocate different memory in constructor -> release same memory in child1 destructor
child2 -> use stack -> no worried here

That is so patronising I'm sure I misunderstood the question, could you elaborate? Maybe post some code?
Thank you for your reply,
the problem arises with the copy constructor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class PixelContainer
{
  private:
    char *data;

  public:
  PixelContainer(PixelContainer &obj){ // copy constructor
    this->data = new char[10];
    memcpy(this->data, obj.data, 10);
  }
  // iterator implementation...
}

class Image1 : public PixelContainer
{
  PixelContainer()
  {
    this->data = new char [10];
  }
  ~PixelContainer()
  {
    delete[] this->data;
  }
}

class Image2 : public PixelContainer
{
  PixelContainer(char *ptr)
  {
    this->data = ptr;
  }
}


Example usage:
1
2
3
4
5
6
7
8
9
10
11
char array[10];
Image1 img1;
Image2 img2(&array);

{
  PixelContainer &rimg1 = img1;
  PixelContainer &rimg2 = img2; 
  
  PixelContainer copy1(rimg1);
  PixelContainer copy2(rimg2);
}

Now as both copies get out of scope, the memory allocated by the copy constructor is not de-allocated.
Last edited on
The memory managment of pixel data shall be
implemented in derived class as I want one derived class to
allocate the data dynamically and other one to use
memory on stack.

This sounds like a lot of complication for premature optimization to me.
I also need to implement a copy constructor
in the base class which would dynamically allocate memory
and copy the pixel data.

So if you create an instance of the class that uses memory on the stack, and then copy a pixelContainer to it, then it now manages memory on the heap?

Simplify. Create a single pixelContainer class. Have it provide access to the pixels and manage the memory.
Well it seems to me quite useful to let the most specific class manage the memory as the most specific class feeds in the data.

So if you create an instance of the class that uses memory on the stack, and then copy a pixelContainer to it, then it now manages memory on the heap?

If you create a copy of PixelContainer no matter whether it actually holds data on stack or heap,
the copy would hold it on heap.
Last edited on
Topic archived. No new replies allowed.