 |
 |
C++ "allocator" class usage
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Sep 2003
Status:
Offline
|
|
I'm trying to write my own container classes, and I need a resizeable C-style array. I am using malloc, realloc and free, but they never construct/destroy the arrays I am malloc/realloc/free-ing. I've gotten it to destroy (call the destructor) but never to construct (call the constructor). Here's my test code:
Code:
class temp {
public:
temp() {
printf("allocated\n");
}
~temp() {
printf("deallocated\n");
}
};
int main (int argc, const char * argv[]) {
//temp tem;
temp * temps = malloc(sizeof(temp) * 4);
allocator<temp> alloc ;
for(int i = 0 ; i < 4 ; i++)
{
alloc.construct(&temps[i],temps[i]);
}
for(int i = 0 ; i < 4 ; i++)
{
alloc.destroy(temps+i);
}
free(temps);
return 0;
}
It prints:
Code:
deallocated
deallocated
deallocated
deallocated
Thanks for any help.
Daniel
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Aug 2003
Status:
Offline
|
|
I am not quite sure why it is not calling the constructor, but I believe it has to do with what the alloc.construct(pointer p, const Temp& val) function actually does, which is to call
Code:
new(satic_cast<void*>(p)) T(val);
This is results in the compilier trying to convert from void* to temp*, which generates a warning. Also, when you are constructing the instances, you are initializing it with the value temps[i], which is probably empty or NULL. This might be causing the initialization to fail, but I am not sure.
I have a question for you though, why don't you use the new and delete operators directly, like this:
Code:
#include <iostream>
using namespace std;
class temp {
public:
temp() {
printf("allocated\n");
}
~temp() {
printf("deallocated\n");
}
};
int main (int argc, const char * argv[]) {
//temp tem;
temp * temps = new temp[4];
//do something with temps...
delete[] temps;
return 0;
}
You may have a reason for using alloc.construct and alloc.destroy, but I think new and delete will do what you want, unless you need really customized initialization.
Hope this helps
|
|
-- Devin Lane, Cocoa Programmer
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Sep 2003
Status:
Offline
|
|
Originally posted by Devin Lane:
You may have a reason for using alloc.construct and alloc.destroy, but I think new and delete will do what you want, unless you need really customized initialization.
Hope this helps
I would love to call new/delete, but you can't re-allocate! That's a pretty silly design point, so I have to use allocators.
Daniel
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Feb 2000
Location: Nashua NH, USA
Status:
Offline
|
|
why do you need to reallocate? just make a class
class MyContainer< class T> {
MyContainer( n ) {
call new
}
~MyContainer() {
call delete
}
resize( n ) {
new, then copy, then delete
}
};
BTW: slt gives you this for free and the C allocators while included are not garenteed to work with C++ objects. I'm surprised the destructor is even getting called.
(Last edited by BLAZE_MkIV; Sep 20, 2003 at 08:24 PM.
)
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status:
Offline
|
|
If all you need is a dynamically-sized array, just use std::vector. The STL can be very useful.
Code:
#include <vector>
std::vector<int> myVector;
myVector.push_back(5);
myVector.push_back(10);
std::cout << myVector[0] << std::endl;
(Off the top of my head & I haven't used STL for awhile, treat code with care)
|
Geekspiff - generating spiffdiddlee software since before you began paying attention.
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Feb 2000
Location: Nashua NH, USA
Status:
Offline
|
|
That looks fine to me but he said he was writing his own container.
BTW my personal C++ bible is The C++ Programming Language by Stroustrup, Not much of a leaning book but if you finish one of those 30days book this has everything else that isn't platform dependent.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Aug 2003
Status:
Offline
|
|
C++ in a nutshell is also a great reference if you want to know everything (yes, this book is loaded) about c++. It contains complete library references, as well as discussion on topics like class, templates, etc.
|
|
-- Devin Lane, Cocoa Programmer
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status:
Offline
|
|
I use C++, The Complete Reference, Vol. 3 by Schildt.
I'll have to try one of your recommendations, though. I'm not super-pleased with the one I've got. Stroupsoup'd probably be the way to go.
|
Geekspiff - generating spiffdiddlee software since before you began paying attention.
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|