Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > C++ "allocator" class usage

C++ "allocator" class usage
Thread Tools
Fresh-Faced Recruit
Join Date: Sep 2003
Status: Offline
Reply With Quote
Sep 19, 2003, 07:49 PM
 
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
Reply With Quote
Sep 20, 2003, 02:27 PM
 
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
Reply With Quote
Sep 20, 2003, 04:54 PM
 
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
Reply With Quote
Sep 20, 2003, 08:09 PM
 
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
Reply With Quote
Sep 21, 2003, 01:22 AM
 
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
Reply With Quote
Sep 21, 2003, 01:58 AM
 
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
Reply With Quote
Sep 21, 2003, 12:11 PM
 
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
Reply With Quote
Sep 21, 2003, 08:20 PM
 
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.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:11 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2