Velocity Reviews > questions about dynamic allocations

luke_2003
Guest
Posts: n/a

 06-04-2004

I have a pointer to point to data array.

I have a for loop. And I need to define different array size in the for
loop.

for example:

iter=4;
float **xp;

for(int i=0; i<4; i++){

xs1=(int)(floor(xdim/(iter-i)));
ys1=(int)(floor(ydim/(iter-i)));

xp=new float *[xs1];
for(j=0; j<xs1; j++){
xp[j]=new float[ys1];
}

//implementations
}

My question is where should I delete the pointer, inside of the for loop or
outside of the for loop.

If outside of the for loop, what happend to those allocated memory at each
for loop.

Thanks!

Luke

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*** Usenet.com - The #1 Usenet Newsgroup Service on The Planet! ***
http://www.usenet.com
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Barry Schwarz
Guest
Posts: n/a

 06-04-2004
On Thu, 3 Jun 2004 20:40:25 -0400, "luke_2003" <(E-Mail Removed)>
wrote:

>
>I have a pointer to point to data array.
>
>I have a for loop. And I need to define different array size in the for
>loop.
>
>for example:
>
>iter=4;
>float **xp;
>
>for(int i=0; i<4; i++){
>
> xs1=(int)(floor(xdim/(iter-i)));
> ys1=(int)(floor(ydim/(iter-i)));
>
> xp=new float *[xs1];

You obviously want comp.lang.c++, down the hall, 2nd door on the
right, but watch out for the wet paint on the door knob.

> for(j=0; j<xs1; j++){
> xp[j]=new float[ys1];
> }
>
>//implementations
>}
>
>My question is where should I delete the pointer, inside of the for loop or
>outside of the for loop.

You delete your pointers only after you are done using the memory they
point to.

>
>If outside of the for loop, what happend to those allocated memory at each
>for loop.

Nothing. Allocated memory remains allocated until you deallocate it.

>
>Thanks!
>
>
>Luke
>
>
>
>-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> *** Usenet.com - The #1 Usenet Newsgroup Service on The Planet! ***
> http://www.usenet.com
>-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

<<Remove the del for email>>