cli_ninja
cli_ninja

Reputation: 21

Debug assertion failed in Visual Studio but runs OK on online IDE

My code runs fine when I run it on online IDEs but fails with debug assertion error when run on visual studio. The error is probably in the rand() function because when I specifically use random integer values, it works just fine. I don't understand why is it not running on VS.

The following message is being displayed:

Debug Assertion Failed!
Program: [Path] 
Line: 1639
Expression : sequence not ordered

Also I'd like to ask how to debug these errors as they are not specifying a particular code instance.

#include <iostream>
#include <list>
#include <cstdlib>          //for rand() function
using namespace std;
void display(list <int> &lst)
{
    list <int> ::iterator p;
    for (p = lst.begin(); p != lst.end(); p++)
    {
        cout << *p << " ";
    }
    cout << "\n\n";
}
int main()
{
    list <int> list1;           //empty list of zero length
    list <int> list2(5);        //empty list of size 5
    for (int i = 0; i < 3; i++)
    {
        list1.push_back(rand() % 100);
    }

    list <int> ::iterator p;

    for (p = list2.begin(); p != list2.end(); p++)
    {
        *p = (rand() % 100);
    }

    cout << "List1:  ";
    display(list1);
    cout << endl << endl;
    cout << "List2:  ";
    display(list2);
    cout << endl << endl;

    //Add two elements at the ends of list 1
    list1.push_back(200);
    list1.push_front(100);

    //Remove an element at the front of list 2
    list2.pop_front();

    cout << "Now list1:  ";
    display(list1);
    cout << "\n\n";
    cout << "Now list2:  ";
    display(list2);
    cout << "\n\n";

    list<int> listA, listB;
    listA = list1;
    listB = list2;

    //Merging two lists(unsorted)

    list1.merge(list2);
    cout << "Merged unsorted lists\n";
    display(list1);
    cout << "\n\n";

    //Sorting and merging
    listA.sort();
    listB.sort();
    listA.merge(listB);

    cout << "Merged sorted lists\n";
    display(listA);
    cout << "\n\n";

    //Reversing a list
    listA.reverse();
    cout << "Reversed merged list: \n";
    display(listA);

    return 0;
}

enter image description here

Upvotes: 0

Views: 243

Answers (2)

Marek R
Marek R

Reputation: 37917

Documentation of list::merge says:

Exception safety
If the allocators in both containers do not compare equal, if comp does not define a strict weak ordering, or if the container elements are not ordered according to it, it causes undefined behavior.
Otherwise, if an exception is thrown by a comparison, the container is left in a valid state (basic guarantee). Otherwise, if an exception is thrown, there are no changes in the container (strong guarantee).

So depending on compiler implementation behavior can be different when merging unsorted lists. Apparently Visual Studio does extra checking in debug mode which is not required (but allowed) by C++ standard.

Upvotes: 2

Jarra McIntyre
Jarra McIntyre

Reputation: 1275

merge requires that the lists be sorted (see: https://en.cppreference.com/w/cpp/container/list/merge). In your first call to merge the lists are not sorted (the lists are only sorted prior to your second call).

In debug mode, the Microsoft C++ standard libraries include extra runtime checks to enforce invariants such as this. The "Online IDE" likely uses GCC with libstdc++ or similar which generally does not include the same level of runtime checking that the Microsoft libraries do, and therefore does not catch the mistake.

Upvotes: 2

Related Questions