qsort behavior with VC6 and VC7.1

Discussion in 'C++' started by chamois, Feb 27, 2007.

  1. chamois

    chamois Guest

    Hello,

    I am migrating a program from Visual C++ 6 to Visual C++ 7.1.
    When I run the exe file, the behavior is different because of the
    qsort function.
    Indeed I have some data which are identical for my comparison
    function, but they aren't sorted on the same way depending on the
    version of the compiler.

    It is not a disastrous problem but I would like to know if there is a
    way to have an identical behaviour.

    Regards,

    Laurent Pajou.
     
    chamois, Feb 27, 2007
    #1
    1. Advertising

  2. chamois

    Ismo Salonen Guest

    chamois wrote:
    > Hello,
    >
    > I am migrating a program from Visual C++ 6 to Visual C++ 7.1.
    > When I run the exe file, the behavior is different because of the
    > qsort function.
    > Indeed I have some data which are identical for my comparison
    > function, but they aren't sorted on the same way depending on the
    > version of the compiler.
    >
    > It is not a disastrous problem but I would like to know if there is a
    > way to have an identical behaviour.
    >
    > Regards,
    >
    > Laurent Pajou.
    >


    This is OT here, probably microsoft.public.vstudio.general is better.

    Anyway:

    What kind of data you are sorting ?
    Does the comparison routine utilize strict ordering rules
    ( A < B , B < C => A <C ) for every element ?

    The qsort may change order of those objects having equal values.

    ismo
     
    Ismo Salonen, Feb 27, 2007
    #2
    1. Advertising

  3. chamois

    peter koch Guest

    On Feb 27, 11:46 am, "chamois" <> wrote:
    > Hello,
    >
    > I am migrating a program from Visual C++ 6 to Visual C++ 7.1.
    > When I run the exe file, the behavior is different because of the
    > qsort function.
    > Indeed I have some data which are identical for my comparison
    > function, but they aren't sorted on the same way depending on the
    > version of the compiler.

    This should not be a surprise to you. qsort does not specify an order
    for elements that compare equal.

    >
    > It is not a disastrous problem but I would like to know if there is a
    > way to have an identical behaviour.


    Probably not if you insist on using qsort. But since you are in a C++
    forum, I should mention that std::stable_sort gives a "deterministic"
    sort every time. It will likely differ from your current qsort result,
    but the result on the two platforms will be identical.
    You really should give up qsort, by the way. It is only useful for POD
    and it is likely quite a bit slower than std::sort.

    /Peter
     
    peter koch, Feb 27, 2007
    #3
  4. chamois

    chamois Guest

    On 27 fév, 12:43, Ismo Salonen <> wrote:
    >
    > This is OT here, probably microsoft.public.vstudio.general is better.
    >
    > Anyway:
    >
    > What kind of data you are sorting ?
    > Does the comparison routine utilize strict ordering rules
    > ( A < B , B < C => A <C ) for every element ?
    >
    > The qsort may change order of those objects having equal values.
    >
    > ismo


    Ismo,

    I sort pointers on objects and the rule ( A < B , B < C => A <C ) is
    respected.
    I think I will use std::stable_sort that Peter suggested.

    Thansks,

    Laurent.
     
    chamois, Feb 27, 2007
    #4
  5. chamois

    Peter Guest

    "Ismo Salonen" <> wrote in message
    news:SPUEh.119$...
    > chamois wrote:
    > This is OT here, probably microsoft.public.vstudio.general is better.



    just ignore such people -- this is nobodies private news group
     
    Peter, Mar 2, 2007
    #5
  6. chamois

    Peter Guest

    "chamois" <> wrote in message
    news:...
    > Hello,
    >
    > I am migrating a program from Visual C++ 6 to Visual C++ 7.1.
    > When I run the exe file, the behavior is different because of the
    > qsort function.
    > Indeed I have some data which are identical for my comparison
    > function, but they aren't sorted on the same way depending on the
    > version of the compiler.
    >
    > It is not a disastrous problem but I would like to know if there is a
    > way to have an identical behaviour.



    implement your own sort or use one with deterministic behaviour.
    There are other implementations of sort
    -- some promise to keep to identical objects in the same order as far as I
    remember.
     
    Peter, Mar 2, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Moonlit
    Replies:
    12
    Views:
    689
    Jonathan Mcdougall
    Oct 8, 2003
  2. sheep
    Replies:
    3
    Views:
    1,265
    Jiangsheng[Microsoft MVP of VC++]
    Aug 4, 2004
  3. 3doutpost
    Replies:
    2
    Views:
    1,197
    Jonathan Turkanis
    Dec 3, 2004
  4. Ross2772

    VC7 and hash_map

    Ross2772, Aug 9, 2006, in forum: C Programming
    Replies:
    0
    Views:
    468
    Ross2772
    Aug 9, 2006
  5. Replies:
    1
    Views:
    310
    =?ISO-8859-1?Q?Stefan_N=E4we?=
    Sep 2, 2005
Loading...

Share This Page