We help IT Professionals succeed at work.

"delete" ing only the end of a memory block allocated with "new"

glebspy
glebspy asked
on
322 Views
Last Modified: 2010-04-16
Hi

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..  
Comment
Watch Question

evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
You cannot 'reallocate' using new/delete like you can with malloc/realloc/free.
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
BTW: Why don't you just use a vector of floats and let STL manage all this heap allocation for you?
http://www.cplusplus.com/reference/stl/vector/
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
Example of using vector...

The resize doesn't actually unallocate any memory but it does set the internal state of the vector so it will only have 200 rather than 300 items. If you were to try and resize it to 400 it would add an extra 100 elements for you.
#include <vector>
 
typedef std::vector<int> vec_t;
 
int main()
{
	vec_t vec(300);
 
	// Use vector with 300 elements and then resize it to 200
 
	vec.resize(200);
}

Open in new window

Author

Commented:
How would I do it using malloc/realloc/free? Is there any workaround or hack that I could use to initially allocate with new, but then reallocate with malloc/realloc/free?
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
>> How would I do it using malloc/realloc/free?
Something like below...

http://www.cplusplus.com/reference/clibrary/cstdlib/malloc.html
http://www.cplusplus.com/reference/clibrary/cstdlib/realloc.html

>> Is there any workaround or hack that I could use to initially allocate with new, but then reallocate with malloc/realloc/free?
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/tips/Tip/12582
http://www.daniweb.com/forums/thread75705.html
#include <malloc.h>
 
int main()
{
	// Don't forget to test return of each of these for NULL
	float *p = (float *) malloc(300*sizeof(float));
	p = (float *) realloc(p, 200*sizeof(float));
}

Open in new window

Author

Commented:
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
Senior Software Engineer (Avast)
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
ok  thanks.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.