By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,783 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,783 IT Pros & Developers. It's quick & easy.

General Allocator Regarding type definitions and void * specializedproblem

P: n/a
Hello all C++ expert programmer,

i have wrote partial general allocator for my container.

After reading standard C++ library and code guru article, i have
several questions.

1. Why allocator write forward declaration then allocation for void*
rather than directly wrote allocator first ?

Expand|Select|Wrap|Line Numbers
  1. namespace std {
  2. template <class Tclass allocator;
  3.  
  4. // specialize for void:
  5. template <class allocator<void{
  6. public:
  7. typedef void*       pointer;
  8. typedef const void* const_pointer;
  9. // reference to void members are impossible.
  10. typedef void value_type;
  11. template <class Ustruct rebind { typedef allocator<U>
  12. other; };
  13. };
  14.  
  15. template <class Tclass allocator {
  16. public:
  17. typedef size_t    size_type;
  18. typedef ptrdiff_t difference_type;
  19. typedef T*        pointer;
  20. typedef const T*  const_pointer;
  21. typedef T&        reference;
  22. typedef const T&  const_reference;
  23. typedef T         value_type;
  24. template <class Ustruct rebind { typedef allocator<U>
  25. other; };
  26.  
  27. allocator() throw();
  28. allocator(const allocator&) throw();
  29. template <class Uallocator(const allocator<U>&) throw();
  30. ~allocator() throw();
  31.  
  32. pointer address(reference x) const;
  33. const_pointer address(const_reference x) const;
  34.  
  35. pointer allocate(size_type,
  36. allocator<void>::const_pointer hint = 0);
  37. void deallocate(pointer p, size_type n);
  38. size_type max_size() const throw();
  39.  
  40. void construct(pointer p, const T& val);
  41. void destroy(pointer p);
  42. };
  43. }
  44.  
  45.  
  46.  

2. What is the use/purpose of type definition ?

1. A couple of type definitions. These ensure that the allocators'
client (for instance, 'std::vector') is able to use some relevant
types by known names. For example, consider that you write an
allocator, that is able to allocate memory in a far area, that cannot
be reached by normal pointers (let your imagination wander). Now, the
'allocator' will use some pointer-like construct. The allocators'
client has, of course, no idea of such a thing. When a client needs to
pass such a pointer it will use the

typedef T* pointer;

and if it needs to suptract such pointers, the result will have the
type 'difference_type', whatever that internally means for the
allocator.

What i understand from here is because different container needs
different pointer construct memory, therefore, there are different
member data in list and vector. So, allocator need to match back its
member data with vector for example.

typedef T* pointer; ----------- Allocator

Vector
typedef A allocator_type; typedef typename A::pointer pointer;
Although, i not able to understand what this does

vector
typedef A allocator_type; typedef typename A::pointer pointer;
3. How allocator know when it need rebind for its allocator
client(vector, list, map, set) ?

This is the magic required for std::list to work properly,
since given std::list<int( allocator<int>() ), std::list actually
needs to allocate memory for Node<int>, and not int.
Thus, they need to rebind to allocator<int>()::rebind<Node<int
:ther instead.

Expand|Select|Wrap|Line Numbers
  1. template <class U>
  2. struct rebind
  3. {
  4. typedef allocator<Uother;
  5. };
  6.  
  7. list<int>;
  8.  
  9. interprete by compiler as list<int, allocator<int;
  10.  
  11. This code template <class T>
  12. class allocator; is replace with allocator<int;
  13.  
  14. In other words, T(represent generic types) = int;
  15.  
  16. Then allocator class receive integer as argument
  17.  
  18. How compiler interpreter
  19. template <class U>
  20. struct rebind
  21. {
  22. typedef allocato<Uother;
  23. }
  24.  
  25. How list is pass U ?
  26.  
Thanks for your help.

Please help me.
I really appreciated any help.
Aug 14 '08 #1
Share this Question
Share on Google+
16 Replies


P: n/a
This is current work.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. /*
  4. Two steps define custom allocators
  5.  
  6. 1. Design a memory management mechanism/model
  7. 2. Create standard-like allocators
  8.  
  9.  
  10. */
  11.  
  12.  
  13.  
  14. // ================================================
  15.  
  16. #ifndef _Custom_Allocator_
  17. #define _Custom_Allocator_
  18.  
  19. #include <memory>
  20.  
  21. using std::allocator;
  22.  
  23.  
  24. // ================================================
  25.  
  26. template <class T>
  27. class MyAlloc
  28. {
  29.  
  30. // Two Constructos which did nothing
  31. public:
  32.  
  33.  
  34. // Type Definitions
  35.  
  36. // Pointer to element type used in memory model
  37. typedef T* pointer;
  38. // Const Pointer to element type used in memory model
  39. typedef const T* const_pointer;
  40.  
  41. // Reference  to element type used in memory model
  42. typedef T& reference;
  43. typedef const T& const_reference;
  44.  
  45. // Type of the element that is being used in the memory model
  46. typedef T value_type;
  47.  
  48. // Rpresent largest object in allocator memory model
  49. typedef size_t size_type; // Unsigned
  50.  
  51. // Represent two pointer in two allocator model
  52. typedef ptrdiff_t difference_type; // Signed
  53.  
  54.  
  55. // =================================================
  56.  
  57.  
  58. // Member Function
  59. /*
  60. No throw is allowed for constructor
  61. and destructor
  62.  
  63. C && D is trivial(Not important)
  64.  
  65.  
  66. */
  67. MyAlloc();
  68. /*
  69. Copy C is need because exception
  70. specification stated that constructor
  71. is not allow to throw.
  72.  
  73. Does require operator= because
  74. if (this != rhs) then code will not
  75. be executed and Two MyAlloc object must
  76. same which form by C++ standard allocator
  77.  
  78. */
  79. MyAlloc(const MyAlloc<T&);
  80. ~MyAlloc();
  81.  
  82. /*
  83. Require rebind because list(nodes), vector
  84. (contigious)
  85.  
  86. Rebind is a structure that enables
  87. an allocator for objects of one type
  88. interpret as to allocate storage for
  89. objects of another type.
  90.  
  91. To allocate objects of
  92. different types than its
  93. template parameter
  94.  
  95. The rebind member allows a container
  96. to construct an allocator for some
  97. arbitrary type out of the allocator type
  98. provided as a template parameter.
  99.  
  100. This is the magic required
  101. for std::list to work properly,
  102. since given std::list<int>
  103. ( allocator<int>() ),
  104. std::list actually needs to allocate memory
  105. for Node<int>, and not int.
  106. Thus, they need to rebind to
  107.  
  108.  
  109. allocator<int>()::rebind<Node<int
  110. ::other instead.
  111.  
  112.  
  113. For instance, the list container gets an
  114. allocator<Tby default, but a list may
  115. well need to allocate list_nodes as well
  116. as T's. The container can construct an
  117. allocator for list_nodes out of the
  118. allocator for T's
  119. (the template parameter,
  120. Allocator, in this case) as follows:
  121.  
  122. Allocator::rebind<list_node>
  123. ::other list_node_allocator;
  124.  
  125. */
  126.  
  127. /*
  128. Explicit call by compiler is
  129. allocator<T>::rebind<U>::other;
  130.  
  131. Here allocator client(vector, list)
  132. request allocator type from allocator
  133.  
  134. Therefore, allocator using rebind to
  135. preseve the old state type and duplicate
  136. a same/new state type to pass to
  137. allocator client.
  138.  
  139. Then, continue to class to rework a new
  140. type which is
  141. listAllocator < node<int and
  142. not allocator<int>.
  143.  
  144.  
  145. */
  146. template <class U>
  147. struct rebind
  148. {
  149. typedef allocator<Uother;
  150. }
  151.  
  152.  
  153.  
  154. // Return address of given object
  155. pointer address(reference x) const;
  156. const_pointer address(const_reference x) const;
  157.  
  158. //  Returns the largest value which can be passed to the 'allocate()'
  159. function.
  160. size_type MaxMemory();
  161.  
  162. /*
  163. Returns storage for n elements of
  164. the element type being used
  165. in the memory model.
  166.  
  167. Elements will not be c
  168. onstructed/initialized.
  169.  
  170. */
  171. pointer allocate(size_type);
  172. /*
  173. Deallocate element type used in
  174. memory model begin at position p
  175.  
  176. Storage must be allocate by same allocator
  177.  
  178. Size must same in allocate()
  179. p must not be 0.
  180. Elements must have been destroyed before.
  181. */
  182. void deallocate(pointer, size_type);
  183.  
  184. /*
  185. Allocate must call before construct
  186. This is a call to placement new
  187. value is U
  188. new((void*)p) T(u);
  189. */
  190. void construct(pointer, const_reference);
  191. /*
  192. Destrory call ahead of (prior to) deallocate
  193. new((void*)p) T(u);
  194. */
  195. void destrory(pointer);
  196. };
  197.  
  198. /*
  199. No refernce type to void* -That's why need
  200. specialization for void.
  201. */
  202.  
  203. // ================================================
  204.  
  205. template<class T1, class T2>
  206. bool operator==(MyAlloc<T1>, MyAlloc<T2>) const
  207. {
  208. return MyAlloc<T1== MyAlloc<T2>;
  209. }
  210.  
  211. template<class T1, class T2>
  212. bool operator!=(MyAlloc<T1>, MyAlloc<T2>) const
  213. {
  214. return MyAlloc<T1!= MyAlloc<T2>;
  215. }
  216.  
  217. // ================================================
  218.  
  219.  
  220.  
  221. #endif
  222.  
  223. /*
  224.  
  225. allocate and deallocate function are
  226. low level memory management which
  227. doesn't participate in
  228. object construction and destruction.
  229.  
  230. The purpose of the allocator is to allocate
  231. raw memory without construction of objects,
  232. as well as simply deallocate memory
  233. without the need to destroy them.
  234.  
  235. Usage of ::operator new and ::operator delete
  236. is preferred over keywords new and delete.
  237.  
  238. A* a = new A;
  239. delete a;
  240.  
  241. Intepreted by compiler as below:
  242.  
  243. // assuming new throws std::bad_alloc upon failure
  244.  
  245. Allocate then construct
  246. A* a = ::operator new(sizeof(A));
  247. a->A::A();
  248.  
  249. if ( a != 0 )
  250. {  // a check is necessary for delete
  251.  
  252. a->~A();
  253. ::operator delete(a);
  254.  
  255. Destroyed(Destruct) first
  256. before deallocate
  257. }
  258.  
  259.  
  260. Every C++ standard like allocator must provide
  261. these global operator== and operator!=
  262.  
  263. Memory Model are shared model, grabage collection,
  264. segregrated model.
  265.  
  266. Why write custom allocators ?
  267. 1. To trace the memory operations of
  268. your application to a file
  269. 2. Speed
  270.  
  271.  
  272.  
  273. Sample Override New and delete Code
  274.  
  275. void* operator new(size_t,void* anAddress)
  276. {
  277. return anAddress;
  278. }
  279. void* operator new(size_t size)
  280. {
  281. return Standard::Allocate(size);
  282. }
  283. void  operator delete(void *anAddress)
  284. {
  285. if (anAddress)
  286. Standard::Free((Standard_Address&)anAddress);
  287. }
  288.  
  289. The first new operator overload is for the
  290. new with placement syntax, instead of
  291. creating instances on the free store it
  292. will use the address you provided.
  293.  
  294. This is useful for using preallocated memory (e.g. a buffer)
  295. to store your objects and still have the
  296. construtors and destructors called for these
  297. objects.
  298.  
  299. Apparently this first overload is just the
  300. default one that would be generated by
  301. the compiler anyway.
  302.  
  303.  
  304. The second new and the delete operator overload are apparently
  305. defined because the coder wanted to use a custom allocator.
  306. If the first new overload seems useless but is still present it may
  307. be that the compiler is requiring it if you overload the new(size_t)
  308. one ( just a guess), try removing the new(size_t, void*) definition
  309. and see if the code still compiles and link.
  310.  
  311.  
  312. No reference to object which allocated on the stack
  313. This is make sense since stack unwinding
  314. will get clean up and you will use danling
  315. reference
  316.  
  317. Never pass auto_ptrs by value
  318. if a function can throw
  319.  
  320.  
  321. BTW, returning auto_ptrs by value is a
  322. good idea for factory and
  323. clone like functions.
  324. */
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332. // Sketch version of list
  333.  
  334. /*
  335. template <typename T, typename A>
  336. class node
  337. {
  338. typedfed node list_nodes;
  339.  
  340. typename A::rebind<list_nodes>::others listNodeAllocator;
  341. // Actually declare listNodeAllocator < list_nodes<T;
  342.  
  343. };
  344.  
  345. */
  346.  
Thanks for your correction.
Aug 14 '08 #2

P: n/a
You can not change or add code to std namespace because it is
undefined behaviour.
Aug 14 '08 #3

P: n/a
I didn't add to code to namespace standard.
I create my custom allocator with my own namespace.

Please behave in C++ usenet community.

Thanks.
Aug 16 '08 #4

P: n/a
Please help me.
Aug 19 '08 #5

P: n/a
This is all my code.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #ifndef _Custom_Allocator_
  3. #define _Custom_Allocator_
  4.  
  5. #include <memory>
  6.  
  7. using std::allocator;
  8.  
  9.  
  10. // ================================================
  11.  
  12. template <class T>
  13. class MyAllocator
  14. {
  15. public:
  16.  
  17. // Type Definitions
  18.  
  19. typedef T value_type;
  20.  
  21. typedef T* pointer;
  22. typedef const T* const_pointer;
  23.  
  24. typedef T& reference;
  25. typedef const T& const_reference;
  26.  
  27. typedef size_t size_type; // Unsigned
  28.  
  29. // Signed
  30. typedef ptrdiff_t difference_type;
  31.  
  32.  
  33. // Member Function
  34.  
  35. MyAllocator(){}
  36.  
  37. // MyAllocator(const MyAllocator<T&rhs);
  38. ~MyAllocator(){}
  39.  
  40. // MyAllocator<T>::rebind<U>::other;
  41. template <class U>
  42. struct rebind
  43. {
  44. typedef MyAllocator<Uother;
  45. }
  46.  
  47. pointer address(reference memory) const
  48. {
  49. return *memory;
  50. }
  51. const_pointer address(const_reference memory) const
  52. {
  53. return *memory;
  54. }
  55.  
  56. size_type MaxMemory()
  57. {
  58. return ;
  59. }
  60.  
  61. pointer allocate(size_type allocateSiZe)
  62. {
  63. return ::operator new (allocateSize);
  64. }
  65. void deallocate(pointer aPtr, size_type allocateSize)
  66. {
  67. if (aPtr != 0)
  68. {
  69. destrory(aPtr);
  70. ::operator delete aPtr[allocateSize];
  71. }
  72. }
  73.  
  74. void construct(pointer aPtr, const_reference value)
  75. {
  76. if (aPtr != 0)
  77. {
  78. // ((void *)aPtr) is placement new
  79. // T(value) convert to T type
  80. ::operator new ((void *)aPtr) T(value);
  81. }
  82. }
  83. void destrory(pointer aPtr)
  84. {
  85. if (aPtr != 0)
  86. {
  87. *aPtr = 0;
  88. }
  89. }
  90.  
  91.  
  92. };
  93.  
  94. /*
  95. No refernce type to void* -That's why need
  96. specialization for void.
  97. */
  98.  
  99. // ==============  Global Functions  ===============
  100.  
  101. template<class T1, class T2>
  102. bool operator==(MyAllocator<T1>, MyAllocator<T2>) const
  103. {
  104. return MyAllocator<T1== MyAllocator<T2>;
  105. }
  106.  
  107. template<class T1, class T2>
  108. bool operator!=(MyAlloc<T1>, MyAlloc<T2>) const
  109. {
  110. return MyAllocator<T1!= MyAllocator<T2>;
  111. }
  112.  
  113. // ================================================
  114.  
  115.  
  116.  
  117. #endif
  118.  
What should i do for next steps ?

Thanks for your help.
Aug 26 '08 #6

P: n/a
I have corrected my code.

Now my problem is this:

Error 1 error C2440: 'default argument' : cannot convert from
'MyAllocator<T>' to 'MyAllocator<T>' c:\program files\microsoft visual
studio 8\vc\include\list 83
Expand|Select|Wrap|Line Numbers
  1. #ifndef Custom_Allocator
  2. #define Custom_Allocator
  3.  
  4.  
  5. template <class T>
  6. class MyAllocator
  7. {
  8. public:
  9.  
  10. // Type Definitions
  11.  
  12. typedef T value_type;
  13.  
  14. typedef T* pointer;
  15. typedef const T* const_pointer;
  16.  
  17. typedef T& reference;
  18. typedef const T& const_reference;
  19.  
  20. typedef size_t size_type; // Unsigned
  21.  
  22. // Signed
  23. typedef ptrdiff_t difference_type;
  24.  
  25.  
  26. // Member Function
  27.  
  28. MyAllocator(){}
  29.  
  30. template <class U>
  31. MyAllocator(const MyAllocator<T&rhs){}
  32.  
  33. ~MyAllocator(){}
  34.  
  35. // MyAllocator<T>::rebind<U>::other;
  36. template <class U>
  37. struct rebind
  38. {
  39. typedef MyAllocator<Uother;
  40. };
  41.  
  42. pointer address(reference memory) const
  43. {
  44. return &memory;
  45. }
  46. const_pointer address(const_reference memory) const
  47. {
  48. return &memory;
  49. }
  50.  
  51. size_type max_size() const
  52. {
  53. return size_t(+99);
  54. }
  55.  
  56. pointer allocate(size_type allocateSize)
  57. {
  58. return static_cast<pointer(::operator new (allocateSize));
  59. }
  60.  
  61. void deallocate(pointer aPtr, size_type allocateSize)
  62. {
  63. if (aPtr != 0)
  64. {
  65. destroy(aPtr);
  66. ::operator delete aPtr[allocateSize];
  67. }
  68. }
  69.  
  70. void construct(pointer aPtr, const_reference value)
  71. {
  72. if (aPtr != 0)
  73. {
  74. // ((void *)aPtr) is placement new
  75. // T(value) convert to T type
  76. ::operator new ((void *)aPtr) T(value);
  77. }
  78. }
  79. void destroy(pointer aPtr)
  80. {
  81. if (aPtr != 0)
  82. {
  83. aPtr->~MyAllocator<T>;
  84. }
  85. }
  86.  
  87.  
  88.  
  89. };
  90.  
  91. /*
  92. No refernce type to void* -That's why need
  93. specialization for void.
  94. */
  95.  
  96. // ==============  Global Functions  ===============
  97.  
  98. // Non member function cannot declare as constant
  99. template<class T1, class T2>
  100. bool operator==(MyAllocator<T1>& first, MyAllocator<T2>& second)
  101. {
  102. return first == second;
  103. }
  104.  
  105. template<class T1, class T2>
  106. bool operator!=(MyAllocator<T1>& first, MyAllocator<T2>& second)
  107. {
  108. return first!=second;
  109. }
  110.  
  111.  
Please help me.
Sep 2 '08 #7

P: n/a
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4. #ifndef Custom_Allocator
  5. #define Custom_Allocator
  6.  
  7.  
  8.  
  9. template <class T>
  10. class MyAllocator
  11. {
  12. public:
  13.  
  14. // Type Definitions
  15.  
  16. typedef T value_type;
  17.  
  18. typedef T* pointer;
  19. typedef const T* const_pointer;
  20.  
  21. typedef T& reference;
  22. typedef const T& const_reference;
  23.  
  24. typedef size_t size_type; // Unsigned
  25.  
  26. // Signed
  27. typedef ptrdiff_t difference_type;
  28.  
  29.  
  30. // Member Function
  31.  
  32. MyAllocator(){}
  33.  
  34. template <class U>
  35. MyAllocator(const MyAllocator<T&rhs){}
  36.  
  37. ~MyAllocator(){}
  38.  
  39. // MyAllocator<T>::rebind<U>::other;
  40. template <class U>
  41. struct rebind
  42. {
  43. typedef MyAllocator<Uother;
  44. };
  45.  
  46. pointer address(reference memory) const
  47. {
  48. return &memory;
  49. }
  50. const_pointer address(const_reference memory) const
  51. {
  52. return &memory;
  53. }
  54.  
  55. size_type max_size() const
  56. {
  57. return size_t(+99);
  58. }
  59.  
  60. pointer allocate(size_type allocateSize)
  61. {
  62. return static_cast<pointer(::operator new (allocateSize));
  63. }
  64.  
  65. void deallocate(pointer aPtr, size_type allocateSize)
  66. {
  67. if (aPtr != 0)
  68. {
  69. destroy(aPtr);
  70. //        ::operator delete aPtr[allocateSize];
  71. }
  72. }
  73.  
  74. void construct(pointer aPtr, const_reference value)
  75. {
  76. if (aPtr != 0)
  77. {
  78. // ((void *)aPtr) is placement new
  79. // T(value) convert to T type
  80. ::operator new ((void *)aPtr) T(value);
  81. }
  82. }
  83. void destroy(pointer aPtr)
  84. {
  85. if (aPtr != 0)
  86. {
  87. //            aPtr->~MyAllocator<T>;
  88. }
  89. }
  90. };
  91.  
  92. /*
  93. No refernce type to void* -That's why need
  94. specialization for void.
  95. */
  96.  
  97. // ==============  Global Functions  ===============
  98.  
  99. // Non member function cannot declare as constant
  100. template<class T1, class T2>
  101. bool operator==(MyAllocator<T1>& first, MyAllocator<T2>& second)
  102. {
  103. return first == second;
  104. }
  105.  
  106. template<class T1, class T2>
  107. bool operator!=(MyAllocator<T1>& first, MyAllocator<T2>& second)
  108. {
  109. return first!=second;
  110. }
  111.  
  112.  
This is my code so far.
Sep 3 '08 #8

P: n/a

Please help me.
Sep 4 '08 #9

P: n/a
Pe********@gmail.com wrote:
[...]
template <class T>
class MyAllocator
{
public:

// Type Definitions

typedef T value_type;
[...more types...]
>
// Member Function

MyAllocator(){}

template <class U>
MyAllocator(const MyAllocator<T&rhs){}
^^^
The template parameter U is not used. Do you mean this instead?

template <class U>
MyAllocator(const MyAllocator<U&rhs){}
[...]
// ============== Global Functions ===============

// Non member function cannot declare as constant
You cannot declare the functions as constant, but the parameter:
template<class T1, class T2>
bool operator==(MyAllocator<T1>& first, MyAllocator<T2>& second)
bool operator==(const MyAllocator<T1>& first, const MyAllocator<T2>& second)
{
return first == second;
What function should be called here? The function will call itself. This
is infinite recursion.
}

template<class T1, class T2>
bool operator!=(MyAllocator<T1>& first, MyAllocator<T2>& second)
{
return first!=second;
}
Same as above.

--
Thomas
Sep 4 '08 #10

P: n/a
Pe********@gmail.com wrote:
Please help me.
We can't help if we don't know your problem.

If your code doesn't compile, write what error messages you get, and on
which line of code the error is.

If your code compiles but doesn't work, tell us what you expect and what
did you get instead.

Also see here:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

--
Thomas
Sep 4 '08 #11

P: n/a
Why cannot use "using" in header file ?

how do resolve the operator == and operator!= ?

Thanks for your help.
Sep 7 '08 #12

P: n/a
Another error is

Error 1 error C2146: syntax error : missing ';' before identifier
'aPtr' d:\c++\custom allocator\custom allocator\custom_allocator.h 87
Warning 2 warning C4551: function call missing argument list d:\c++
\custom allocator\custom allocator\custom_allocator.h 87
for this code

Expand|Select|Wrap|Line Numbers
  1. void deallocate(pointer aPtr, size_type allocateSize)
  2. {
  3. if (aPtr != 0)
  4. {
  5. destroy(aPtr);
  6. ::operator delete aPtr[allocateSize];
  7. }
  8. }
  9.  
  10.  
  11.  

This is my complete code:

Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. class MyAllocator
  3. {
  4. public:
  5.  
  6. // Type Definitions
  7.  
  8. typedef T value_type;
  9.  
  10. typedef T* pointer;
  11. typedef const T* const_pointer;
  12.  
  13. typedef T& reference;
  14. typedef const T& const_reference;
  15.  
  16. typedef size_t size_type; // Unsigned
  17.  
  18. // Signed
  19. typedef ptrdiff_t difference_type;
  20.  
  21.  
  22. // Member Function
  23.  
  24. MyAllocator(){}
  25.  
  26. template <class U>
  27. MyAllocator(const MyAllocator<U&rhs){}
  28.  
  29. ~MyAllocator(){}
  30.  
  31. // MyAllocator<T>::rebind<U>::other;
  32. template <class U>
  33. struct rebind
  34. {
  35. typedef MyAllocator<Uother;
  36. };
  37.  
  38. pointer address(reference memory) const
  39. {
  40. return &memory;
  41. }
  42. const_pointer address(const_reference memory) const
  43. {
  44. return &memory;
  45. }
  46.  
  47. size_type max_size() const
  48. {
  49. return size_t(+99);
  50. }
  51.  
  52. pointer allocate(size_type allocateSize)
  53. {
  54. return static_cast<pointer(::operator new (allocateSize));
  55. }
  56.  
  57. void deallocate(pointer aPtr, size_type allocateSize)
  58. {
  59. if (aPtr != 0)
  60. {
  61. destroy(aPtr);
  62. ::operator delete aPtr[allocateSize];
  63. }
  64. }
  65.  
  66. void construct(pointer aPtr, const_reference value)
  67. {
  68. if (aPtr != 0)
  69. {
  70. // ((void *)aPtr) is placement new
  71. // T(value) convert to T type
  72. ::operator new ((void *)aPtr) T(value);
  73. }
  74. }
  75. void destroy(pointer aPtr)
  76. {
  77. if (aPtr != 0)
  78. {
  79. aPtr->~MyAllocator<T>;
  80. }
  81. }
  82. };
  83.  
  84. /*
  85. No refernce type to void* -That's why need
  86. specialization for void.
  87. */
  88.  
  89. // ==============  Global Functions  ===============
  90.  
  91. // Non member function cannot declare as constant
  92. template<class T1, class T2>
  93. bool operator==(MyAllocator<T1>& first, MyAllocator<T2>& second)
  94. {
  95. return first == second;
  96. }
  97.  
  98. template<class T1, class T2>
  99. bool operator!=(MyAllocator<T1>& first, MyAllocator<T2>& second)
  100. {
  101. return first!=second;
  102. }
  103.  
  104.  
  105.  
  106. Please correct me if i wrong.
  107.  
  108. A billion thanks for your help.
  109.  
  110.  
Sep 7 '08 #13

P: n/a
On 2008-09-07 11:27, Pe********@gmail.com wrote:
Why cannot use "using" in header file ?
Since you have not quoted the part you are replying to I can only guess
you meant "why can I not use "using namespace" in a header file?" and
the answer is that you can, but it is bad. The reason is simple, every
file that included your header will get the effects of the "using
namespace", consider the following:

------ Test.h -----

#ifndef TEST_H
#define TEST_H

#include <iostream>

using namespace std;

void foo()
{
std::cout << "foo()\n";
}

#endif

------ Test.cpp -----

#include <vector>

#include "Test.h"

template<typename T>
class vector
{
T t;
};

int main()
{
vector<intv; // Error
}

The problem is that the "using namespace std;" in Test.h" is in effect
in Test.cpp, and vector<intcan now refer to your class or std::vector.
In some cases you might get even worse problems because you do not get
an ambiguity and instead you use some type/function which you did not
intend to use, and the only clue is that the program does not work as it
should.

Therefore you should avoid "using namespace" in headers.

--
Erik Wikström
Sep 7 '08 #14

P: n/a


"why can I not use "using namespace" in a header file?"

Are you trying to explain that name clashed between user defined code
and standard namespace ?

After listen to you advise, i guess we can include any header file in
our own header file but cannot using whole namespace instead explicit
specify which function or class to use.

In the example you given, compiler cannot resolve vector defined by us
ans std. Therefore, an ambiguity has occurred.

Am i correct ?
Another question is how about code at below ?

Expand|Select|Wrap|Line Numbers
  1. pointer allocate(size_type allocateSize)
  2. {
  3. return static_cast<pointer(::operator new (allocateSize));
  4. }
  5.  
  6. void deallocate(pointer aPtr, size_type allocateSize)
  7. {
  8. if (aPtr != 0)
  9. {
  10. destroy(aPtr);
  11. ::operator delete aPtr[allocateSize];
  12. }
  13. }
  14.  
  15. void construct(pointer aPtr, const_reference value)
  16. {
  17. if (aPtr != 0)
  18. {
  19. // ((void *)aPtr) is placement new
  20. // T(value) convert to T type
  21. ::operator new ((void *)aPtr) T(value);
  22. }
  23. }
  24. void destroy(pointer aPtr)
  25. {
  26. if (aPtr != 0)
  27. {
  28. aPtr->~MyAllocator<T>;
  29. }
  30. }
  31.  
  32. template<class T1, class T2>
  33. bool operator==(const MyAllocator<T1>& first,
  34. const MyAllocator<T2>& second)
  35. {
  36. return true;
  37. }
  38.  
  39. template<class T1, class T2>
  40. bool operator!=(const MyAllocator<T1>& first,
  41. const MyAllocator<T2>& second)
  42. {
  43. return false;
  44. }
  45.  
  46.  
  47.  

What code is need to add ?
A billion thanks for your help.
Sep 8 '08 #15

P: n/a
Yet another question is when i try to compile my code.

It shown my error :

Expand|Select|Wrap|Line Numbers
  1. void destroy(pointer aPtr)
  2. {
  3. if (aPtr != 0)
  4. {
  5. aPtr->~MyAllocator<T>;
  6. }
  7. }
  8.  
  9. error C2325: 'MyAllocator<T>' : unexpected type to the right of '->~':
  10. expected 'std::_List_nod<_Ty,_Alloc>::_Node '
  11.  
  12.  
  13. void construct(pointer aPtr, const_reference value)
  14. {
  15. if (aPtr != 0)
  16. {
  17. // ((void *)aPtr) is placement new
  18. // T(value) convert to T type
  19. ::operator new ((void *)aPtr) T(value);
  20. }
  21. }
  22.  
  23. error C2665: 'operator new' : none of the 5 overloads could convert
  24. all the argument types
  25. error C2146: syntax error : missing ';' before identifier 'T'
  26.  
  27.  
  28.  
  29. void deallocate(pointer aPtr, size_type allocateSize)
  30. {
  31. if (aPtr != 0)
  32. {
  33. destroy(aPtr);
  34. ::operator delete aPtr[allocateSize];
  35. }
  36. }
  37.  
  38. error C2146: syntax error : missing ';' before identifier 'aPtr'
  39. warning C4551: function call missing argument list
  40.  
  41.  
  42.  
  43.  

A billion thanks for your help.
Sep 8 '08 #16

P: n/a
This is my intention of my code

Expand|Select|Wrap|Line Numbers
  1. void deallocate(pointer aPtr, size_type allocateSize)
  2. {
  3. if (aPtr != 0)
  4. {
  5. destroy(aPtr);
  6. //             ::operator delete aPtr[allocateSize];
  7.  
  8. // I want delete the memory of container
  9. }
  10. }
  11.  
  12. void construct(pointer aPtr, const_reference value)
  13. {
  14. if (aPtr != 0)
  15. {
  16. // ((void *)aPtr) is placement new
  17. // T(value) convert to T type
  18. ::operator new ((void *)aPtr) T(value);
  19.  
  20. // I want to do placement new to initialize the memory which has
  21. allocated
  22. }
  23. }
  24. void destroy(pointer aPtr)
  25. {
  26. if (aPtr != 0)
  27. {
  28. aPtr->~MyAllocator<T>;
  29. // I want to do uninitialize the container value hold.
  30.  
  31. }
  32. }
  33.  
  34.  

A billion thanks for your help.
Sep 8 '08 #17

This discussion thread is closed

Replies have been disabled for this discussion.