CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Threaded View

  1. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: STL issues with sort()?

    Quote Originally Posted by Radius
    Ya, I've been thinking about that. I don't use this across threads or anything, but maybe something totally unrelated has ripped into the memory, I am putting some bounds checking software on here and seeing what that points to.
    1) Since this is C++, the use of "typedef struct" is no longer necessary. The "typedef struct" is only necessary for 'C' programs.
    Code:
    struct SortedList
    {
      ///...
    };
    2) Looking at your member variables, there is no need for a user-defined assignment operator. If the member variables of your struct are safely copyable by themselves, adding an unnecessary user-defined assignment operator will just lead to bugs.

    Speaking of that, you didn't code it correctly, and you are missing a user-defined copy constructor to match the assignment operator. Recommendations: Get rid of the user-defined assignment operator, unless you can show us that the members are not safely copyable.

    3) This is incorrect
    Code:
       inline tagSORTED_LIST &operator = (tagSORTED_LIST _v)
       {
          if ( this != &_v )
             memcpy(this, &_v, sizeof(tagSORTED_LIST));
    
          return *this;
       }
    Since your struct is non-POD, using memcpy to copy from one instance to another is undefined behaviour. Never use raw C functions to copy non-POD types. Again, get rid of this altogether, and let the C++ generate the standard assignment operator.

    4) When using std::sort() it requires that the function used to see if a > b be of a strict weak ordering. This means that if a > b, then b < a for all pairs of values. In other words, if your code determines that a > b for values a and b, and for some unforseen reason determines that b > a when given the same pair of values in another iteration of the sort, then the sort will not work.

    This can be caused by a badly coded sort predicate, or a sort predicate that just won't uniquely determine the correct order of a and b (even though the coder believes it will always correctly and uniquely determines the order of two values). It is hard to tell if your function adheres to this.

    5) The std::sort() predicate must check for a < b and return true (for descending sort), or if a > b and return true (for ascending sort). Otherwise return false. Do not return true if a == b. This violates the strict weak ordering rule.

    In general, it looks like a C programmers valiant attempt at C++. The flaws I pointed out earlier should be addressed, and then try the sort again.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 6th, 2007 at 04:24 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured