Jump to page: 1 2
Thread overview
How to delete a dynamically allocated multi-dimensional array?
Mar 27, 2003
Jens Friese
Mar 27, 2003
Nic Tiger
Mar 27, 2003
Jens Friese
Mar 27, 2003
Matthew Wilson
Mar 28, 2003
Nic Tiger
Mar 28, 2003
Matthew Wilson
Mar 28, 2003
Rajiv Bhagwat
Mar 28, 2003
Jens Friese
Mar 28, 2003
Rajiv Bhagwat
Mar 28, 2003
Matthew Wilson
Apr 01, 2003
KarL
Apr 12, 2003
Richard Grant
March 27, 2003
Hi,

I cannot figure out how to delete this array:

 	
float ***xbuffer,***ybuffer,***zbuffer;

...

void main(int argc, char* argv[]){

...

xbuffer = new float**[4];
ybuffer = new float**[4];
zbuffer = new float**[4];
for (i=0;i<4;i++) {
	xbuffer[i] = new float*[hsize[i]];
	ybuffer[i] = new float*[hsize[i]];
	zbuffer[i] = new float*[hsize[i]];
	for (j=0;j<hsize[i];j++) {
		xbuffer[i][j] = new float[vsize[i]];
		ybuffer[i][j] = new float[vsize[i]];
		zbuffer[i][j] = new float[vsize[i]];
	}
}


...


Any suggestions? Thanks a lot! :)

Best wishes

JENS

March 27, 2003
Just use reversed order (compared to what you have when allocated it)

for (i=0;i<4;i++) {
  for (j=0;j<hsize[i];j++) {
    delete xbuffer[i][j];
    delete ybuffer[i][j];
    delete zbuffer[i][j];
  }
  delete xbuffer[i];
  delete ybuffer[i];
  delete zbuffer[i];
}
delete xbuffer;
delete ybuffer;
delete zbuffer;

As these arrays are simple type, you can simply use free() function instead
of delete operator.

Nic Tiger.

"Jens Friese" <j.friese@highfisch.de> ???????/???????? ? ???????? ?????????: news:3E8318B4.20307@highfisch.de...
> Hi,
>
> I cannot figure out how to delete this array:
>
>
> float ***xbuffer,***ybuffer,***zbuffer;
>
> ...
>
> void main(int argc, char* argv[]){
>
> ...
>
> xbuffer = new float**[4];
> ybuffer = new float**[4];
> zbuffer = new float**[4];
> for (i=0;i<4;i++) {
> xbuffer[i] = new float*[hsize[i]];
> ybuffer[i] = new float*[hsize[i]];
> zbuffer[i] = new float*[hsize[i]];
> for (j=0;j<hsize[i];j++) {
> xbuffer[i][j] = new float[vsize[i]];
> ybuffer[i][j] = new float[vsize[i]];
> zbuffer[i][j] = new float[vsize[i]];
> }
> }
>
>
> ...
>
>
> Any suggestions? Thanks a lot! :)
>
> Best wishes
>
> JENS
>


March 27, 2003
Thank you so much for helping me, Nic!
Jens

Nic Tiger wrote:
> Just use reversed order (compared to what you have when allocated it)
> 
> for (i=0;i<4;i++) {
>   for (j=0;j<hsize[i];j++) {
>     delete xbuffer[i][j];
>     delete ybuffer[i][j];
>     delete zbuffer[i][j];
>   }
>   delete xbuffer[i];
>   delete ybuffer[i];
>   delete zbuffer[i];
> }
> delete xbuffer;
> delete ybuffer;
> delete zbuffer;
> 
> As these arrays are simple type, you can simply use free() function instead
> of delete operator.
> 
> Nic Tiger.
> 
> "Jens Friese" <j.friese@highfisch.de> ???????/???????? ? ???????? ?????????:
> news:3E8318B4.20307@highfisch.de...
> 
>>Hi,
>>
>>I cannot figure out how to delete this array:
>>
>>
>>float ***xbuffer,***ybuffer,***zbuffer;
>>
>>...
>>
>>void main(int argc, char* argv[]){
>>
>>...
>>
>>xbuffer = new float**[4];
>>ybuffer = new float**[4];
>>zbuffer = new float**[4];
>>for (i=0;i<4;i++) {
>>xbuffer[i] = new float*[hsize[i]];
>>ybuffer[i] = new float*[hsize[i]];
>>zbuffer[i] = new float*[hsize[i]];
>>for (j=0;j<hsize[i];j++) {
>>xbuffer[i][j] = new float[vsize[i]];
>>ybuffer[i][j] = new float[vsize[i]];
>>zbuffer[i][j] = new float[vsize[i]];
>>}
>>}
>>
>>
>>...
>>
>>
>>Any suggestions? Thanks a lot! :)
>>
>>Best wishes
>>
>>JENS
>>
> 
> 
> 

March 27, 2003
I'm sorry, but you cannot possibly justify allocating a resource with one API and deallocating it with another, particularly when the language standard proscribes such action.

It's bad practise. It's not maintainable. It's a bad example. It contravenes the standard. It has no benefit whatsoever.

This is wrong, wrong, wrong!

Soap-box Simon

"Nic Tiger" <nictiger@progtech.ru> wrote in message news:b5v9ml$1u8g$1@digitaldaemon.com...
> Just use reversed order (compared to what you have when allocated it)
>
> for (i=0;i<4;i++) {
>   for (j=0;j<hsize[i];j++) {
>     delete xbuffer[i][j];
>     delete ybuffer[i][j];
>     delete zbuffer[i][j];
>   }
>   delete xbuffer[i];
>   delete ybuffer[i];
>   delete zbuffer[i];
> }
> delete xbuffer;
> delete ybuffer;
> delete zbuffer;
>
> As these arrays are simple type, you can simply use free() function
instead
> of delete operator.
>
> Nic Tiger.
>
> "Jens Friese" <j.friese@highfisch.de> ???????/???????? ? ????????
?????????:
> news:3E8318B4.20307@highfisch.de...
> > Hi,
> >
> > I cannot figure out how to delete this array:
> >
> >
> > float ***xbuffer,***ybuffer,***zbuffer;
> >
> > ...
> >
> > void main(int argc, char* argv[]){
> >
> > ...
> >
> > xbuffer = new float**[4];
> > ybuffer = new float**[4];
> > zbuffer = new float**[4];
> > for (i=0;i<4;i++) {
> > xbuffer[i] = new float*[hsize[i]];
> > ybuffer[i] = new float*[hsize[i]];
> > zbuffer[i] = new float*[hsize[i]];
> > for (j=0;j<hsize[i];j++) {
> > xbuffer[i][j] = new float[vsize[i]];
> > ybuffer[i][j] = new float[vsize[i]];
> > zbuffer[i][j] = new float[vsize[i]];
> > }
> > }
> >
> >
> > ...
> >
> >
> > Any suggestions? Thanks a lot! :)
> >
> > Best wishes
> >
> > JENS
> >
>
>


March 28, 2003
Ok, than I suggest to allocate arrays also via malloc() rather than new operator, as long as the only difference between them is that new calls constructor afterwards.

Well, in general I admit that using different APIs is not good. Thanks for correction.

Nic Tiger.

"Matthew Wilson" <dmd@synesis.com.au> ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ ÓÌÅÄÕÀÝÅÅ: news:b5vpl0$2cqn$1@digitaldaemon.com...
> I'm sorry, but you cannot possibly justify allocating a resource with one API and deallocating it with another, particularly when the language standard proscribes such action.
>
> It's bad practise. It's not maintainable. It's a bad example. It
contravenes
> the standard. It has no benefit whatsoever.
>
> This is wrong, wrong, wrong!
>
> Soap-box Simon
>
> "Nic Tiger" <nictiger@progtech.ru> wrote in message news:b5v9ml$1u8g$1@digitaldaemon.com...
> > Just use reversed order (compared to what you have when allocated it)
> >
> > for (i=0;i<4;i++) {
> >   for (j=0;j<hsize[i];j++) {
> >     delete xbuffer[i][j];
> >     delete ybuffer[i][j];
> >     delete zbuffer[i][j];
> >   }
> >   delete xbuffer[i];
> >   delete ybuffer[i];
> >   delete zbuffer[i];
> > }
> > delete xbuffer;
> > delete ybuffer;
> > delete zbuffer;
> >
> > As these arrays are simple type, you can simply use free() function
> instead
> > of delete operator.
> >
> > Nic Tiger.
> >
> > "Jens Friese" <j.friese@highfisch.de> ???????/???????? ? ????????
> ?????????:
> > news:3E8318B4.20307@highfisch.de...
> > > Hi,
> > >
> > > I cannot figure out how to delete this array:
> > >
> > >
> > > float ***xbuffer,***ybuffer,***zbuffer;
> > >
> > > ...
> > >
> > > void main(int argc, char* argv[]){
> > >
> > > ...
> > >
> > > xbuffer = new float**[4];
> > > ybuffer = new float**[4];
> > > zbuffer = new float**[4];
> > > for (i=0;i<4;i++) {
> > > xbuffer[i] = new float*[hsize[i]];
> > > ybuffer[i] = new float*[hsize[i]];
> > > zbuffer[i] = new float*[hsize[i]];
> > > for (j=0;j<hsize[i];j++) {
> > > xbuffer[i][j] = new float[vsize[i]];
> > > ybuffer[i][j] = new float[vsize[i]];
> > > zbuffer[i][j] = new float[vsize[i]];
> > > }
> > > }
> > >
> > >
> > > ...
> > >
> > >
> > > Any suggestions? Thanks a lot! :)
> > >
> > > Best wishes
> > >
> > > JENS
> > >
> >
> >
>
>


March 28, 2003
Just curious: where are you using a structure which requires a pointer to a pointer to a pointer?

Would suggest to wrap up allocations and releases in a class constructor &
destructor (as advocated by www.relisoft.com) , so that compiler worries
about freeing in the right order and right amount, not you.
- Rajiv


"Jens Friese" <j.friese@highfisch.de> wrote in message news:3E8318B4.20307@highfisch.de...
> Hi,
>
> I cannot figure out how to delete this array:
>
>
> float ***xbuffer,***ybuffer,***zbuffer;
>
> ...
>
> void main(int argc, char* argv[]){
>
> ...
>
> xbuffer = new float**[4];
> ybuffer = new float**[4];
> zbuffer = new float**[4];
> for (i=0;i<4;i++) {
> xbuffer[i] = new float*[hsize[i]];
> ybuffer[i] = new float*[hsize[i]];
> zbuffer[i] = new float*[hsize[i]];
> for (j=0;j<hsize[i];j++) {
> xbuffer[i][j] = new float[vsize[i]];
> ybuffer[i][j] = new float[vsize[i]];
> zbuffer[i][j] = new float[vsize[i]];
> }
> }
>
>
> ...
>
>
> Any suggestions? Thanks a lot! :)
>
> Best wishes
>
> JENS
>


March 28, 2003
Hi everybody,

I hardly dare to say that I have no idea
what pointers are. I just have to create
this three-dimensional array during runtime
because it uses up to 800 mb (!) of memory.
Don't tell me that's too much - I do really need it ;)
Someone told me, to allocate memory this way -
and it works fine for me...

Can you tell me an equally easy method to do so
in a better way? I would be happy if you could
tell me about this destructor thing too.

Jens



Rajiv Bhagwat wrote:
> Just curious: where are you using a structure which requires a pointer to a
> pointer to a pointer?
> 
> Would suggest to wrap up allocations and releases in a class constructor &
> destructor (as advocated by www.relisoft.com) , so that compiler worries
> about freeing in the right order and right amount, not you.
> - Rajiv
> 
> 
> "Jens Friese" <j.friese@highfisch.de> wrote in message
> news:3E8318B4.20307@highfisch.de...
> 
>>Hi,
>>
>>I cannot figure out how to delete this array:
>>
>>
>>float ***xbuffer,***ybuffer,***zbuffer;
>>
>>...
>>
>>void main(int argc, char* argv[]){
>>
>>...
>>
>>xbuffer = new float**[4];
>>ybuffer = new float**[4];
>>zbuffer = new float**[4];
>>for (i=0;i<4;i++) {
>>xbuffer[i] = new float*[hsize[i]];
>>ybuffer[i] = new float*[hsize[i]];
>>zbuffer[i] = new float*[hsize[i]];
>>for (j=0;j<hsize[i];j++) {
>>xbuffer[i][j] = new float[vsize[i]];
>>ybuffer[i][j] = new float[vsize[i]];
>>zbuffer[i][j] = new float[vsize[i]];
>>}
>>}
>>
>>
>>...
>>
>>
>>Any suggestions? Thanks a lot! :)
>>
>>Best wishes
>>
>>JENS
>>
> 
> 
> 

March 28, 2003
Well, any book on 'c' will tell you about arrays and pointers. Any book on
'c++' will tell you about classes, their constructors and destructors. Lot
of sites about learning these languages will also explain these to you.
(Search using Google.com).
Best of luck,

- Rajiv



"Jens Friese" <j.friese@highfisch.de> wrote in message news:3E8404A3.2000108@highfisch.de...
> Hi everybody,
>
> I hardly dare to say that I have no idea
> what pointers are. I just have to create
> this three-dimensional array during runtime
> because it uses up to 800 mb (!) of memory.
> Don't tell me that's too much - I do really need it ;)
> Someone told me, to allocate memory this way -
> and it works fine for me...
>
> Can you tell me an equally easy method to do so
> in a better way? I would be happy if you could
> tell me about this destructor thing too.
>
> Jens
>
>
>
> Rajiv Bhagwat wrote:
> > Just curious: where are you using a structure which requires a pointer
to a
> > pointer to a pointer?
> >
> > Would suggest to wrap up allocations and releases in a class constructor
&
> > destructor (as advocated by www.relisoft.com) , so that compiler worries
> > about freeing in the right order and right amount, not you.
> > - Rajiv
> >
> >
> > "Jens Friese" <j.friese@highfisch.de> wrote in message news:3E8318B4.20307@highfisch.de...
> >
> >>Hi,
> >>
> >>I cannot figure out how to delete this array:
> >>
> >>
> >>float ***xbuffer,***ybuffer,***zbuffer;
> >>
> >>...
> >>
> >>void main(int argc, char* argv[]){
> >>
> >>...
> >>
> >>xbuffer = new float**[4];
> >>ybuffer = new float**[4];
> >>zbuffer = new float**[4];
> >>for (i=0;i<4;i++) {
> >>xbuffer[i] = new float*[hsize[i]];
> >>ybuffer[i] = new float*[hsize[i]];
> >>zbuffer[i] = new float*[hsize[i]];
> >>for (j=0;j<hsize[i];j++) {
> >>xbuffer[i][j] = new float[vsize[i]];
> >>ybuffer[i][j] = new float[vsize[i]];
> >>zbuffer[i][j] = new float[vsize[i]];
> >>}
> >>}
> >>
> >>
> >>...
> >>
> >>
> >>Any suggestions? Thanks a lot! :)
> >>
> >>Best wishes
> >>
> >>JENS
> >>
> >
> >
> >
>


March 28, 2003
Don't have a problem with that, other than that it is more work and more scope for error. I am sceptical that there is much, if any, performance gain to be had from going straight to malloc()/free() rather than new/delete.

Nonetheless, I am not a religious adherent to the notion that every mem alloc in C++ should be through new. My only objection was your mixing APIs, which is guaranteed to fail on many compilers, even if it works with DMC++

:)

Matthew


"Nic Tiger" <nictiger@progtech.ru> wrote in message news:b60kl4$31il$1@digitaldaemon.com...
> Ok, than I suggest to allocate arrays also via malloc() rather than new operator, as long as the only difference between them is that new calls constructor afterwards.
>
> Well, in general I admit that using different APIs is not good. Thanks for correction.
>
> Nic Tiger.
>
> "Matthew Wilson" <dmd@synesis.com.au> ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ
ÓÌÅÄÕÀÝÅÅ:
> news:b5vpl0$2cqn$1@digitaldaemon.com...
> > I'm sorry, but you cannot possibly justify allocating a resource with
one
> > API and deallocating it with another, particularly when the language standard proscribes such action.
> >
> > It's bad practise. It's not maintainable. It's a bad example. It
> contravenes
> > the standard. It has no benefit whatsoever.
> >
> > This is wrong, wrong, wrong!
> >
> > Soap-box Simon
> >
> > "Nic Tiger" <nictiger@progtech.ru> wrote in message news:b5v9ml$1u8g$1@digitaldaemon.com...
> > > Just use reversed order (compared to what you have when allocated it)
> > >
> > > for (i=0;i<4;i++) {
> > >   for (j=0;j<hsize[i];j++) {
> > >     delete xbuffer[i][j];
> > >     delete ybuffer[i][j];
> > >     delete zbuffer[i][j];
> > >   }
> > >   delete xbuffer[i];
> > >   delete ybuffer[i];
> > >   delete zbuffer[i];
> > > }
> > > delete xbuffer;
> > > delete ybuffer;
> > > delete zbuffer;
> > >
> > > As these arrays are simple type, you can simply use free() function
> > instead
> > > of delete operator.
> > >
> > > Nic Tiger.
> > >
> > > "Jens Friese" <j.friese@highfisch.de> ???????/???????? ? ????????
> > ?????????:
> > > news:3E8318B4.20307@highfisch.de...
> > > > Hi,
> > > >
> > > > I cannot figure out how to delete this array:
> > > >
> > > >
> > > > float ***xbuffer,***ybuffer,***zbuffer;
> > > >
> > > > ...
> > > >
> > > > void main(int argc, char* argv[]){
> > > >
> > > > ...
> > > >
> > > > xbuffer = new float**[4];
> > > > ybuffer = new float**[4];
> > > > zbuffer = new float**[4];
> > > > for (i=0;i<4;i++) {
> > > > xbuffer[i] = new float*[hsize[i]];
> > > > ybuffer[i] = new float*[hsize[i]];
> > > > zbuffer[i] = new float*[hsize[i]];
> > > > for (j=0;j<hsize[i];j++) {
> > > > xbuffer[i][j] = new float[vsize[i]];
> > > > ybuffer[i][j] = new float[vsize[i]];
> > > > zbuffer[i][j] = new float[vsize[i]];
> > > > }
> > > > }
> > > >
> > > >
> > > > ...
> > > >
> > > >
> > > > Any suggestions? Thanks a lot! :)
> > > >
> > > > Best wishes
> > > >
> > > > JENS
> > > >
> > >
> > >
> >
> >
>
>


March 28, 2003
Please don't apologise for lack of understanding. This community is very friendly, so please feel at ease. (Plus, you should see the number of stupid questions I pose on the D newsgroup. Ouch!)

In answer to your question, the STLSoft library (http://stlsoft.org/) has a
dynamically-sized (as opposed to sized at compile time) but not resizable
(once created, it cannot be resized) 3 dimensional array called
fixed_array_3d. (There are fixed_array_1d, _2d and _4d also.)

It supports multi-dimensional array notation, which is unchecked (though
asserts are used in debug mode), as well as checked access via the at(d0,
d1, d2) method

You would use it like

#include <stlsoft.h>
#include <stlsoft_fixed_array.h>

int main(int /* argc */, char ** /*argv*/)
{
 typedef stlsoft::fixed_array_3d<float> float_array_t;

 float_array_t array(4, 10, 20);

 array[0][1][2] = 12345.6789;

  for(int i = 0; i < dimension0(); ++i)
  {
    for(int j = 0; j < dimension1(); ++j)
    {
      for(int k = 0; k < dimension2(); ++k)
      {
            assert(array.at(i, j, k) == array[i][j][k]);
      }
    }
  }
}

Only one allocation is performed for the entire array, which means your use of memory would be more efficient both from a performance and size perspective. However, since the [] operators involve the creation of temporaries of fixed_array_2d and fixed_array_1d, there is a small performance penalty in their use. at() is more efficient, but still involves checks for the validity of the given indexers. I've plans to make an unchecked_at(d0, d1, d2) which would be extremely efficient, but I've not yet got round to it. Maybe if a user makes a request ... ;)

Hope that helps

Matthew

"Jens Friese" <j.friese@highfisch.de> wrote in message news:3E8404A3.2000108@highfisch.de...
> Hi everybody,
>
> I hardly dare to say that I have no idea
> what pointers are. I just have to create
> this three-dimensional array during runtime
> because it uses up to 800 mb (!) of memory.
> Don't tell me that's too much - I do really need it ;)
> Someone told me, to allocate memory this way -
> and it works fine for me...
>
> Can you tell me an equally easy method to do so
> in a better way? I would be happy if you could
> tell me about this destructor thing too.
>
> Jens
>
>
>
> Rajiv Bhagwat wrote:
> > Just curious: where are you using a structure which requires a pointer
to a
> > pointer to a pointer?
> >
> > Would suggest to wrap up allocations and releases in a class constructor
&
> > destructor (as advocated by www.relisoft.com) , so that compiler worries
> > about freeing in the right order and right amount, not you.
> > - Rajiv
> >
> >
> > "Jens Friese" <j.friese@highfisch.de> wrote in message news:3E8318B4.20307@highfisch.de...
> >
> >>Hi,
> >>
> >>I cannot figure out how to delete this array:
> >>
> >>
> >>float ***xbuffer,***ybuffer,***zbuffer;
> >>
> >>...
> >>
> >>void main(int argc, char* argv[]){
> >>
> >>...
> >>
> >>xbuffer = new float**[4];
> >>ybuffer = new float**[4];
> >>zbuffer = new float**[4];
> >>for (i=0;i<4;i++) {
> >>xbuffer[i] = new float*[hsize[i]];
> >>ybuffer[i] = new float*[hsize[i]];
> >>zbuffer[i] = new float*[hsize[i]];
> >>for (j=0;j<hsize[i];j++) {
> >>xbuffer[i][j] = new float[vsize[i]];
> >>ybuffer[i][j] = new float[vsize[i]];
> >>zbuffer[i][j] = new float[vsize[i]];
> >>}
> >>}
> >>
> >>
> >>...
> >>
> >>
> >>Any suggestions? Thanks a lot! :)
> >>
> >>Best wishes
> >>
> >>JENS
> >>
> >
> >
> >
>


« First   ‹ Prev
1 2