   std::string problem
  Basically, I am writing a memory manager, and I've come across a problem with allocating std::string. The answer seems like it should be simple, but I can't figure out how it will work, since reinterpret_casting doesn't properly construct the string. And, as of right now my memory manager reinterpret_casts to the templated data type the user wants. In essence, here is the problem stated in code:#include <string>typedef unsigned char uint8;using namespace std;int main(){string* test = reinterpret_cast<string*>(new uint8[sizeof(string*)]);*test = "hi"; // this breaks because it is a bad pointer, not constructed correctly...return 0;}
  Whoa whoa whoa. What on earth are you doing here? reinterpret_cast is a cast, not a means of construction. You're doing this very, very wrong. You cannot construct arbitrary objects like that.What you probably need to be using here, ultimately, is placement new, but I would strongly recommend you reconsider your memory manager project because it seems like it might be something you're not quite equipped for yet. Can we see your implementation? This snippet you've put here is just not going to work, ever, so I'd like to see the context in which you expect it work.   ;  you can't allocate memory like that with C++ objects.  By doing so you are preventing the constructor from getting called for the string object.  It's crashing because the object isn't constructed and is likely completely fubar'd.Google "placement new"[EDIT: damn you and your fast fingers jpetrie!]-me   ;  1) You are only allocating enough memory for a POINTER to a string. The pointer is never set to point to anything.2) Even if you allocate the proper size of memory to hold a string, the constructor will never be called. You'd be trying to build a string out of a chunk of arbitrary data without any initialization.It looks like you're going about this memory manager in the wrong way. Is this exactly what the manager does or is it a simplification?   ;  Well, it is still in its early stages. This is a problem I completely skipped over when trying to come up with my own implementation. Anyway, here is the main parts of my class that are not finished...and I have left out other parts that I haven't got around to yet, such as when the client wants to Free the memory.Allocator::Allocator(const AllocatorInfo& info) : info_(info){AllocPage();}Allocator::~Allocator(){std::list<void*>::iterator end = page_list_.end();for(std::list<void*>::iterator iter = page_list_.begin(); iter != end; ++iter)SAFE_DELETE_ARR(*iter);}void Allocator::AllocPage(){size_left_ = info_.page_size;page_ptr_  =  new uint8[info_.page_size];page_list_.push_back(page_ptr_);}void* Allocator::SetupMemory(const size_t& size){*reinterpret_cast<size_t*>(page_ptr_) = size; // imbed the chunk size before the memory usedreturn reinterpret_cast<void*>(reinterpret_cast<size_t*>(page_ptr_) + 1);}template <typename T>T* Allocator::Alloc(){if(!free_list_.empty() ) // free list has pointers{// search through list looking for a usable slot// if a slot is usable, remove from free list and return the pointer}if(sizeof(T) + sizeof(void*) > size_left_) // not enough size left on page{// put remainder of page on free listfree_list_.push_back(SetupMemory(size_left_) );// allocate new pageAllocPage();}// set up memory// return pointerreturn reinterpret_cast<T*>(SetupMemory(sizeof(T) ) );}EDIT: Thanks for the placement new suggestions, I had never read the details on its use. From what I am reading it is the solution I wanted! And, as stated above, I knew it wasn't being constructed, but I didn't know the best way to actually construct the object...hence the question. I don't appreciate the bashing, though.   ;Quote:Original post by EnviiEDIT: Thanks for the placement new suggestions, I had never read the details on its use. From what I am reading it is the solution I wanted! And, as stated above, I knew it wasn't being constructed, but I didn't know the best way to actually construct the object...hence the question. I don't appreciate the bashing, though."Bashing"?  They're not overstating how seriously mucked up the original code snippet you had was, which appears to have been derived from trial and error (and error) -- which in C++, one of the most ill-defined languages (in terms of behavior) on the planet, "undefined behavior" lurking as a description in every other sentence of the standard, isn't just a very bad idea (tm), it's just about one of the worst things you can possibly do (tm).Maybe you really do have a legitimate need for a truly legitimate, profile-backed performance need for a custom memory manager that existing ones (such as the many in The Boost Library Collection) can't quite match up against, and this apparent lack of good C++ sense and knowledge just a localized fluke of nature of an otherwise overflowing supply of the both, but we've not seen evidence of either.And without that evidence, we'd prefer to err on the side of caution, and point out the fact that, on the surface, this rewriting of standard (and thus heavily battle-tested in the real world (tm)) library & language components (memory management falls under the domain of both), without somuch as an apparent consideration for other battle-tested alternatives, probably isn't a good idea.  Hopefully you can forgive us for this attempted favor.Feel free to provide evidence that you've done this and I'm sure we'll all gladly concede the point if that evidence seems to be valid.  I would have put this simple context in the very initial post -- it's an effective means to cut through this speculation we're doing in the absence of it, and also allows your rationale to be scrutinized for better alternatives (or just plain mistakes!), which happens even to the best of us.That said, I'd point you towards the general forums rather than the For Beginners forum, because you'd seemingly rather clearly not be a beginner to have these valid reasons for writing your own memory manager.  Even professional usually don't have these valid reasons -- hence the reason for standard memory management system in the first place.   ;  First off, my code example was breaking my problem down into simplest terms. That being I didn't know how to construct an object using the raw memory I had allocated. Sure the code was doing a big no no, but it easily expressed the problem I was having. And, I was well aware of the problem...I just didn't know the answer.I don't really consider myself a beginner, but this question seemed more on the beginner side; shame on me for not knowing about placement new. More people browse the For Beginner's forum. I haven't really looked into the alternatives much (mainly because I can code and it seems like a good learning experience). I do want to have control over exactly what my memory manager is checking for via on and off switches. So far it seems to be coming along nicely (in case you were wondering).   ;  But you haven't answered the question of WHY you want to write your own memory manager.  What problem have you encountered that suggests to you that you need to do so or that you would benefit from having written a custom memory manager?   ;Quote:First off, my code example was breaking my problem down into simplest terms. That being I didn't know how to construct an object using the raw memory I had allocated. Sure the code was doing a big no no, but it easily expressed the problem I was having. And, I was well aware of the problem...I just didn't know the answer.For SC++L containers:typedef std::basic_string<char, MyAllocator> MyString;typdef std::list<int, MyAllocator> MyList...where MyAllocator is implementation of Custom STL allocators.   ;Quote:Original post by cshoweBut you haven't answered the question of WHY you want to write your own memory manager.  What problem have you encountered that suggests to you that you need to do so or that you would benefit from having written a custom memory manager?Because, I would like to pool my memory for faster allocation time, along with check how many allocations/deallocations occurred during run-time, and to make sure that all memory is being freed when ran in debug mode. It seems to me that it would greatly benefit my team's school project this year if these checks are being made...And, as I mentioned before, it's a good learning experience. I for one don't learn the "ins and outs" of a project unless I attempt it myself.Thanks for the link Antheus, that covered a few things I was looking into! :)