Prototype for function that allocates memory on the heap (C/C++) -
I am quite new to C ++, so this is probably of some initial question. It relates to "proper" style to do something which I suspect is common.
I am writing a function that allocates memory of the pile for use by the collar in adhering to its duties. I am curious about this function should look like a good prototype. I've just found it:
int f (char ** buffer);
To use it, I will write:
four * data; Int data_length = f (more data); // ... Delete [] data;
However, the fact is that I give the indicator an indicator, let me tell that I am probably doing it wrong.
me?
In C, this is more or less legal.
Functions in C ++ should not normally do this. You are guaranteed that memory should not be leaked.
And now you can say "how it will leak, I destroy [/]> just there!", But what if an exception
// ...
Is Throwed On Lines?
Based on what is done to actually work, you have several options to consider, to change the array with a clear vector:
Std :: vector & lt; Char & gt; F (); Std :: vector & lt; Four & gt; Data = F (); Int data_length = data.size (); // ... // delete [] data;
And now we do not need to explicitly delete, because the vector has been allocated on the stack, and its destroyer is called when it is not scope.
In response to I comments, it should be mentioned that one means the copy of the vector from above, which can be potentially expensive, if most compiler, f The
function is not very complicated, so customize the copy so that it will be fine. (And if the function is not often called, then ohrehead anyway ) will not make any difference, but if it does not happen, you can instead replace an empty array with reference to f < / Code> function, and archive its data instead of returning a new vector to
f
.
If the display of returning a copy is unacceptable, then the second option would be to completely reduce the choice of the container, and use iterators instead:
// Definition template & lt; Typename iter & gt; Zero F (itter out); // f std :: vector & lt; Char & gt; Use vec; F (std :: back_inserter (vec));
Now the normal Iterator operation can be used ( * out
to reference or to write the current element, and ++ out
take the itator forward in the next element) - More importantly, all the standard algorithms will now work. For example, you can use std :: copy
to copy the data to iterator. This is usually the approach chosen by the standard library (i.e. this is a good idea;)) when a function has to return the order of the data.
Another option would be that you have taken your object responsibility for the allocation / deallocation:
struct f {// has been simplified for example. In the real world, it should be given a proper copy constructor + assignment operator, or they should be insufficient to avoid copying the object f () {// do f function was originally done here for size =? ?? Data = new four [size]; } ~ F () {Delete} data; } Integer size; Four * data; }; F data; Int data_length = data.size; // ... // delete [] data;
And then we do not need to explicitly delete because the allocation is managed by the object on the stack. The latter is obviously more work, and more space for errors, so if the standard vector class (or other standard library components) employs, then they like it. This example is only when you were optimized in your situation Ho.
The general rule of thumb in C ++ is that "if you delete
or ]
outside of an RAII object, you are doing it wrong If you are writing a new
or new [] out of an RAII object, then you are doing it wrong, as long as the result is immediately a smart pointer. "
Comments
Post a Comment