473,803 Members | 3,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

could/should allocator<T> be a static member?

Hi all,

I saw this code in the book "Accelerate d C++" (chapt 11, iirc):

template <class T> class Vec {
...
std::allocator< T> alloc; // object to handle memory allocation

// ??would static member work here??:
// static std::allocator< T> alloc;
...
};

If i understand this correctly, in the code above,
every instance of Vec<T> gets its own allocator.

Wouldn't it be better "style" if alloc was a _static_ member?
Then all instances of Vec<T> would share one common allocator.
Is a static allocator guaranteed to work?

Thanks for comments/insights,
Bernhard Kick.
Jul 22 '05 #1
3 1880
"Bernhard Kick" <be***********@ gmx.de> wrote in message
news:f2******** *************** ***@posting.goo gle.com...
I saw this code in the book "Accelerate d C++" (chapt 11, iirc):

template <class T> class Vec {
...
std::allocator< T> alloc; // object to handle memory allocation

// ??would static member work here??:
// static std::allocator< T> alloc;
...
};

If i understand this correctly, in the code above,
every instance of Vec<T> gets its own allocator.

Wouldn't it be better "style" if alloc was a _static_ member?
Then all instances of Vec<T> would share one common allocator.
Is a static allocator guaranteed to work?


Two considerations:

1) An allocator is often a zero-sized object. With proper
finagling, you can get it to occupy no space in each
vector object. Making it a static would then use *more*
space (however small), but also add a minor complication
to instantiating such a container in a DLL.

2) An allocator that is not a zero-sized object may well
have writable storage, for maintaining a per-container
pool, for example. The standard containers are not required
to accept such allocators, but the C++ Standard encourages
implementations to do so. The Dinkum C++ Library accepts
them. Making such an allocator static would then require
thread locking to avoid conflicts if more than one thread
is working with containers of the given type.

So it's probably not a good idea to make allocators static
under two circumstances:

1) if allocators are zero-sized objects

2) if allocators are not zero-sized objects

I won't comment on whether it would be better *style* to
do so.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 22 '05 #2
"P.J. Plauger" <pj*@dinkumware .com> wrote in message news:<jg******* **********@nwrd dc03.gnilink.ne t>...
Two considerations:

1) An allocator is often a zero-sized object. With proper
finagling, you can get it to occupy no space in each
vector object. Making it a static would then use *more*
space (however small), but also add a minor complication
to instantiating such a container in a DLL.
proper finagling? hmm...(had to look up the word in the dictionary)
2) An allocator that is not a zero-sized object may well
have writable storage, for maintaining a per-container
pool, for example. The standard containers are not required
to accept such allocators, but the C++ Standard encourages
implementations to do so. The Dinkum C++ Library accepts
them. ...
Aha! So std::allocator could have private state.
Then of course its a *big* difference whether you make it
static or not. Making it static could break the code. Correct?
So it's probably not a good idea to make allocators static
under two circumstances:

1) if allocators are zero-sized objects
2) if allocators are not zero-sized objects
The following little test seems to show that
non-static member takes space, but static does not.
(gcc under cygwin):

//------------------------------------------------------------------------
#include <memory>
#include <iostream>
using namespace std;

class small { int a; };
class big { int a[200]; };

class small_alloc {
small a;
allocator<small > alloc;
};

class small_alloc_sta tic {
small a;
static allocator<small > alloc;
};

class big_alloc {
big a;
allocator<big> alloc;
};

class big_alloc_stati c {
big a;
static allocator<big> alloc;
};

int main(int argc, char *argv[]) {
cout << "size(alloc<sma ll>)=" << sizeof(allocato r<small>)
<< " size(alloc<big> )=" << sizeof(allocato r<big>)
<< endl;

cout << "size(small )=" << sizeof(small)
<< " size(small_allo c)=" << sizeof(small_al loc)
<< " size(small_allo c_static)=" << sizeof(small_al loc_static)
<< endl;

cout << "size(big)= " << sizeof(big)
<< " size(big_alloc) =" << sizeof(big_allo c)
<< " size(big_alloc_ static)=" << sizeof(big_allo c_static)
<< endl;

return 0;
}
//---------------------------------------------------------------

The result is:
size(alloc<smal l>)=1 size(alloc<big> )=1
size(small)=4 size(small_allo c)=8 size(small_allo c_static)=4
size(big)=800 size(big_alloc) =804 size(big_alloc_ static)=800

From this i would conclude that:

1) the gcc std::allocator< T> is a zero-sized object.
(sizeof(allocat or<T>) returns 1, but i cannot imagine it
really has 1 byte of private state.)

2) the static member alloc takes _no_ space, but the non-static does.
So I'd prefer to make it a static member, unless it breaks the code.

The result could also mean that gcc cannot do "proper finagling"
(whatever that means...would you care to explain?)
I won't comment on whether it would be better *style* to
do so.


No need to comment on style.

I am coming form the traditional malloc/free() world,
*one* global allocator for *all* types of objects.

Thanks very much for your technical explanations,
which help me to better understand what a C++ std::allocator is.

Bernhard Kick.
Jul 22 '05 #3
"Bernhard Kick" <be***********@ gmx.de> wrote in message
news:f2******** *************** ***@posting.goo gle.com...
2) An allocator that is not a zero-sized object may well
have writable storage, for maintaining a per-container
pool, for example. The standard containers are not required
to accept such allocators, but the C++ Standard encourages
implementations to do so. The Dinkum C++ Library accepts
them. ...
Aha! So std::allocator could have private state.
Then of course its a *big* difference whether you make it
static or not. Making it static could break the code. Correct?


In the sense that it's a sucker for race conditions in a
multithreaded environment, yes.
So it's probably not a good idea to make allocators static
under two circumstances:

1) if allocators are zero-sized objects
2) if allocators are not zero-sized objects


The following little test seems to show that
non-static member takes space, but static does not.
(gcc under cygwin):


Of course. Non-static members exist in each object, while
static members are shared across all objects and have just
one instance.
From this i would conclude that:

1) the gcc std::allocator< T> is a zero-sized object.
(sizeof(allocat or<T>) returns 1, but i cannot imagine it
really has 1 byte of private state.)
It doesn't. C++ generally requires that distinct objects have
distinct addresses, so the zero-sized object is given one
padding byte to distinguish it from whatever comes next.
2) the static member alloc takes _no_ space, but the non-static does.
So I'd prefer to make it a static member, unless it breaks the code.
See multithread discussion above.
The result could also mean that gcc cannot do "proper finagling"
(whatever that means...would you care to explain?)


An exception to the rule about distinct addresses occurs for
base objects. Put a zero-sized object (alone) in a base class
and it need not have any padding. Thus it contributes nothing
to the size of the class. Our library does this trick, as do
others.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 22 '05 #4

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

Similar topics

9
2964
by: Francesco Moi | last post by:
Hello. I'm trying to build a RSS feed for my website. It starts: ----------------//--------------------- <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN" "http://my.netscape.com/publish/formats/rss-0.91.dtd"> <rss version="0.91"> ----------------//----------------------
7
4343
by: Piotre Ugrumov | last post by:
I have tried to implement the overload of these 2 operators. ostream & operator<<(ostream &out, Person &p){ out<<p.getName()<<" "<<p.getSurname()<<", "<<p.getDateOfBirth()<<endl; return out; } This overload work but I have a curiosty If I try to approch to the name or to the surname or to the dateofbirth in this way i receive error: ostream & operator<<(ostream &out, Person &p){ out<<p.name<<" "<<p.surname<<", "<<p.dateofbirth<<endl;
5
1867
by: Vinu | last post by:
Hi I am facing a problem in compilation the error is like this In constructor xServices::CServices<TImp>::StHoldClientList::StHoldClientList(std::set<TImp*, std::less<TImp*>, std::allocator<TImp*> >&)': : error: expected `;' before "pos" : error: `pos' undeclared (first use this function) : error: (Each undeclared identifier is reported only once for each function it appears in.)
11
13730
by: Les Paul | last post by:
I'm trying to design an HTML page that can edit itself. In essence, it's just like a Wiki page, but my own very simple version. It's a page full of plain old HTML content, and then at the bottom, there's an "Edit" link. So the page itself looks something like this: <HTML><HEAD><TITLE>blah</TITLE></HEAD><BODY> <!-- TEXT STARTS HERE --> <H1>Hello World!</H1> <P>More stuff here...</P>
4
1523
by: an0 | last post by:
Bjarne Stroustrup's "The C++ Language Programming" says at P330: "Within the scope of String<C>, qualification with <C> is redundant for the name of the template itself, so String<C>::String is the name for the constructor. If you prefer, you can be explicit: template<class T> String<C>: String<C> () { /*...*/ }" But in fact, I find I cannot use this 'explicit' form for any member function of a template class with g++ or Comeau. Is it...
1
7298
by: sharmadeep1980 | last post by:
Hi All, I am facing a very unique problem while compling my project in "Release" build. The project is building in DEBUG mode but giving linking error on Release build. Here is the error: Creating library Release/fnimqcmd.lib and object Release/fnimqcmd.exp CoIMQCmd.obj : error LNK2001: unresolved external symbol
5
2561
by: Martin Jørgensen | last post by:
Hi, The piece of code I'm struggling with is so simple, that I hope nobody wants a complete example for answering the question: -------- string color_line; int data_type = 0; for( vector<string>::const_iterator it = possible_data_types.begin();
7
3633
by: Nathan Sokalski | last post by:
Something that I recently noticed in IE6 (I don't know whether it is true for other browsers or versions of IE) is that it renders <br/and <br></br> differently. With the <br/version, which is what most people use when they write static code (some people use <br>, but with xhtml you are required to close all tags), IE6 simply breaks to the next line like it is supposed to. However, with <br></br>, which is what is sometimes generated by...
1
10410
by: =?Utf-8?B?TWFuaXNoIEJhZm5h?= | last post by:
Hi, I am getting following error while validating xml file with schema using ReaderSettings in .NET 2.0 "Line: 0 - Position: 0 - The root element of a W3C XML Schema should be <schemaand its namespace should be 'http://www.w3.org/2001/XMLSchema'." First few lines of xsd file are as follows: <?xml version="1.0" encoding="utf-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.keaneaustralia.com/Nts"...
0
9566
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10317
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10300
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7607
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.