How to delete arrays that are passed as function return values?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Oliver Graeser

    How to delete arrays that are passed as function return values?

    Hi All,

    I do some ensemble averaging calculation that requires me to accumulate
    very many distributions which I store in arrays. Basically something like

    class AdaptiveNW{
    public:
    int** getDegreeDistri bution();
    .......
    }

    AdaptiveNW::get DegreeDistribut ion(){
    int* CN= new int[10000];
    int* ErrN=new int[10000];
    int** res = new int* [2];
    ..........
    res[0]=CN;
    res[1]=ErrN;
    return res;
    }


    ........

    AdaptiveNW *anw;
    int **accArray;
    for(i=0; i<10000; i++){
    anw = new AdaptiveNW();
    accArray=anw->getDegreeDistr ibution();
    .......//adding up accArrays
    delete []accArray;
    delete anw;
    }
    ......

    nonetheless my memory usage increases steadily. How do I free the memory
    occupied by the return arrays correctly?

    Thanks, Oliver
  • Triple-DES

    #2
    Re: How to delete arrays that are passed as function return values?

    On 21 Aug, 07:16, Oliver Graeser <grae...@phy.cu hk.edu.hkwrote:
    Hi All,
    >
    I do some ensemble averaging calculation that requires me to accumulate
    very many distributions which I store in arrays. Basically something like
    >
    AdaptiveNW::get DegreeDistribut ion(){
            int* CN= new int[10000];
    std::vector<int CN(10000);
            int* ErrN=new int[10000];
    std::vector<int ErrN(10000);
            int** res = new int* [2];
    std::vector< std::vector<int res;
            ..........
            res[0]=CN;
            res[1]=ErrN;
    res.push_back(C N);
    res.push_back(E rrN);
            return res;
    >
    }
    Problem solved.

    [snip]
    >
    nonetheless my memory usage increases steadily. How do I free the memory
    occupied by the return arrays correctly?
    >
    Thanks, Oliver
    Don't use dynamically allocated arrays. Sorry for spoonfeeding :)

    DP

    Comment

    • Pascal J. Bourguignon

      #3
      Re: How to delete arrays that are passed as function return values?

      "Alf P. Steinbach" <alfps@start.no writes:
      * Oliver Graeser:
      >I do some ensemble averaging calculation that requires me to
      >accumulate very many distributions which I store in
      >arrays. [...]
      >nonetheless my memory usage increases steadily. How do I free the
      >memory occupied by the return arrays correctly?
      >
      Return small items by value, use standard library container types such
      as std::vector, and *don't* use raw arrays and raw pointers.
      That doesn't help a lot, std::vectors are not deleted automatically either.

      The OP should use a garbage collector (as almost everybody else).

      --
      __Pascal Bourguignon__

      Comment

      • ave

        #4
        Re: How to delete arrays that are passed as function return values?

        I would say, don't return arrays as return values.

        The example code you gave has no sense of ownership over the allocated
        memory, so it is difficult to deallocate. When such an issue crops up in
        code you've written, you should assume that there is a better way of doing
        it.

        From your example, I would suggest you write a 'DegreeDistribu tion' object
        and have that used as the target store of your function. Rather than
        building an array locally and returning it.

        For example (ignore incorrect\incom plete code, it's just a demonstration):

        class DegreeDistribut ion
        {
        size_t m_nDistribution s;
        int** m_distributionL ist;

        public:
        DegreeDistribut ion();
        ~DegreeDistribu tion();

        void AddDistribution ( int iIndex, int iValue );
        };

        DegreeDistribut ion::~DegreeDis tribution()
        {
        for ( size_t loop = 0; loop < m_nDistribution s; ++loop )
        {
        delete [] m_distributionL ist[ loop ];
        }

        delete [] m_distrtibution List;
        }

        Then change your getDegreeDistri bution to resemble:

        void getDegreeDistri bution( DegreeDistribut ion &distributio n );

        Rather than building a local array and returning it, the method can now add
        values to the object and the object can delete it with full knowledge of the
        memory it owns. I'm fairly in the pool of developers that think memory
        should be deleted from the same place it was allocated.

        You should find that 'getDegreeDistr ibution' ends up a much tidier method
        also.

        ave


        Comment

        • peter koch

          #5
          Re: How to delete arrays that are passed as function return values?

          On 21 Aug., 14:09, p...@informatim ago.com (Pascal J. Bourguignon)
          wrote:
          "Alf P. Steinbach" <al...@start.no writes:
          >
          * Oliver Graeser:
          I do some ensemble averaging calculation that requires me to
          accumulate very many distributions which I store in
          arrays. [...]
          nonetheless my memory usage increases steadily. How do I free the
          memory occupied by the return arrays correctly?
          >
          Return small items by value, use standard library container types such
          as std::vector, and *don't* use raw arrays and raw pointers.
          >
          That doesn't help a lot, std::vectors are not deleted automatically either.
          you can't delete a std::vector, but they are destructed automatically
          when they leave scope - see the example given by Triple-DES.
          >
          The OP should use a garbage collector (as almost everybody else).
          Almost everybody else? In a C++ context, this statement is clearly
          wrong.

          /Peter

          Comment

          • mohi

            #6
            Re: How to delete arrays that are passed as function return values?

            On Aug 21, 9:09 pm, peter koch <peter.koch.lar ...@gmail.comwr ote:
            On 21 Aug., 14:09, p...@informatim ago.com (Pascal J. Bourguignon)
            wrote:
            >
            "Alf P. Steinbach" <al...@start.no writes:
            >
            * Oliver Graeser:
            >I do some ensemble averaging calculation that requires me to
            >accumulate very many distributions which I store in
            >arrays. [...]
            >nonetheless my memory usage increases steadily. How do I free the
            >memory occupied by the return arrays correctly?
            >
            Return small items by value, use standard library container types such
            as std::vector, and *don't* use raw arrays and raw pointers.
            >
            That doesn't help a lot, std::vectors are not deleted automatically either.
            >
            you can't delete a std::vector, but they are destructed automatically
            when they leave scope - see the example given by Triple-DES.
            >
            >
            >
            The OP should use a garbage collector (as almost everybody else).
            >
            Almost everybody else? In a C++ context, this statement is clearly
            wrong.
            >
            /Peter
            i dont understand can't we delete the above newED array's as

            accarray=anew->getDegreeDistr ibution;

            delete [] *accarray[0];
            delete [] *accarray[1];
            delete [] accarray;


            won't this work fine???

            mohan

            Comment

            • peter koch

              #7
              Re: How to delete arrays that are passed as function return values?

              On 21 Aug., 18:41, mohi <mohangupt...@g mail.comwrote:
              On Aug 21, 9:09 pm, peter koch <peter.koch.lar ...@gmail.comwr ote:
              >
              >
              >
              >
              >
              On 21 Aug., 14:09, p...@informatim ago.com (Pascal J. Bourguignon)
              wrote:
              >
              "Alf P. Steinbach" <al...@start.no writes:
              >
              * Oliver Graeser:
              I do some ensemble averaging calculation that requires me to
              accumulate very many distributions which I store in
              arrays. [...]
              nonetheless my memory usage increases steadily. How do I free the
              memory occupied by the return arrays correctly?
              >
              Return small items by value, use standard library container types such
              as std::vector, and *don't* use raw arrays and raw pointers.
              >
              That doesn't help a lot, std::vectors are not deleted automatically either.
              >
              you can't delete a std::vector, but they are destructed automatically
              when they leave scope - see the example given by Triple-DES.
              >
              The OP should use a garbage collector (as almost everybody else).
              >
              Almost everybody else? In a C++ context, this statement is clearly
              wrong.
              >
              /Peter
              >
              i dont understand can't we delete the above newED array's as
              >
              accarray=anew->getDegreeDistr ibution;
              >
              delete [] *accarray[0];
              delete [] *accarray[1];
              delete [] accarray;
              >
              won't this work fine???
              >
              mohan
              I only skimmed the code, but the answer is simple: if and only if you
              allocate something with new [], you release it with delete [].
              The real answer is to "never" use new []. new [] dates from before
              templates and the usage has been made obsolete with the availability
              of std::vector.

              /Peter

              Comment

              • Jorgen Grahn

                #8
                Re: How to delete arrays that are passed as function return values?

                On Thu, 21 Aug 2008 14:09:33 +0200, Pascal J. Bourguignon <pjb@informatim ago.comwrote:
                "Alf P. Steinbach" <alfps@start.no writes:
                >
                >* Oliver Graeser:
                >>I do some ensemble averaging calculation that requires me to
                >>accumulate very many distributions which I store in
                >>arrays. [...]
                >>nonetheless my memory usage increases steadily. How do I free the
                >>memory occupied by the return arrays correctly?
                >>
                >Return small items by value, use standard library container types such
                >as std::vector, and *don't* use raw arrays and raw pointers.
                >
                That doesn't help a lot, std::vectors are not deleted automatically either.
                They are, unless you 'new' them. I don't see any reason why the
                poster should have to use new, if he used std::vector.
                The OP should use a garbage collector (as almost everybody else).
                You are trolling, right? I think I have read about one or two people
                using GC for very specific tasks.

                Unless you're counting things like boost::shared_p ointer.

                /Jorgen

                --
                // Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
                \X/ snipabacken.se R'lyeh wgah'nagl fhtagn!

                Comment

                • Pascal J. Bourguignon

                  #9
                  Re: How to delete arrays that are passed as function return values?

                  Jorgen Grahn <grahn+nntp@sni pabacken.sewrit es:
                  On Thu, 21 Aug 2008 14:09:33 +0200, Pascal J. Bourguignon <pjb@informatim ago.comwrote:
                  >"Alf P. Steinbach" <alfps@start.no writes:
                  >>
                  >>* Oliver Graeser:
                  >>>I do some ensemble averaging calculation that requires me to
                  >>>accumulate very many distributions which I store in
                  >>>arrays. [...]
                  >>>nonetheles s my memory usage increases steadily. How do I free the
                  >>>memory occupied by the return arrays correctly?
                  >>>
                  >>Return small items by value, use standard library container types such
                  >>as std::vector, and *don't* use raw arrays and raw pointers.
                  >>
                  >That doesn't help a lot, std::vectors are not deleted automatically either.
                  >
                  They are,
                  Yes, sorry I was wrong, indeed temporary objects are cleanly destroyed.

                  unless you 'new' them. I don't see any reason why the
                  poster should have to use new, if he used std::vector.
                  For example, to avoid copying the vector?


                  --
                  __Pascal Bourguignon__

                  Comment

                  • peter koch

                    #10
                    Re: How to delete arrays that are passed as function return values?

                    On 22 Aug., 13:01, p...@informatim ago.com (Pascal J. Bourguignon)
                    wrote:
                    Jorgen Grahn <grahn+n...@sni pabacken.sewrit es:
                    On Thu, 21 Aug 2008 14:09:33 +0200, Pascal J. Bourguignon <p...@informati mago.comwrote:
                    "Alf P. Steinbach" <al...@start.no writes:
                    >
                    >* Oliver Graeser:
                    >>I do some ensemble averaging calculation that requires me to
                    >>accumulate very many distributions which I store in
                    >>arrays. [...]
                    >>nonetheless my memory usage increases steadily. How do I free the
                    >>memory occupied by the return arrays correctly?
                    >
                    >Return small items by value, use standard library container types such
                    >as std::vector, and *don't* use raw arrays and raw pointers.
                    >
                    That doesn't help a lot, std::vectors are not deleted automatically either.
                    >
                    They are,
                    >
                    Yes, sorry I was wrong, indeed temporary objects are cleanly destroyed.
                    >
                    unless you 'new' them.  I don't see any reason why the
                    poster should have to use new, if he used std::vector.
                    >
                    For example, to avoid copying the vector?
                    >
                    What copy? The copy will most likely be optimized away (I know of no
                    modern C++ comipler that does not). Alternatively, you could have the
                    array as a parameter, but this is less elegant.

                    /Peter

                    Comment

                    Working...