Sign in to follow this  
PaulHolverda

c++ question

Recommended Posts

PaulHolverda    128
say i have these 2 function int* getValues(int numValues) { int* ret = new int[numValues]; return ret; } void doSth() { int* values = getValues(600); } should i call delete[] on values or not?

Share this post


Link to post
Share on other sites
Durfy    109
From what I hear if you dont delete the things you new it causes a memory leak! I dont know why its bad but it is!
-durfy

Share this post


Link to post
Share on other sites
Hnefi    386
Memory leaks are bad because every time you call a leaking function, it will consume more memory. Eventually there will be no more memory available and the program will crash.

IIRC, Windows 95a had a memory leak in its wait loop, causing any W95a system to crash after a few days even if it was simply idle.

Share this post


Link to post
Share on other sites
Antheus    2409
As a general rule, which will save you *lots* of headaches, always de-allocate in the same place where you allocate.


void calculate( int *data, int n )
{
for (int i = 0; i < n; i++) {
data[i] = // do something with data
}
}

...
void doSth()
{
int *x = new int[600];
calculate( x, 600 );
delete[] x;
}



And before anyone comments, std::vector would eliminate all these problems:

typedef std::vector<int> IntVector;

IntVector getValues( int numValues )
{
IntVector ret;
ret.resize( numValues );
return ret;
}

void doSth()
{
IntVector values = getValues( 600 );
}



While this example is better, it's not efficient. If you follow the first example, this would be ideal:

void calculate( IntVector &v )
{
for (IntVector::iterator i = v.begin(); i < v.end(); ++i) {
(*i) = // do something with data
}
}

void doSth()
{
IntVector x;
x.resize( 600 );
calculate( x );
// no need to delete, handled by compiler and std::vector
}



If this is C, not C++, then remember the first advice about allocations.

Share this post


Link to post
Share on other sites
nobodynews    3126
int* getValues(int numValues)
{
int* ret = new int[numValues];
return ret;
}
void doSth()
{
int* values = getValues(600);
}

The problem with allocating memory this way is that it can confuse who owns the memory. The idea is that a function of class that allocates memory 'owns that memory' and should take care of it. So if you allocate memory in a function you should try and delete it in the same function and if a class allocates memory it should take care of it when it is destroyed in the destructor. You can technically avoid doing this, but then you have to make clear in the documentation that the person calling your function has to take care of the resources. So you have you getValues already and you can make a corresponding freeValues function like so:

void freeValues(int * values)
{
delete [] values;
}

For instance, SDL does this when acquiring SDL_Surfaces. You call a function with parameters(location of file, size of file, color masks, etc) that returns the location in memory of the surface. When you are done with it you call SDL_FreeSurface as specified in the documentation.

Actually, I'm sure the real reason that's needed is because SDL is compiled as a DLL and you can't safely delete memory allocated from a dll file, if I remember correctly and this thread is correct.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this