My_allocator
class design my own vector
.
|
|
T* allocate(int n)
and void construct(T* p, const T& v)
mean but I don't know what void destroy(T* p)
and void deallocate(T* p, int n)
should do exactly.
|
|
free(&p[i]);
(line 30).Using malloc and free in C++ |
malloc
and free
is C, not c++. new
and delete
is c++ - http://www.cplusplus.com/doc/tutorial/dynamic/free(p);
construct()
needs to call the copy constructor using placement new:http://stackoverflow.com/questions/222557/what-uses-are-there-for-placement-new
destroy()
needs to call the destructor on the items. This can be called directory:p->~T(); // explicitly destroy p.
My_allocator
, which is as follows:~Vector<T, A>() { delete[] elem; }
to the class Vector : public vector<T>
.
|
|
~Vector<T, A>() { delete[] elem; }
new
-> delete
new[]
-> delete[]
malloc()
-> free()
class Vector : public vector<T>
> I also added ~Vector<T, A>() { delete[] elem; } that's the allocator job. |
~My_allocator<T>()
inside the class My_allocator
please?Also new -> delete new[] -> delete[] malloc() -> free() don't mix them |
~My_allocator<T>() { free(this); }
, yes?> class Vector : public vector<T> That's a terrible idea. |
The operations of std::vector would not work with your member variables and will likely break invariances. |
std::vector
are accessible for the members and objects of the class Vector
, because they have been inherited.
free(this);
delete[] elem;
. `elem' is not pointing to memory allocated with new[]
, but with malloc()
.
|
|
construct
10 element (because we initialized elem with new T[n]
. We want to reserve space for 10 elements but we don't want to construct any in that space (yet).sz*sizeof(T)
bytes in that space contain sz
valid, constructed T's.Class Vector : public vector<T> {
std::vector
, not to extend it. As ne555 pointed out, if you access the members of std::vector, you won't get what you expect because those members will be using the internal data (vector space, size, capacity etc) of std::vector, not your data.And by this, I think you meant that I should use the code ~My_allocator<T>() { free(this); } , yes? |
My_allocator
doesn't have any data members, there's no need for the destructor to do anything. ne555 just means to allocate and deallocate memory with matching functions. In this case, I think you have it right. You are allocating/deallocating memory with malloc/free. You are using new
to construct objects, not to allocate memory.void Vector<T, A>::my_reserve(int newalloc)
malloc()
and free()
. And as you already pointed out correctly, the author meant using "replacement new" and "explicit destroying" to be used in solving that exercise. So I implemented an allocator named My_allocator and used malloc()
and free()
there. Just like my above (My_allocator class) code. So I have to use My_allocator and malloc() and free() because it is an exercise not an arbitrarily program. std::vector
may not have a useful outcome. Because of that, I haven't used them. I added a My_
prefix to the used functions (like My_size()
and etc) to use my own functions in Vector<T, A>
.Vector
class that inherits from std::vector
but doesn't use any of the std::vector
's public members). But it was up to the author what exercise he offer. Look at the 19.4 please. My_allocator
class in my first post of this thread with the one I offered later (after your pointing to the replacement new and explicit destruction) the code is fine and acceptable. Do you disagree? vector<T, My_allocator<T> >
and test it.
|
|
You're still deriving Vector from std::vector. You should not do that. When removing this, change the my_XYZ method names to XYZ. E.g., change my_reserve() to reserve() |
|
|
It says I should derive from std::vector, |
8. Implement an allocator (§19.3.6) using the basic allocation functions malloc() and free() ( §B.10.4 ). Get vector as defined by the end of §19.4 to work for a few simple test cases. |
In this and the following two chapters, we show how to build vector from the basic language facilities available to every programmer. Doing so allows us to illustrate useful concepts and programming techniques, and to see how they are expressed using C++ language features. The language facilities and programming techniques we encounter in the vector implementation are generally useful and very widely used. |
¿does this work for you? |
sz
and space
in the std::vector
, in this way. (because we are using std::vector
's operator[]
)