473,388 Members | 1,526 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,388 software developers and data experts.

a question about exception safety in the book 'the c++ programminglanguage'

Hi,

I am reading the book 'the c++ programming language' 3rd Edition by
Bjarne Stroustrup.

I have a question regarding the initialization. He said the v might be
0 in the following implementation(clumsy use of contructor). But I
think if v is not correctly initialized, an exception will be thrown
in this statement: v = alloc.allocated(n); So you won't get a vector
with v=0; It should have the same effect by saying
v(alloc.allocated(n)) in the constructor list. The following is the
code from the book:

vector implementation(clumsy use of contructor):

t e m p l a t e <c l a s s T , c l a s s A = a l l o c a t o r <T >
// clumsy use of constructor
c l a s s v e c t o r _ b a s e {
p u b l i c :
A a l l o c ; // allocator
T * v ; // start of allocation
T * s p a c e ; // end of element sequence, start of space allocated
for possible expansion
T * l a s t ; // end of allocated space
v e c t o r _ b a s e (c o n s t A & a , t y p e n a m e A :: s i z e
_ t y p e n ) : a l l o c (a ), v (0 ), s p a c e (0 ), l a s t (0 )
{
v = a l l o c .a l l o c a t e (n );
s p a c e = l a s t = v +n ;
}
~v e c t o r _ b a s e () { i f (v ) a l l o c .d e a l l o c a t e
(v ,l a s t -v ); }
}

a right implmentation in E3.2:

t e m p l a t e <c l a s s T , c l a s s A = a l l o c a t o r <T
s t r u c t v e c t o r _ b a s e {
A a l l o c ; // allocator
T * v ; // start of allocation
T * s p a c e ; // end of element sequence, start of space allocated
for possible expansion
T * l a s t ; // end of allocated space
v e c t o r _ b a s e (c o n s t A & a , t y p e n a m e A :: s i z e
_ t y p e n )
: a l l o c (a ), v (a .a l l o c a t e (n )), s p a c e (v +n ), l a
s t (v +n ) { }
~v e c t o r _ b a s e () { a l l o c .d e a l l o c a t e (v ,l a s t
-v ); }
};

Mar 6 '08 #1
0 1045

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

Similar topics

5
by: John Harrison | last post by:
Where can I find information on exception safety in the STL? I.e. which methods on which types offer what level of exception safety. Josuttis has a useful list of classes and methods but he fails...
0
by: Luke Meyers | last post by:
I'm attempting a strict analysis of the exception safety of a set of classes which use various STL containers. Appendix E of has been very helpful, but I have been unable to find any mention of...
4
by: robinsand | last post by:
Header File: car.h #if !defined CAR_H #define CAR_H enum TCarType { ctEconomy = 1, ctCompact, ctStandard, ctFullSize, ctMiniVan, ctSUV }; class Car { public: Car();
4
by: Binary | last post by:
Hi, I am reading a chinese book about STL, the book says below code will have no memory leak. I think its magic and wonder why the auto_ptr knows the memory should be freed when leaving the...
7
by: myfavdepo | last post by:
Hi all, I have a query regarding the exchanging of a boost::shared_ptr beween different threads. In my program i've two threads both of which having their own internal queues for storing the...
0
by: Graham Wideman | last post by:
Folks: Can anyone tell me what controls php's "thread safety" feature? I have an installation where phpinfo() is showing Thread safety: enabled, whereas I need it disabled in order to work...
8
by: dragon9 | last post by:
I want to write some small programs with exception safety, but I don't how to write. Because my programs wrap C-api function( example: win32 api). I don't find my direction. I wish any hot heart...
11
by: George2 | last post by:
Hello everyone, How do you understand the Bjarne's comments about exception specification? Especially, "not required to be checked across compilation-unit" and "violations will not be caught...
19
by: =?ISO-8859-1?Q?Nordl=F6w?= | last post by:
I am currently designing a synchronized queue used to communicate between threads. Is the code given below a good solution? Am I using mutex lock/unlock more than needed? Are there any resources...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
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...

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.