questions about dynamic allocations

L

luke_2003

**** Post for FREE via your newsreader at post.usenet.com ****

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
Unlimited Download - 19 Seperate Servers - 90,000 groups - Uncensored
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
B

Barry Schwarz

**** Post for FREE via your newsreader at post.usenet.com ****

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
Unlimited Download - 19 Seperate Servers - 90,000 groups - Uncensored
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=



<<Remove the del for email>>
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top