Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Oliver Graeser
Guest
 
Posts: n/a
#1: Aug 21 '08
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** getDegreeDistribution();
.......
}

AdaptiveNW::getDegreeDistribution(){
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->getDegreeDistribution();
.......//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
Guest
 
Posts: n/a
#2: Aug 21 '08

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


On 21 Aug, 07:16, Oliver Graeser <grae...@phy.cuhk.edu.hkwrote:
Quote:
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::getDegreeDistribution(){
* * * * int* CN= new int[10000];
std::vector<intCN(10000);
Quote:
* * * * int* ErrN=new int[10000];
std::vector<intErrN(10000);
Quote:
* * * * int** res = new int* [2];
std::vector< std::vector<int res;
Quote:
* * * * ..........
* * * * res[0]=CN;
* * * * res[1]=ErrN;
res.push_back(CN);
res.push_back(ErrN);
Quote:
* * * * return res;
>
}
Problem solved.

[snip]
Quote:
>
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
Pascal J. Bourguignon
Guest
 
Posts: n/a
#3: Aug 21 '08

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


"Alf P. Steinbach" <alfps@start.nowrites:
Quote:
* Oliver Graeser:
Quote:
>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__
ave
Guest
 
Posts: n/a
#4: Aug 21 '08

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 'DegreeDistribution' 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\incomplete code, it's just a demonstration):

class DegreeDistribution
{
size_t m_nDistributions;
int** m_distributionList;

public:
DegreeDistribution();
~DegreeDistribution();

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

DegreeDistribution::~DegreeDistribution()
{
for ( size_t loop = 0; loop < m_nDistributions; ++loop )
{
delete [] m_distributionList[ loop ];
}

delete [] m_distrtibutionList;
}

Then change your getDegreeDistribution to resemble:

void getDegreeDistribution( DegreeDistribution &distribution );

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 'getDegreeDistribution' ends up a much tidier method
also.

ave


peter koch
Guest
 
Posts: n/a
#5: Aug 21 '08

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


On 21 Aug., 14:09, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
Quote:
"Alf P. Steinbach" <al...@start.nowrites:
>
Quote:
* Oliver Graeser:
Quote:
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?
>
Quote:
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.
Quote:
>
The OP should use a garbage collector (as almost everybody else).
Almost everybody else? In a C++ context, this statement is clearly
wrong.

/Peter
mohi
Guest
 
Posts: n/a
#6: Aug 21 '08

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


On Aug 21, 9:09 pm, peter koch <peter.koch.lar...@gmail.comwrote:
Quote:
On 21 Aug., 14:09, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
>
Quote:
"Alf P. Steinbach" <al...@start.nowrites:
>
Quote:
Quote:
* 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?
>
Quote:
Quote:
Return small items by value, use standard library container types such
as std::vector, and *don't* use raw arrays and raw pointers.
>
Quote:
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.
>
>
>
Quote:
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->getDegreeDistribution;

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


won't this work fine???

mohan
peter koch
Guest
 
Posts: n/a
#7: Aug 21 '08

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


On 21 Aug., 18:41, mohi <mohangupt...@gmail.comwrote:
Quote:
On Aug 21, 9:09 pm, peter koch <peter.koch.lar...@gmail.comwrote:
>
>
>
>
>
Quote:
On 21 Aug., 14:09, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
>
Quote:
Quote:
"Alf P. Steinbach" <al...@start.nowrites:
>
Quote:
Quote:
* 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?
>
Quote:
Quote:
Return small items by value, use standard library container types such
as std::vector, and *don't* use raw arrays and raw pointers.
>
Quote:
Quote:
That doesn't help a lot, std::vectors are not deleted automatically either.
>
Quote:
you can't delete a std::vector, but they are destructed automatically
when they leave scope - see the example given by Triple-DES.
>
Quote:
Quote:
The OP should use a garbage collector (as almost everybody else).
>
Quote:
Almost everybody else? In a C++ context, this statement is clearly
wrong.
>
Quote:
/Peter
>
i dont understand can't we delete the above newED array's as
>
accarray=anew->getDegreeDistribution;
>
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
Jorgen Grahn
Guest
 
Posts: n/a
#8: Aug 21 '08

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@informatimago.comwrote:
Quote:
"Alf P. Steinbach" <alfps@start.nowrites:
>
Quote:
>* Oliver Graeser:
Quote:
>>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.
Quote:
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_pointer.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.se R'lyeh wgah'nagl fhtagn!
Pascal J. Bourguignon
Guest
 
Posts: n/a
#9: Aug 22 '08

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


Jorgen Grahn <grahn+nntp@snipabacken.sewrites:
Quote:
On Thu, 21 Aug 2008 14:09:33 +0200, Pascal J. Bourguignon <pjb@informatimago.comwrote:
Quote:
>"Alf P. Steinbach" <alfps@start.nowrites:
>>
Quote:
>>* 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.

Quote:
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__
peter koch
Guest
 
Posts: n/a
#10: Aug 22 '08

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


On 22 Aug., 13:01, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
Quote:
Jorgen Grahn <grahn+n...@snipabacken.sewrites:
Quote:
On Thu, 21 Aug 2008 14:09:33 +0200, Pascal J. Bourguignon <p...@informatimago.comwrote:
Quote:
"Alf P. Steinbach" <al...@start.nowrites:
>
Quote:
Quote:
>* 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?
>
Quote:
Quote:
>Return small items by value, use standard library container types such
>as std::vector, and *don't* use raw arrays and raw pointers.
>
Quote:
Quote:
That doesn't help a lot, std::vectors are not deleted automatically either.
>
Quote:
They are,
>
Yes, sorry I was wrong, indeed temporary objects are cleanly destroyed.
>
Quote:
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
Closed Thread