473,386 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Behaviour of custom allocator for vector

Compiler GNU g++ version 3.4.4 (cygming special)

Custom allocator for vector (see below) checks a return value of
'operator new'.
If that value is NULL, the allocator "allocates" no memory.
Nevertheless the allocator produces "Segmentation fault (core dumped)".
Is it possible to correct that allocator in order to avoid such
behavior

------ foo.cpp ------
#include <vector>
#include <cstddef>
#include <iostream>
using namespace std;

template <class T> class my_alloc
{
public:
typedef T value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;

pointer allocate(size_type sz, void* vp = 0)
{
pointer p = static_cast<pointer>(operator new (sz *
sizeof(value_type), nothrow));
if (p == 0)
{
cerr << "[1] Unable to alloc: size = " << sz << endl;
p = static_cast<pointer>(operator new (0 * sizeof(value_type),
nothrow));
if (p == 0)
{
cerr << "[2] Unable to alloc: size = 0" << endl;
}
else
{
cerr << "[3] Alloc: size = 0" << endl;
}
}

return p;
}

void deallocate(pointer p, size_type)
{
if (p == 0)
{
cerr << "[4] Unable to dealloc" << endl;
return;
}
cerr << "[5] Dealloc" << endl;
operator delete(p, nothrow);
}
};

int main()
{
vector<int, my_alloc<int> > v (0xfffffffe);

return 0;

}

---------------------
------ Run ---

[1] Unable to alloc: size = 4294967294
[3] Alloc: size = 0
Segmentation fault (core dumped)

--------------
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Apr 1 '06 #1
3 2121

Alex Vinokur wrote:
Compiler GNU g++ version 3.4.4 (cygming special)

Custom allocator for vector (see below) checks a return value of
'operator new'.
If that value is NULL, the allocator "allocates" no memory.
Nevertheless the allocator produces "Segmentation fault (core dumped)".


You should throw a bad_alloc exception rather than returning NULL from
the allocate() function. You may as well allow ::operator new() to
throw bad_alloc (it could contain useful debugging information --
though in gcc 3.4.4 I think it doesn't) and catch, log and rethrow in
that event.

--
Richard Smith

Apr 1 '06 #2
rich...@ex-parrot.com wrote:
Alex Vinokur wrote:
Compiler GNU g++ version 3.4.4 (cygming special)

Custom allocator for vector (see below) checks a return value of
'operator new'.
If that value is NULL, the allocator "allocates" no memory.
Nevertheless the allocator produces "Segmentation fault (core dumped)".


You should throw a bad_alloc exception rather than returning NULL from
the allocate() function. You may as well allow ::operator new() to
throw bad_alloc (it could contain useful debugging information --
though in gcc 3.4.4 I think it doesn't) and catch, log and rethrow in
that event.

[snip]

I use nothrow in 'operator new", I want to avoid exception handling.

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Apr 1 '06 #3
Alex Vinokur wrote:
Compiler GNU g++ version 3.4.4 (cygming special)

Custom allocator for vector (see below) checks a return value of
'operator new'.
If that value is NULL, the allocator "allocates" no memory.
Nevertheless the allocator produces "Segmentation fault (core dumped)".
Is it possible to correct that allocator in order to avoid such
behavior
Yes: use exceptions.

------ foo.cpp ------
#include <vector>
#include <cstddef>
#include <iostream>
using namespace std;

template <class T> class my_alloc
{
public:
typedef T value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;

pointer allocate(size_type sz, void* vp = 0)
{
pointer p = static_cast<pointer>(operator new (sz *
sizeof(value_type), nothrow));
if (p == 0)
{
cerr << "[1] Unable to alloc: size = " << sz << endl;
p = static_cast<pointer>(operator new (0 * sizeof(value_type),
nothrow));
if (p == 0)
{
cerr << "[2] Unable to alloc: size = 0" << endl;
}
else
{
cerr << "[3] Alloc: size = 0" << endl;
}
}

return p;
}

void deallocate(pointer p, size_type)
{
if (p == 0)
{
cerr << "[4] Unable to dealloc" << endl;
return;
}
cerr << "[5] Dealloc" << endl;
operator delete(p, nothrow);
}
};

int main()
{
vector<int, my_alloc<int> > v (0xfffffffe);

return 0;

}

---------------------
------ Run ---

[1] Unable to alloc: size = 4294967294
[3] Alloc: size = 0
Segmentation fault (core dumped)


It is not the allocator that produces the segfault. However, your allocator
does not keep the contract that an allocator is required to obey. What
happens is that vector assumes the returned pointer to be valid and tries
to construct and object in that place. This causes the segfault. There is
no way around that. The containers work with allocators in such a way that
error handling is done by throwing exceptions. There are no hooks into
std::vector that allow for a different method of error handling.
Best

Kai-Uwe Bux
Apr 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Brian Genisio | last post by:
Hi all, I am developing some software, that creates a tree of information. For each node, currently I am overriding the new operator, because it is a requirement that after initialization, no...
3
by: Orjan Westin | last post by:
Hi, I have an interesting (read frustrating) problem. I'm writing a generic container class, which holds data as well as links to other instances of itself, like this: template<class T>...
13
by: John Harrison | last post by:
If you specify an allocator in an STL container is it a requirement that the allocator allocates object of the right type, or can you assume that the container will rebind the allocator to the...
15
by: Alex Vinokur | last post by:
I am looking for any custom allocator sample code for std::vector. Thanks. -- Alex Vinokur http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn
15
by: natespamacct | last post by:
Hi All, I'm not sure if I'm dealing with a C++ question or a compiler question, so please forgive me if I'm asking in the wrong spot. If so, maybe someone can direct me to more appropriate spot....
3
by: slonial | last post by:
Hi, I am using two STL maps as data member for COM server with VC++6.0.These two maps are unrelated in the sense that they are storing different data. first map is of type(6 MB in size)...
4
by: sreedhar.cs | last post by:
Hi all, In my application,I want to place a vector in a specific location in shared memory.(a user supplied pointer). I understand that the STL allocator mechanism places the data objects within...
8
by: Paulo da Silva | last post by:
Hi! Why doesn't this work? If I change the name of the vector toLower for ex. to toLowerV it works! (GCC) Thanks. Paulo ..h _______________
16
by: PeterAPIIT | last post by:
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.