Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > General Allocator Regarding type definitions and void * specializedproblem

Reply
Thread Tools

General Allocator Regarding type definitions and void * specializedproblem

 
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      09-04-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
PeterAPIIT@gmail.com
Guest
Posts: n/a
 
      09-07-2008
Why cannot use "using" in header file ?

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

Thanks for your help.
 
Reply With Quote
 
 
 
 
PeterAPIIT@gmail.com
Guest
Posts: n/a
 
      09-07-2008
Another error is

Quote:
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

Code:
void deallocate(pointer aPtr, size_type allocateSize)
	{
		if (aPtr != 0)
		{
			destroy(aPtr);
			::operator delete aPtr[allocateSize];
		}
	}

This is my complete code:

Code:
template <class T>
class MyAllocator
{
public:

// Type Definitions

	typedef T value_type;

	typedef T* pointer;
	typedef const T* const_pointer;

	typedef T& reference;
	typedef const T& const_reference;

	typedef size_t size_type; // Unsigned

	// Signed
	typedef ptrdiff_t difference_type;


// Member Function

	MyAllocator(){}

	template <class U>
	MyAllocator(const MyAllocator<U> &rhs){}

	~MyAllocator(){}

	// MyAllocator<T>::rebind<U>::other;
	template <class U>
	struct rebind
	{
		typedef MyAllocator<U> other;
	};

	pointer address(reference memory) const
	{
		return &memory;
	}
	const_pointer address(const_reference memory) const
	{
		return &memory;
	}

	size_type max_size() const
	{
		return size_t(+99);
	}

	pointer allocate(size_type allocateSize)
	{
		return static_cast<pointer> (::operator new (allocateSize));
	}

	void deallocate(pointer aPtr, size_type allocateSize)
	{
		if (aPtr != 0)
		{
			destroy(aPtr);
			::operator delete aPtr[allocateSize];
		}
	}

	void construct(pointer aPtr, const_reference value)
	{
		if (aPtr != 0)
		{
			// ((void *)aPtr) is placement new
			// T(value) convert to T type
			::operator new ((void *)aPtr) T(value);
		}
	}
	void destroy(pointer aPtr)
	{
		if (aPtr != 0)
		{
			aPtr->~MyAllocator<T>;
		}
	}
};

/*
	No refernce type to void* -That's why need
	specialization for void.
*/

// ==============  Global Functions  ===============

// Non member function cannot declare as constant
template<class T1, class T2>
bool operator==(MyAllocator<T1>& first, MyAllocator<T2>& second)
{
	return first == second;
}

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



Please correct me if i wrong.

A billion thanks for your help.
 
Reply With Quote
 
Erik Wikström
Guest
Posts: n/a
 
      09-07-2008
On 2008-09-07 11:27, (E-Mail Removed) 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<int> v; // Error
}

The problem is that the "using namespace std;" in Test.h" is in effect
in Test.cpp, and vector<int> can 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
 
Reply With Quote
 
PeterAPIIT@gmail.com
Guest
Posts: n/a
 
      09-08-2008

Quote:

"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 ?

Code:
pointer allocate(size_type allocateSize)
	{
		return static_cast<pointer> (::operator new (allocateSize));
	}

	void deallocate(pointer aPtr, size_type allocateSize)
	{
		if (aPtr != 0)
		{
			destroy(aPtr);
			::operator delete aPtr[allocateSize];
		}
	}

	void construct(pointer aPtr, const_reference value)
	{
		if (aPtr != 0)
		{
			// ((void *)aPtr) is placement new
			// T(value) convert to T type
			::operator new ((void *)aPtr) T(value);
		}
	}
	void destroy(pointer aPtr)
	{
		if (aPtr != 0)
		{
			aPtr->~MyAllocator<T>;
		}
	}

template<class T1, class T2>
bool operator==(const MyAllocator<T1>& first,
				const MyAllocator<T2>& second)
{
	return true;
}

template<class T1, class T2>
bool operator!=(const MyAllocator<T1>& first,
				const MyAllocator<T2>& second)
{
	return false;
}

What code is need to add ?


A billion thanks for your help.
 
Reply With Quote
 
PeterAPIIT@gmail.com
Guest
Posts: n/a
 
      09-08-2008
Yet another question is when i try to compile my code.

It shown my error :

Code:
void destroy(pointer aPtr)
	{
		if (aPtr != 0)
		{
			aPtr->~MyAllocator<T>;
		}
	}

error C2325: 'MyAllocator<T>' : unexpected type to the right of '->~':
expected 'std::_List_nod<_Ty,_Alloc>::_Node '


void construct(pointer aPtr, const_reference value)
	{
		if (aPtr != 0)
		{
			// ((void *)aPtr) is placement new
			// T(value) convert to T type
			::operator new ((void *)aPtr) T(value);
		}
	}

error C2665: 'operator new' : none of the 5 overloads could convert
all the argument types
error C2146: syntax error : missing ';' before identifier 'T'



void deallocate(pointer aPtr, size_type allocateSize)
	{
		if (aPtr != 0)
		{
			destroy(aPtr);
			::operator delete aPtr[allocateSize];
		}
	}

error C2146: syntax error : missing ';' before identifier 'aPtr'
warning C4551: function call missing argument list

A billion thanks for your help.
 
Reply With Quote
 
PeterAPIIT@gmail.com
Guest
Posts: n/a
 
      09-08-2008
This is my intention of my code

Code:
void deallocate(pointer aPtr, size_type allocateSize)
	{
		if (aPtr != 0)
		{
			destroy(aPtr);
// 			::operator delete aPtr[allocateSize];

// I want delete the memory of container
		}
	}

	void construct(pointer aPtr, const_reference value)
	{
		if (aPtr != 0)
		{
			// ((void *)aPtr) is placement new
			// T(value) convert to T type
			::operator new ((void *)aPtr) T(value);

// I want to do placement new to initialize the memory which has
allocated
		}
	}
	void destroy(pointer aPtr)
	{
		if (aPtr != 0)
		{
			aPtr->~MyAllocator<T>;
// I want to do uninitialize the container value hold.

}
	}

A billion thanks for your help.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting 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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the difference between void proba(); and void proba(void); ??? PencoOdStip@gmail.com C++ 1 05-23-2007 07:12 PM
CAN WE TYPE CAST AN INTEGER AS (VOID *)X..(Like can I return a value (void *)x) Abhishek C Programming 12 01-30-2006 03:22 PM
what is the difference, void func(void) and void fucn() noblesantosh@yahoo.com C Programming 5 07-22-2005 04:38 PM
"void Method()" vs "void Method(void)" Ollej Reemt C++ 7 04-22-2005 03:47 AM
`void **' revisited: void *pop(void **root) Stig Brautaset C Programming 15 10-28-2003 09:03 AM



Advertisments