views:

141

answers:

4

I'm defining a vector as:

vector< int, MyAlloc< int> > *v = new vector< int, MyAllooc< int> > (4);

MyAlloc is allocating space for only 4 ints. Memory for _M_start, _M_finish, and _M_end_of_storage is being allocated on the heap before the memory for the 4 ints. But who is allocating this memory for _M_start, _M_finish, and _M_end_of_storage? I want to allocate this memory myself. What do I have to do?

+4  A: 

Do not use new for allocating the vector. Generally you should allocate vectors from stack:

vector< int, MyAlloc< int> > v(4);

If you really need to use your own allocator for that too, allocate the memory for the object and then call placement new on it to construct the vector.

It is also possible to overload global operator new/delete, but this is really messy and I would not recommend it.

Tronic
I have to allocate the vector on the heap. Even if I were to use placement new, the question stands. Where does the memory for _M_start, _M_finish etc come from; since even then I'll be putting only 4x4=16 (assuming int is 4 bytes) bytes in the placement pool.
Make up your mind! Do you want to allocate the memory yourself (question) or do you want to use heap memory (comment)? The two options are really mutually exclusive.
MSalters
The piece of code I've mentioned here is in a static C++ library which is used by C programs.I want to allocate the memory myself. It so happens that ultimately this memory is coming from the c-heap by a malloc call.
A: 

new T allocates sizeof(T) memory on the heap. In your case, T is std::vector< int, MyAlloc< int> > and includes members like _M_start.

If you want to allocate that memory yourself, then don't call new.

MSalters
Intresting thing is that sizeof(*v) gives 12 which is the space for _M_start, _M_finish, and _M_end_of_storage.I can see from the allocate function in MyAlloc that it is allocating only 16 bytes - just enough for the ints and excluding the _M_'s.If I want to do this on the heap, then I guess I have to use new?
The last sentence of that remark just does not make any sense at all. What does "do this" refer to?
MSalters
+1  A: 

Allocators allocate the data storage of the container, not the container data members, which are allocated as normal. This must be the case, or you would not be able to create the vector using new or on the stack. This is clearest for a node based container like a list - the list nodes are allocated by the allocator, but the other data members, including the pointers to the first and last nodes (if implemented that way) are allocated normally.

anon
What you are saying makes sense, but please see my response to the previous post. (MSalters' post.)
A: 

When you create the vector, it allocates room for the vector's member variables (the _M ones) wherever you place the vector. If you use new, it allocates space for these variables on the heap. With local variables, the compiler makes space for them in the current stack frame. If you make the vector a member of a class, the compiler makes space for them in the containing class.

The vector then uses its allocator to allocate room for the data you wish to store in the vector, using whatever mechanism the allocator is defined to use.

jalf
This is consistent with what I've seen. Am I correct in understanding that the space for (_M's) is allocated by "new" outside of MyAlloc?The reason I ask this question is that inspite of any number of print statements, I can not figure out where MyAlloc allocates space for the _M's.And if the space for _M's is allocated by "new" and not MyAlloc, then how do I take control of this space allocation?
Yes, `MyAlloc` is simply a helper object that the vector can call when it likes to. `new` doesn't know about allocators, it just allocates space for a vector.You "take control" of the allocation of the member variables exactly like I said in my answer: By allocating them where you want. If you use `new` to allocate the vector, the members of the vector are allocated wherever `new` decides to (on the heap). If you make the vector a class member, all its members are allocated as part of that class. If you use placement new, they're allocated wherever you want.
jalf