Memory Management

A

Alexander Adam

Hi!

Is there an efficient way to "shorten" an array with C++ Operators
without the need to delete it, then reallocate it? I've thought about
using reallocate which might do the job but its C and besides that I
am using C++ operators new [] and delete [].
Besides, would it be faster to use malloc() and free() instead of new
[] and delete [] ? I can make sure that the types used for those
operations will be simple types (most of the time an unsigned char)
only so no constructors / destructors stuff etc.

Thanks + Regards
Alex
 
V

Victor Bazarov

Alexander said:
Is there an efficient way to "shorten" an array with C++ Operators
without the need to delete it, then reallocate it?

Yes. Consider it shortened. Next problem!...

No, really. If you just need it shortened, simply ignore the
elements that are beyond your "new end" of the array.
I've thought about
using reallocate which might do the job but its C and besides that I
am using C++ operators new [] and delete [].
Besides, would it be faster to use malloc() and free() instead of new
[] and delete [] ?

Usually, no. 'new' and 'delete' ensure that objects that need to be
constructed (and destroyed) are actually constructed (and destroyed)
properly.
I can make sure that the types used for those
operations will be simple types (most of the time an unsigned char)
only so no constructors / destructors stuff etc.

For those types it shouldn't matter, but if you're concerned, test it
and see.

V
 
M

Marcus Kwok

Alexander Adam said:
Is there an efficient way to "shorten" an array with C++ Operators
without the need to delete it, then reallocate it? I've thought about
using reallocate which might do the job but its C and besides that I
am using C++ operators new [] and delete [].

If it satisfies your requirements, you could try using std::vector
instead of your dynamic arrays. This way, you can use the
vector::resize() method, plus you don't have to worry about managing the
memory yourself. std::vector is compatible with functions that expect
native arrays, if that is one of your concerns.
Besides, would it be faster to use malloc() and free() instead of new
[] and delete [] ? I can make sure that the types used for those
operations will be simple types (most of the time an unsigned char)
only so no constructors / destructors stuff etc.

Also, don't just automatically assume that the performance will be less
when using vector instead of an array (or malloc()/free() versus
new[]/delete[]). The only way to see is to profile, and you may find
that there is no performance impact. Besides, many implementations of
new/delete internally use malloc/free anyway. Also, std::vector will
handle constructors/destructors appropriately.
 
V

Victor Bazarov

Marcus said:
Alexander Adam said:
Is there an efficient way to "shorten" an array with C++ Operators
without the need to delete it, then reallocate it? I've thought about
using reallocate which might do the job but its C and besides that I
am using C++ operators new [] and delete [].

If it satisfies your requirements, you could try using std::vector
instead of your dynamic arrays. This way, you can use the
vector::resize() method, plus you don't have to worry about managing
the memory yourself. std::vector is compatible with functions that
expect native arrays, if that is one of your concerns.
Besides, would it be faster to use malloc() and free() instead of new
[] and delete [] ? I can make sure that the types used for those
operations will be simple types (most of the time an unsigned char)
only so no constructors / destructors stuff etc.

Also, don't just automatically assume that the performance will be
less when using vector instead of an array (or malloc()/free() versus
new[]/delete[]). The only way to see is to profile, and you may find
that there is no performance impact. Besides, many implementations of
new/delete internally use malloc/free anyway. Also, std::vector will
handle constructors/destructors appropriately.

It is probably worth mentioning that standard containers do put some
requirements on the types of contained objects (Assignable and Copy-
Constructible) which may actually force somebody to use arrays instead
of 'std::vector'.

V
 
Z

Zachary Turner

Alexander Adam said:
Is there an efficient way to "shorten" an array with C++ Operators
without the need to delete it, then reallocate it? I've thought about
using reallocate which might do the job but its C and besides that I
am using C++ operators new [] and delete [].

If it satisfies your requirements, you could try using std::vector
instead of your dynamic arrays. This way, you can use the
vector::resize() method, plus you don't have to worry about managing the
memory yourself.

If actually reclaiming the memory is of cocern to the OP, then even
vector::resize() is not sufficient. I guess it depends on his intent.

- If all you want is to easily reclaim the additional memory without
having to dirty up your code with multiple function calls to achieve
this, use vector. It handles all the dirty work for you. Just be
aware that resize() won't reclaim the memory. One common way to
reclaim the memory immediately though is to do this

vector<char> vec1;
//fill vec1 with lots of data.
vec1.swap(std::vector<char>());

- If what you want is the speed of not having to delete/reallocate
memory, you can do what victor suggested above and keep track of some
variable that specifies your "true" size. (i.e. the size that you are
asserting the array is). Even if your array is allocated to be 700
items, as long as your variable says 5, you know that it's really only
holding 5 items.

- If you want both of the above at the same time, then your
requirements are that a) at any given time, there must never be more
memory allocated than necessary, and b) it must happen in such a way
that you does not involve the two step process of deleting the entire
array and reallocating it with a different size. There is no way to
achieve this.
 
M

Marcus Kwok

Victor Bazarov said:
Marcus said:
Alexander Adam said:
Is there an efficient way to "shorten" an array with C++ Operators
without the need to delete it, then reallocate it? I've thought about
using reallocate which might do the job but its C and besides that I
am using C++ operators new [] and delete [].

If it satisfies your requirements, you could try using std::vector
instead of your dynamic arrays. This way, you can use the
vector::resize() method, plus you don't have to worry about managing
the memory yourself. std::vector is compatible with functions that
expect native arrays, if that is one of your concerns.

It is probably worth mentioning that standard containers do put some
requirements on the types of contained objects (Assignable and Copy-
Constructible) which may actually force somebody to use arrays instead
of 'std::vector'.

Good point. I probably should have mentioned them explicitly, but I
guess it could be covered by the "If it satisfies your requirements..."
:)
 
M

Marcus Kwok

Zachary Turner said:
Alexander Adam said:
Is there an efficient way to "shorten" an array with C++ Operators
without the need to delete it, then reallocate it? I've thought about
using reallocate which might do the job but its C and besides that I
am using C++ operators new [] and delete [].

If it satisfies your requirements, you could try using std::vector
instead of your dynamic arrays. This way, you can use the
vector::resize() method, plus you don't have to worry about managing the
memory yourself.

If actually reclaiming the memory is of cocern to the OP, then even
vector::resize() is not sufficient. I guess it depends on his intent.

- If all you want is to easily reclaim the additional memory without
having to dirty up your code with multiple function calls to achieve
this, use vector. It handles all the dirty work for you. Just be
aware that resize() won't reclaim the memory. One common way to
reclaim the memory immediately though is to do this

vector<char> vec1;
//fill vec1 with lots of data.
vec1.swap(std::vector<char>());

Almost; you got it switched. It needs to be:

std::vector<char>().swap(vec1);

vector::swap() takes a non-const reference as its parameter, and so it
cannot bind to a temporary. However, you may call non-const methods on
a temporary object.

[snip rest]
 
M

Marcus Kwok

BobR said:
Can also be written:

std::vector<char>().swap( vec1 );

Use which ever is your style.

Except the first one doesn't work... see my other post about it.
 
B

BobR

Marcus Kwok wrote in message...
Except the first one doesn't work... see my other post about it.

Doh, that's what I get for not testing his code myself (I assumed he had!).

Thanks.

[ Next time, tell me you are going to post the answer so I don't duplicate
the effort! <G> ( ....what?!? Your crystal ball is not working?) ]
 
Z

Zachary Turner

Marcus Kwok wrote in message...

Doh, that's what I get for not testing his code myself (I assumed he had!).

:( That's what I get for posting about something I haven't actually
used in a while.
 
J

James Kanze

[...]
It is probably worth mentioning that standard containers do put some
requirements on the types of contained objects (Assignable and Copy-
Constructible) which may actually force somebody to use arrays instead
of 'std::vector'.

And that new[] puts other constraints on contained objects, such
as having a default constructor. (In practice, I can't conceive
of any use of new[] that didn't also require assignment. It's
the only way of changing the default constructed values, and in
general, arrays in which all elements are required to have
eternally the same value aren't very useful.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,453
Members
48,137
Latest member
IndiraMcCo

Latest Threads

Top