questions about dynamic allocations

Discussion in 'C Programming' started by luke_2003, Jun 4, 2004.

  1. luke_2003

    luke_2003 Guest

    **** 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
    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
     
    luke_2003, Jun 4, 2004
    #1
    1. Advertising

  2. On Thu, 3 Jun 2004 20:40:25 -0400, "luke_2003" <>
    wrote:

    >**** 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>>
     
    Barry Schwarz, Jun 4, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GG
    Replies:
    4
    Views:
    552
  2. Vince Darley
    Replies:
    4
    Views:
    4,432
    emilchacko
    Mar 2, 2010
  3. MSG
    Replies:
    23
    Views:
    879
    Rob Thorpe
    Jan 29, 2004
  4. Manuel Massing

    tracking memory allocations by class

    Manuel Massing, Jul 16, 2004, in forum: C++
    Replies:
    1
    Views:
    393
    Michiel Salters
    Jul 16, 2004
  5. luke_2003

    Re: questions about dynamic allocations

    luke_2003, Jun 4, 2004, in forum: C Programming
    Replies:
    1
    Views:
    301
    Barry Schwarz
    Jun 5, 2004
Loading...

Share This Page