Home > Mobile >  I'm getting a SIGSEV signal when running a program on HackerRank
I'm getting a SIGSEV signal when running a program on HackerRank

Time:06-17

I had made a post yesterday but I scrapped it and approached it with C instead of Java.

I tested the code on the compiler installed on my computer and it ran fine. When I run it on HackerRank, it keeps giving me Segmentation fault. Please find the code and compiler output below.

I read a few posts that mentioned it can happen due to illegal memory access, but I don't see where I did so. I have spent over 5 hours already and will be coming back to this in a while. Any help is appreciated.

struct Date
{
    int Day;
    int Year;
    int Month;
     
    bool latest(Date d){
        if (Year > d.Year){
            return true;
        }
        else if (Year == d.Year)
        {
            if(Month > d.Month){
                return true;
            }
            
            else if (Month == d.Month){
                if(Day> d.Day){
                    return true;
                }
            }
        }
    }
};

Date ThirdLatest(std::vector<Date> &dates) {
    vector<Date> d;
    int length = dates.size();
    
    //std::cout << std::unitbuf;
    
    for (int i=0; i<length; i  ){
        int flag = 0;
        
        for (int j=0; j<d.size(); j  ){
            if (dates[i].Day == d[j].Day && dates[i].Month == d[j].Month && dates[i].Year == d[j].Year){
                flag = 1;
                break;
            }
        }
        
        if (flag ==1)
            d.push_back(dates[i]);
    }
    
    Date temp;
    
    for (int i=0; i<d.size(); i  ){
        for (int j=i 1; j<d.size(); j  ){
            if (!d[i].latest(d[j])){
                temp.Day = d[i].Day;
                temp.Month = d[i].Month;
                temp.Year = d[i].Year;
                
                d[i].Day = d[j].Day;
                d[i].Month = d[j].Month;
                d[i].Year = d[j].Year;
                
                d[j].Day = temp.Day;
                d[j].Month = temp.Month;
                d[j].Year = temp.Year;
            }
        }
    }
    
    return d[2];

}
int main() {
    int numberOfEntries;
    int res = scanf("%d\n", &numberOfEntries);
    std::vector<Date> dates;
    for (int i = 0; i < numberOfEntries;   i)
    {
        Date date;
        res = scanf("%d-%d-%d", &date.Day, &date.Month, &date.Year);
        dates.push_back(date);
    }    
    
    Date result = ThirdLatest(dates);
    printf("d-d-%d\n", result.Day, result.Month, result.Year);
    return 0;
}

Compiler output

Reading symbols from Solution...done.
[New LWP 71078]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./Solution'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  ThirdLatest (dates=...) at Solution.cpp:57
57      for (int i=0; i<length; i  ){
To enable execution of this file add
    add-auto-load-safe-path /usr/local/lib64/libstdc  .so.6.0.25-gdb.py
line to your configuration file "//.gdbinit".
To completely disable this security protection add
    set auto-load safe-path /
line to your configuration file "//.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
    info "(gdb)Auto-loading safe path"

Sample input:

7
14-04-2001
29-12-2061
21-10-2019
07-01-1973
19-07-2014
11-03-1992
21-10-2019

Expected output: 19-07-2014

CodePudding user response:

The error is here

if (flag ==1)
        d.push_back(dates[i]);

It should be

if (flag == 0)
        d.push_back(dates[i]);

You got your logic wrong. You are trying to avoid adding duplicates to your vector but you ended up adding only duplicates which means that nothing gets added to the d vector. Then this line

return d[2];

results in an illegal access error.

Plus this function is missing a return statement

bool latest(Date d){
    if (Year > d.Year){
        return true;
    }
    else if (Year == d.Year)
    {
        ...
    }
    return false; // this line is necessary
}
  • Related