0

I got a strange error while compiling this code. Its the same as in the book im studying from, but it seems it gets confused somewhere, because my output is off, ages and names got mixed eg( darko 4, zdendk 3, matija 2, draga(dont know where the "n" is gone) 1) and it says invalid pointer. Help me to understand why this is happening.

#include <iostream>
#include <math.h>
#include <cstdlib>

//#include "Tablica.h"

using namespace std;

/*
 * 
 */

struct Person {

    string name;
    int age;

};

int compare (const void* a, const void* b){

  const Person* p1 = static_cast<const Person*>(a);
  const Person* p2 = static_cast<const Person*>(b);

  if(p1->age != p2->age)
  return p2->age - p1->age;

  return p1->name.compare(p2->name); 


}


int main(int argc, char** argv) {

    Person people[]{{"darko", 1},{"zdendka", 2},{"matija", 3},{"dragan", 4}};

    int numP = sizeof(people) / sizeof(Person);
    qsort(people, numP, sizeof(Person), compare);

    for(Person x:people){

       cout<< x.name <<" "<<x.age<<endl;
    }
    return 0;
}
8
  • 9
    qsort doesn't work properly with std::string, try std::sort instead. Commented Feb 15, 2016 at 14:30
  • 2
    The qsort() function uses byte-by-byte copying to move data. That may not work properly for strings. Commented Feb 15, 2016 at 14:32
  • 3
    "Its the same as in the book im studying from, but it seems it gets confused somewhere" Does your book recommend overlooking C++'s rich set of algorithms tailored to its own containers - and then passing said containers to C algorithms that don't know how to deal with them? If so, please, get a different book. Commented Feb 15, 2016 at 14:38
  • 2
    Which book is it? We need to compile a list of books to avoid. Commented Feb 15, 2016 at 14:52
  • 1
    'so what is incorect': It's a bizarre mishmash of C++ (string, iostreams), C++11 (range based for and initializer lists) and C functions (qsort). Use C++ rich library of functions, classes and templates. So std::array or std::vector rather than the C-array you are using and std::sort instead of qsort and you should be pretty much there. Commented Feb 15, 2016 at 15:08

1 Answer 1

2

Compiling this program with VC++, the output is

dragan 4
matija 3
zdendka 2
darko 1

which makes perfectly sense, since the lines

 if(p1->age != p2->age)
  return p2->age - p1->age; 

always kick in (the age is always different), making the array be sorted according to the ages not the names.

As you told by the comments, it is the best to use real C++ techniques like using std::array and std::sort, not supprisingly the code will be smaller, and will run much faster.

The Code, the C++ way:

class Person {

private:
    string name;
    int age;

public:
    Person() = default;
    Person(const string& name, unsigned int age):
      name(name), age(age) {}
    Person(const Person& rhs) = default;
    Person(Person&& rhs) = default;

    string getName() const {return name;}
    unsigned int getAge() const {return age;}
};

int main(){
   array<Person,4> people{{"darko", 1},{"zdendka", 2},{"matija", 3},{"dragan", 4}};
   sort(people.begin(),people.end(),[](auto a, auto b){  
      return a.getName() < b.getName();
   });

   for (const auto& person : people){
       std::cout<< person.getName() <<" , " << person.getAge()<<"\n";
   } 
   return 0;
}
Sign up to request clarification or add additional context in comments.

10 Comments

On systems using the short string optimization qsort just happens to be able to sort short strings (by chance, and as one possible outcome of UB).
#include <algorithm>
cant still find array, and cant deduce && auto...nvm i will skip this troubled code =/
The first part of the answer just encourages usage of qsort when it should be avoided like the plague for the OP's situation.
@DraganItmSmoljan What compiler and settings are you using?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.