c++ question

Started by
4 comments, last by nobodynews 16 years, 9 months ago
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?
Advertisement
Yes, you must always delete[] what you new[] when you are done with it.

Using the RAII idiom (via smart pointers) makes this easy to manage.
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
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.
-------------Please rate this post if it was useful.
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 = // 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.
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.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement