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

From: Jesper Madsen (balls_at_mail.stofanet.dk)
Date: 01/18/05


Date: Tue, 18 Jan 2005 18:44:44 +0100


"Dietmar Kuehl" <dietmar_kuehl@yahoo.com> wrote in message
news:1106049907.256946.293630@z14g2000cwz.googlegroups.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:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
> <http://www.contendix.com> - Software Development & Consulting
>



Relevant Pages

  • Re: FILESYS exception with hive registry in 4.2
    ... It appears that urlmon is passing -1 to RegCloseKey. ... registry code in filesys.exe, bad parameters are caught ... this exception is handled inside filesys.exe ...
    (microsoft.public.windowsce.platbuilder)
  • RE: Getting "Unhandled exception" error when running in IDE
    ... compare zero characters and see if the exception still occurs. ... away on the first or second character then make sure you are passing in the ... Like I said I have never used this string class so you know more than I do. ... > unhandled exception is occurring in a system header file associated with STL ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Socket.Send crashes phone
    ... Sorry, but the socket doesn't know what type of file you are sending, so ... binary files (images, video). ... - Catching the exception is impossible since the exception crashes the ... - I'm indeed passing a byteto Socket.Send ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: python 2.5 scripting in vim on windows: subprocess problem
    ... guess is that the handles that you are passing are not valid. ... try creating explicit handles that are valid (such as for ... Popen object, or when you try to read stdout? ... It throws exception on subprocess.Popen object instantiation. ...
    (comp.lang.python)