Thread: exception from C function?

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    On a side note, Stroustrup is wrong about atexit(): since it only registers handlers and doesn't call them, it can't throw. The function that might throw is exit().
    Have you considered sending him an email to make the correction in an errata and/or future edition?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Hmm, perhaps I should. But I should check the errata first. Maybe it's already there.

    Edit: OK, the errata don't list anything. But I noticed that my 15th print is already behind the latest version, the 16th print.

    George, which print do you have? (It's on the very bottom of the legal info page.)
    Last edited by CornedBee; 01-28-2008 at 11:12 AM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi CornedBee,


    Quote Originally Posted by CornedBee View Post
    The actual rule is simple: catch all C++ exceptions at the end of a callback and use the callback's error reporting mechanism (typically a return code) to report the error. The callback doesn't have such a mechanism? (qsort's callback for example) Then there probably shouldn't be any errors. (How hard can comparing two objects be?)
    How could you do this if we are not allowed to modify the code of the callback function -- supposing it is from some 3rd party code? :-)

    I think the issue mentioned by Bjarne is common nowadays, and you know (1) invoking qsort is common and (2) compare function is common to throw exception, and from your analysis, undefined behavior is so often. :-)


    regards,
    George

  4. #19
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    I agree CornedBee -- aftre reading MSDN document about atexit()!


    I suggest you send him an email to let him correct his in next version. :-)

    Quote Originally Posted by CornedBee View Post
    On a side note, Stroustrup is wrong about atexit(): since it only registers handlers and doesn't call them, it can't throw. The function that might throw is exit().

    regards,
    George

  5. #20
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi CornedBee,


    What do you mean print? The "Show Printable Version" at the bottom of the page?

    Quote Originally Posted by CornedBee View Post
    Hmm, perhaps I should. But I should check the errata first. Maybe it's already there.

    Edit: OK, the errata don't list anything. But I noticed that my 15th print is already behind the latest version, the 16th print.

    George, which print do you have? (It's on the very bottom of the legal info page.)

    regards,
    George

  6. #21
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    For heaven's sake, learn to multiquote!

    1) I'd write a wrapper for the 3rd party function that catches all exceptions.
    2) Invoking qsort in C++ code shouldn't be common. std::sort is way better.
    3) A compare function should not commonly throw exceptions.
    4) No! Presumably, since you're quoting the book a lot, you have it, right? The book is reprinted whenever the copies run out, so there's the first print, the second print, and so on. Each new print has some minor corrections incorporated. My copy is the 15th print, but the newest is the 16th. I just want to know if you have this newest print, because then your quote would indicate that the problem hasn't been fixed.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #22
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    1.

    Quote Originally Posted by CornedBee View Post
    2) Invoking qsort in C++ code shouldn't be common. std::sort is way better.
    Why std::sort is faster than qsort?

    2.

    Quote Originally Posted by CornedBee View Post
    4) No! Presumably, since you're quoting the book a lot, you have it, right? The book is reprinted whenever the copies run out, so there's the first print, the second print, and so on. Each new print has some minor corrections incorporated. My copy is the 15th print, but the newest is the 16th. I just want to know if you have this newest print, because then your quote would indicate that the problem hasn't been fixed.
    I am using The C++ Programming Language Special Edition 4th print -- pretty old. :-)


    regards,
    George

  8. #23
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Yeah, but the issue is that atexit() is the means by which an exception can be made to occur when a program exits (by registering a function that throws).

  9. #24
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, the issue is that the compiler can make assumptions about exceptions being thrown if it knows which calls might throw exceptions. And any C function that doesn't call a C++ callback cannot throw exceptions. This includes atexit(), because although a callback is passed to it, it doesn't call it.

    qsort is a C function, compiled into object code as it is. The callback is called through a function pointer.
    sort is a C++ function template, compiled into object code whenever instantiated. The callback can be a function object, allowing inlining of the comparison, thus greatly speeding up the execution.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #25
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    1. What means "compiled into object code whenever instantiated"?

    2. Why (1) is faster than function callback implemented by function pointer?

    Quote Originally Posted by CornedBee View Post
    qsort is a C function, compiled into object code as it is. The callback is called through a function pointer.
    sort is a C++ function template, compiled into object code whenever instantiated. The callback can be a function object, allowing inlining of the comparison, thus greatly speeding up the execution.

    regards,
    George

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by George2 View Post
    1. What means "compiled into object code whenever instantiated"?
    You don't know how templates works?
    Because of their template-specific nature that they can use any type, the compiler only generates code when a call to the template function is made. You could save size, for one thing.

    2. Why (1) is faster than function callback implemented by function pointer?
    Inline! Hello?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #27
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    I agree with your below points. But if I understand correctly, generating only the type needed is used to save footprint to that no need to generate all possible types...

    But how could it improve performance?

    Quote Originally Posted by Elysia View Post
    You don't know how templates works?
    Because of their template-specific nature that they can use any type, the compiler only generates code when a call to the template function is made. You could save size, for one thing.


    Inline! Hello?

    regards,
    George

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by George2 View Post
    But how could it improve performance?
    How about avoiding function calls + overhead?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #29
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by George2 View Post
    Thanks Elysia,


    I agree with your below points. But if I understand correctly, generating only the type needed is used to save footprint to that no need to generate all possible types...

    But how could it improve performance?
    By inlining the compare operation - the templated sort function just uses the < operator, so if it's an integer compare, it will just do that. If it's an inlinable operator< in the class object, then the compiler can inline it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #30
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,

    1.

    You mean the performance of template function invocation is because there is additional stack operation push/pop parameters, for example?

    If true, I do not think it is a special point which template function brings to us, all function call has such so-called "performance degration". :-)

    Have I understood your correctly?

    2.

    qsort() is a inline function?

    Quote Originally Posted by Elysia View Post
    How about avoiding function calls + overhead?

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM