BTW: Why don't you just use a vector of floats and let STL manage all this heap allocation for you?
http://www.cplusplus.c
Main Topics
Browse All TopicsHi
I allocate
float*A = new float[300];
//... fill up A with stuff and do processing
//now I want to "make A smaller": keep elements A[0 .. 99] but free up memory for elements 100 to 299.
//
// I would prefer not to have to allocate a new block of 100 floats, copy over the first 100 elements of A, and then delete the entirety of A as I have been doing in the past.
Thanks..
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
BTW: Why don't you just use a vector of floats and let STL manage all this heap allocation for you?
http://www.cplusplus.c
>> How would I do it using malloc/realloc/free?
Someth
http://www.cpluspl
http:
>
You might be able to do something using placement new, but it really is a pain to work with -- I wouldn't bother.
http://www.devx.com
http:
1) If I use realloc to *decrease* the size of the block, am I guaranteed that the pointer to the beginning of the blcok will not change? Can I demand this? (I am trying to avoid any time overhead associated with copying the part of the array that I want to preserve)
2)Suppose I use placement new to construct an object in a block that I have already assigned using "ordinary" new. Do I have to call a "placement" delete? What if I call "ordinary" delete before I have finished using the placemented object? That woudl be bad presumably
>> If I use realloc to *decrease* the size of the block, am I guaranteed that the pointer to the beginning of the blcok will not change?
No, but it is unlikely to do so with a decrease in size (but you cannot guarentee this)
>> Can I demand this?
No, how the memory is managed is ultimately down to the implementation (you compiler and OS)
When allocating a buffer for placement new you are getting into very murky waters if you try to use normal new to allocate the memory. It is far safer to stick to using malloc/realloc/free. Actually, using placement new is really quite tricky (there are many caveats) and unless you are confident of its usage I'd avoid it.
http://www.research.att
BT
http://www.cplusplu
I'd recommend you prefer to use one of the STL containers, if not use malloc/realloc and free.
Business Accounts
Answer for Membership
by: evilrixPosted on 2008-07-10 at 03:29:08ID: 21971901
You cannot 'reallocate' using new/delete like you can with malloc/realloc/free.