tags:

views:

174

answers:

3

This is a bit unclear to me... So, i if i have a function:

char *test(int ran){
    char *ret = new char[ran];
    // process...
    return ret;
}

and then call it multiple times:

for(int i = 0; i < 100000000; i++){
   char *str = test(rand()%10000000+10000000);
   // process...

   // delete[] str; // do i have to delete it here?
}

So the question is, do i have to use delete[] for each new[] call?

+3  A: 

Yes you do, otherwise you'll have a memory leak.

It's not the greatest idea to allocate in one function and free in another, though. Why not allocate in the for loop and pass the pointer to test -- this keeps the new and delete together in the code.

zildjohn01
It's perfectly ok to do it, and quite common.
anon
crap. this'll take a while to fix :D
Newbie
@Newbie: I hope you are not actually such big memory allocations in your real code. It will surely throw a `std::bad_alloc` exception indicating memory allocation failure otherwise.
Naveen
Nope, that was just example, is there some certain limit on new[] memory allocation size...? in my example it made max 20megs...
Newbie
@Neil many things are perfectly OK, but still aren't a good idea. :)
Johannes Schaub - litb
@Johannes Why is it not a good idea? I've never seen it mentioned in any of the c++ style books, and I can't see anything wrong with it myself. Of course, you would normally want to allocate the return value to a smart pointer rather than explicitly deleting, but the concept (which I yhink is OK) remains the same.
anon
@Neil i think the way zildjohn phrased it was that it's not the greatest idea, but that it isn't necessarily bad or not necessarily not a good idea. If you allocate in one function, the calling code is dependent on the allocation function used, and has to use the corresponding deallocation function. If you pass the pointer in, there is no such dependency. So sure it's OK to have such dependency, but there may be other ways to solve such things. I think a smart pointer with a deleter set-up by the newing function is superior to having such dependency, since the calling code is decoupled too
Johannes Schaub - litb
@Johannes He actually rephrased his answer after my comment. I don't think passing the pointer in (as a reference?) and allocating to it in the function is at all superior, if that is what you meant.
anon
@Neil ah good point on he editing it. Didn't know :) I meant to do it like the following is superior `shared_ptr<int> f() { return shared_ptr<int>(my_alloc(32), }` . With that, the calling code doesn't need to know that for allocation a `my_alloc` instead of `malloc` or `new[]` or whatever was used.
Johannes Schaub - litb
+5  A: 

You don't have to. But if you don't delete memory you reserved with 'new' you will start running out of memory eventually (memory leak).

WhyGeeEx
Ah, yeah, that makes sense since my program seems to use more and more memory each time :D
Newbie
+2  A: 

The answer was already given, but as you tagged the question as C++, and not as C, this is how you probably want to do it in C++ (of course, there might be other reasons not to, but there is little chance).

vector<char> (int ran){
    vector<char> ret(char);
    // process...
    return ret;
}

And to call it:

for(int i = 0; i < 100000000; i++){
   vector<char> str = test(rand()%10000000+10000000);
   // process...
}

No new, thus no delete thus no memory leak.

Actually, you also probably want to use std::string instead of char* (I used vector to give a more general example).

Don't worry of data that will be copied. The compiler will optimize it out. Edit : ok, it might not optimize it out :) however there are big chances it will. And as long there is no performance issues, go for the simplest version.

Tristram Gräbener
"The compiler will optimize it out." <-- This way lies madness...
zildjohn01
Well, it's not much more a lie than saying "it will copy the vector and result in a performance loss". But I edited it still :)
Tristram Gräbener