15,894,460 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View Python questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
Comments by Yerasel (Top 2 by date)
Yerasel
21-Feb-12 9:17am
View
Deleted
// they all invoke overload new and delete
int* test1 = new int(10);
int* test2 = new int[5];
vector<int,
="" allocator<int="">
> intvec;
vector<int,
="" allocator<int="">
>::iterator it;
intvec.insert(it, 10);
Yerasel
20-Feb-12 4:21am
View
I tried to define a simple Allocator following above statements. In my Allocator ::operator new() is inoked, and than I define vector. However it turns out that vector will call overloaded new and delete if these operators are overloaded globally.
The code as following:
#include <stdlib.h>
#include <vector>
#include <stdio.h>
#include <limits>
using namespace std;
void* operator new(size_t s)
{
void* mem = malloc(s);
puts("My new");
// avoid using iostream, for it will use new & delete,that will lead to deadlock
if (!mem) puts("out");
return mem;
}
void operator delete(void* mem)
{
puts("My free");
free(mem);
}
template<typename t="">
class Allocator {
public :
// typedefs
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
public :
// convert an allocator<t> to allocator
template<typename u="">
struct rebind {
typedef Allocator
other;
};
public :
inline explicit Allocator() {}
inline ~Allocator() {}
inline explicit Allocator(Allocator const&) {}
template<typename u="">
inline explicit Allocator(Allocator
const&) {}
// address
inline pointer address(reference r) { return &r; }
inline const_pointer address(const_reference r) { return &r; }
// memory allocation
inline pointer allocate(size_type cnt,
typename std::allocator<void>::const_pointer = 0) {
return reinterpret_cast<pointer>(::operator new(cnt * sizeof (T)));
}
inline void deallocate(pointer p, size_type) {
::operator delete(p);
}
// size
inline size_type max_size() const {
return std::numeric_limits<size_type>::max() / sizeof(T);
}
// construction/destruction
inline void construct(pointer p, const T& t) { new(p) T(t); }
inline void destroy(pointer p) { p->~T(); }
inline bool operator==(Allocator const&) { return true; }
inline bool operator!=(Allocator const& a) { return !operator==(a); }
}; // end of class Allocator
int main()
{
// they all invoke overload new and delete
int* test1 = new int(10);
int* test2 = new int[5];
vector<int, allocator<int=""> > intvec;
vector<int, allocator<int=""> >::iterator it;
intvec.insert(it, 10);
delete test1;
delete []test2;
return 0;
}