bitmap array

S

Sebastor

Hi

I create bitmap array :

Graphics::TBitmap ***bitmap;

bitmap = new Graphics::TBitmap**[x];
for (int s=0;s<x;s++)
{
bitmap = new Graphics::TBitmap*[y];
for (int u=0;u<y;u++)
bitmap = new Graphics::TBitmap;
}

How can i delete this array ?

for (int s=0;s<x;s++)
{
for (int u=0;u<y;u++)
delete bitmap;
delete bitmap;
}

or I have to use "delete[]" command?

regards

Sebastor
 
V

Victor Bazarov

Sebastor said:
I create bitmap array :

Graphics::TBitmap ***bitmap;

bitmap = new Graphics::TBitmap**[x];
for (int s=0;s<x;s++)
{
bitmap = new Graphics::TBitmap*[y];
for (int u=0;u<y;u++)
bitmap = new Graphics::TBitmap;


Since individual element is created using "regular" 'new'...
}

How can i delete this array ?

for (int s=0;s<x;s++)
{
for (int u=0;u<y;u++)
delete bitmap;


You should use "regular" 'delete'
delete bitmap;


But since 'bitmap' was created using 'new[]', you must use 'delete[]':

delete[] bitmap;

And you forgot to delete the 'bitmap' itself here...
or I have to use "delete[]" command?

Depends. See above.

V
 
B

Bob Hairgrove

Hi

I create bitmap array :

Graphics::TBitmap ***bitmap;

bitmap = new Graphics::TBitmap**[x];
for (int s=0;s<x;s++)
{
bitmap = new Graphics::TBitmap*[y];
for (int u=0;u<y;u++)
bitmap = new Graphics::TBitmap;
}

How can i delete this array ?

for (int s=0;s<x;s++)
{
for (int u=0;u<y;u++)
delete bitmap;
delete bitmap;
}

or I have to use "delete[]" command?

regards

Sebastor


The rule is, use delete[] wherever you used new[]. You used new[]
sometimes, and new at other times. Don't become confused by the array
subscripts, which have nothing to do with delete[].
 
D

Daniel T.

"Sebastor said:
Hi

I create bitmap array :

Graphics::TBitmap ***bitmap;

bitmap = new Graphics::TBitmap**[x];
for (int s=0;s<x;s++)
{
bitmap = new Graphics::TBitmap*[y];
for (int u=0;u<y;u++)
bitmap = new Graphics::TBitmap;
}

How can i delete this array ?

for (int s=0;s<x;s++)
{
for (int u=0;u<y;u++)
delete bitmap;
delete bitmap;
}

or I have to use "delete[]" command?


This is a great example why you should use the standard containers...

template < typename T >
class Array {
int _x;
std::deque< T > _array;
public:
Array( int x, int y ): _x( x ), _array( x * y ) { }
const T& operator()( int x, int y ) const {
return _array[ x * _x + y ];
}
T& operator()( int x, int y ) {
return _array[ x * _x + y ];
}
};

Now create your array:

Array< Graphics::TBitmap > bitmap( x, y );

You get proper destruction as well as copy semantics for free.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top