glebspy
asked on
"delete" ing only the end of a memory block allocated with "new"
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..
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..
You cannot 'reallocate' using new/delete like you can with malloc/realloc/free.
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/
http://www.cplusplus.com/reference/stl/vector/
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.
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);
}
ASKER
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?
>> 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
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));
}
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok thanks.