Thread: exception from C function?

  1. #31
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi Mats,


    I little confused even if I agree with you. Looks like we are talking about template function will degrade performance compared with pure C implementation of qsort(), but you are talking template function could improve performance by inline some code like compare for POD types?

    Quote Originally Posted by matsp View Post
    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

    regards,
    George

  2. #32
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Calling a function is always overhead compared to just doing the same work "here". The amount of overhead is depending on the amount of work in the function. Since qsort() may do A LOT of calls to the compare function, and comparing two data-items is USUALLY trivial, the overhead can be quite noticeable.

    qsort itself may be inlined, but the comparison will ALWAYS be called through a function pointer.

    --
    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.

  3. #33
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by George2 View Post
    Hi Mats,


    I little confused even if I agree with you. Looks like we are talking about template function will degrade performance compared with pure C implementation of qsort(), but you are talking template function could improve performance by inline some code like compare for POD types?




    regards,
    George
    Template functions will normally be faster than qsort - that is the whole point of the last 7 or so posts.

    --
    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.

  4. #34
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by George2 View Post
    Hi Mats,

    I little confused even if I agree with you. Looks like we are talking about template function will degrade performance compared with pure C implementation of qsort(), but you are talking template function could improve performance by inline some code like compare for POD types?

    regards,
    George
    A template function will never degrade performance just because it's a template function. It can increase the size of the executable, but that's all.
    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.

  5. #35
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    Let me recap your point in one sentence from the several posts. Your point is, because template function uses inline technique (e.g. inline compare function), it could be faster than qsort(), right?

    Quote Originally Posted by matsp View Post
    Template functions will normally be faster than qsort - that is the whole point of the last 7 or so posts.

    --
    Mats

    regards,
    George

  6. #36
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by George2 View Post
    Let me recap your point in one sentence from the several posts. Your point is, because template function uses inline technique (e.g. inline compare function), it could be faster than qsort(), right?
    Correction: can be inlined. It's up to the compiler in the end whether or not it inlines it.
    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.

  7. #37
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    Your supplemental information is useful.

    After discussing with you and have some self-study, the original point of this question still makes me confused -- but not as much as before, there are mainly two points about throwing exception from C++ into C caller code, and such code is not uncommon since a part of STL uses C implementation CRT.

    1. Undefined behavior since C know nothing about C++ exception;

    2. Defined behavior, and object on C stack will be released but other resources are not. C++ exception is pased through C code into higher memory address to look for exception handler.

    Which option do you agree?

    Quote Originally Posted by Elysia View Post
    Correction: can be inlined. It's up to the compiler in the end whether or not it inlines it.

    regards,
    George

  8. #38
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    1. Undefined.
    Note that 2) may still happen, because it's undefined. Anything could happen. Don't rely on it, however.
    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.

  9. #39
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks for clarification, Elysia!


    But I think there are lots of code nowadays invoking C function like qsort and provide throwable C++ callback code, agree? :-)

    Quote Originally Posted by Elysia View Post
    1. Undefined.

    regards,
    George

  10. #40
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Haven't used qsort, so dunno.
    Just make sure to catch those exceptions in the callback code,
    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.

  11. #41
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks for your advice, Elysia!


    Quote Originally Posted by Elysia View Post
    Haven't used qsort, so dunno.
    Just make sure to catch those exceptions in the callback code,

    regards,
    George

  12. #42
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by George2 View Post
    Thanks for clarification, Elysia!


    But I think there are lots of code nowadays invoking C function like qsort and provide throwable C++ callback code, agree? :-)




    regards,
    George
    I doubt that more than 0.01% of the code calling qsort uses code that MAY throw - why would you ever throw in a function that essentially does the same as "operator>"? It is conceivable to come up with a function that DOES throw an exception, but it would be bad practice, and what are the circumstances where you expect this to be the case?

    Comparing two existing objects should not throw an exception!

    --
    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.

  13. #43
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    Beyond and comparison operator, do you think there are any other possibilities to throw in callback provided to qsort()?

    Quote Originally Posted by matsp View Post
    I doubt that more than 0.01% of the code calling qsort uses code that MAY throw - why would you ever throw in a function that essentially does the same as "operator>"? It is conceivable to come up with a function that DOES throw an exception, but it would be bad practice, and what are the circumstances where you expect this to be the case?

    Comparing two existing objects should not throw an exception!

    --
    Mats

    regards,
    George

  14. #44
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    What crazy thing could the callback do that it would throw?
    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

  15. #45
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The callback from qsort should only compare the input parameters and return "equal, greater or lesser" - why don't you spend a couple of hours implementing a qsort on an object, to see how it works?

    --
    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.

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