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

C-style File Operations, C++ Streams, Exception Safety

Hello, all,

My most recent assignment has me working on a medium- to large-sized
Windows-based C++ software project. My background is entirely on UNIX
systems, where it appears that most of my peers were writing "better" C++
code. By "better" I mean that it more regularly looked like C++ (use of
objects, streams, exceptions, and ANSI-compliance) whereas some of my recent
Windows-based C++ peers rely on un-portable, operating-system specific
methods. Also, there's a preponderance of C-style character/file operations
(sprintf(), fprintf(), etc.)

I've always believed in the C++ stream operations because I found them
intuitive and robust. My old team had also decided that mixing the two
stream operations was bad style and mandated that we stick to C++ streams.
I also have concerns about the operation of the C-style character operations
in the presence of exceptions. Given that they were written for C, they
probably are NOT exception-safe, right?

Is the use of C-style character/stream operations in our C++ project
something I should worry about? For those of you that are developing C++
code in Windows system, do you find that this free selection between these
two choices is prevalent? For what reasons should I or should I not worry
about this? What are recommended alternatives to generate the most
reliable, ANSI-compliant code on systems where C++-style streams are not
common?

Thanks!
Scott

--
Remove .nospam from my e-mail address to mail me.

http://www.e-scott.net
Jul 22 '05 #1
3 2113
Scott Brady Drummonds wrote:
I also have concerns about the operation of the C-style character operations in the presence of exceptions. Given that they were written for C, they probably are NOT exception-safe, right?
Actually, it is quite likely that they *ARE* exception-safe:
Since they are written in C, they don't throw exceptions
themselves. On the other hand, you cannot inject C++
functionality into these functions which means that they cannot
throw an exception through user code either. That in turn means
that the C character and I/O functions actually have a no-throw
guarantee. That is, exception-safety is a non-issue with
respect to [most] C functions. The only exceptions are the
functions where you might pass in a C++ function, e.g.
'qsort()'. However, you don't want to call most of these anyway
(the one you might want to call is 'signal()' but registering a
function which throws is probably a pretty stupid idea).
Is the use of C-style character/stream operations in our C++ project
something I should worry about?


The C character functions are mostly unproblematic except that
you have to take care of passing only 'unsigned' integers to
them. This may or may not be automatically the case on your
system, depending whether 'char' is signed or unsigned. Of
course, if you want your code to be portable you need to cast
'char's before passing them to one of the ctype functions. The
only value with special treatment is 'EOF': this is typically a
negative value (usually '-1') and this value is treated
separately.

The primary problems with using C I/O functions:

- stdio is not extensible for I/O with user defined types.
- stdio is not extensible for new sources or destinations.
- stdio is not type-safe.
- You cannot register your own formatting routines with stdio.

The first three are, IMO, serious problems each of which making
stdio unsuitable for C++ projects. The forth issue depends on
your needs but is rarely a real issue.

There is, however, also a major issue with some imlementations
of IOStreams: a popular implementation at least had really bad
performance compared with stdio. Something like a factor of 10
was not that uncommon for typical file operations. This is
often a good reason to abandon IOStreams. However, AFAIK most
implementations now have comparable performance (but then, I
haven't made any measurements recently). That is, you might
want to verify that the performance of IOStreams is not
inferior to stdio performance. ~ou might want to accept a small
margin in return of the advantages but definitely no factor of
10.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #2
"Dietmar Kuehl" <di***********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Scott Brady Drummonds wrote:
I also have concerns about the operation of the C-style character operations
in the presence of exceptions. Given that they were written for C,

they
probably are NOT exception-safe, right?


Actually, it is quite likely that they *ARE* exception-safe:
Since they are written in C, they don't throw exceptions
themselves. On the other hand, you cannot inject C++
functionality into these functions which means that they cannot
throw an exception through user code either. That in turn means
that the C character and I/O functions actually have a no-throw
guarantee. That is, exception-safety is a non-issue with
respect to [most] C functions. The only exceptions are the
functions where you might pass in a C++ function, e.g.
'qsort()'. However, you don't want to call most of these anyway
(the one you might want to call is 'signal()' but registering a
function which throws is probably a pretty stupid idea).


On windows you would probably get an exception from any function that can
result in a memory error,
e.g. sprintf(0,"something funky %s",0); And since you do not know the
implementation of the C standardlib, some a function allocating memory, and
calling another function could be non exception safe.

But there are more function that are not thread safe. Several standard C
routines use "global" or static data ptr as result. Or modifies a global
state...

Is the use of C-style character/stream operations in our C++ project
something I should worry about?


The C character functions are mostly unproblematic except that
you have to take care of passing only 'unsigned' integers to
them. This may or may not be automatically the case on your
system, depending whether 'char' is signed or unsigned. Of
course, if you want your code to be portable you need to cast
'char's before passing them to one of the ctype functions. The
only value with special treatment is 'EOF': this is typically a
negative value (usually '-1') and this value is treated
separately.

The primary problems with using C I/O functions:

- stdio is not extensible for I/O with user defined types.
- stdio is not extensible for new sources or destinations.
- stdio is not type-safe.
- You cannot register your own formatting routines with stdio.

The first three are, IMO, serious problems each of which making
stdio unsuitable for C++ projects. The forth issue depends on
your needs but is rarely a real issue.

There is, however, also a major issue with some imlementations
of IOStreams: a popular implementation at least had really bad
performance compared with stdio. Something like a factor of 10
was not that uncommon for typical file operations. This is
often a good reason to abandon IOStreams. However, AFAIK most
implementations now have comparable performance (but then, I
haven't made any measurements recently). That is, you might
want to verify that the performance of IOStreams is not
inferior to stdio performance. ~ou might want to accept a small
margin in return of the advantages but definitely no factor of
10.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #3
Jesper Madsen wrote:
On windows you would probably get an exception from any function that can result in a memory error,
e.g. sprintf(0,"something funky %s",0);
This results in undefined behavior. Of course, undefined behavior is
bound to behave, well, undefined. This may include throwing exceptions,
of course. However, I assumed that the original question was about
well behaved code.
And since you do not know the
implementation of the C standardlib, some a function allocating memory, and calling another function could be non exception safe.


I claim otherwise: according to the C specification, none of the C
function throws an exception. That is quite easy to see: there is
no such concept as exceptions in C. Thus, any function from the
standard C library which throws an exception [when being used
correctly and not invoking undefined behavior] is broken! The only
exception are functions parameterized by function where the parameter
effectively throws an exception (e.g. the compare function passed to
'qsort()'). However, none of the character or I/O functions falls
into this class.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #4

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...
1
by: Klaus Ahrens | last post by:
if arrays were exception save (as i thought) the following should give 2 destructors for the partly constructed x2. unfortunately none of the compilers i tried (g++3.3.1, vc++.net, bcc32, icc)...
3
by: laurenq uantrell | last post by:
I am trying to install SQL Server 2000 on a Win2K OS machine but I get this error message: "A previous program installation created pending file operations on the installation machine. You must...
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...
3
by: benben | last post by:
I have always found Stroustrup's paper on generalized member function wrapper (http://www.research.att.com/~bs/wrapper.pdf) an interesting one. Recently I started to play with it. As I tried to put...
14
by: jehugaleahsa | last post by:
Hello: As an avid reader of C++ books, I know a lot of programmers out there are confronted with the challenge of exception safety. For those who don't know what it is, it is writing code in...
3
by: Aarti | last post by:
I was reading about exception safety in vectors and came across a staement that says The clear operation of vector guarantees a nothrow provided the copy and assignment operator do not throw an...
1
by: Vincent Jacques | last post by:
Hello all, I'm writing a class with value semantic, and I want its public interface to provide the strong exception safety guaranty (if a public operation fails for any reason, then an...
0
by: tabassumpatil | last post by:
Please send the c code for: 1.STACK OPERATIONS : Transfer the names stored in stack s1 to stack s2 and print the contents of stack s2. 2.QUEUE OPERATIONS : Write a program to implement...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.