Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

Memory usage problem when trying to sort a struct list in C

I am trying to sort a struct list in C and i get a 0xC0000005 problem. This is the struct i am using:

typedef struct Aqi Aqi;
struct Aqi{
    int date; // format: yyyymmdd (20200101) - 1st Jan 2020
    int time; // Either 9 , 12 or 19
    int l1; // l stands for location
    int l2;
    int l3;
    Aqi* next;
};

The list is presorted by date and there are three elements for each date (For each given time). So i am using this function to sort the list by times in ascending order:

Aqi* sort(Aqi* root){ // I am passing the root of the list
    int date;
    int tempInt;
    int c = 0;
    int min;

    Aqi* temp = root;
    Aqi* temp2 = NULL;

    while(temp != NULL){
        if(c == 3){ // Counter c is being used to restart values on every 3rd iteration because
            c = 0; // that's when the new date starts.
        }
        if(c == 0){
            date = temp->date;
            temp2 = temp;
            min = temp2->time;
        }
        while(temp2->date == date){
            if(temp2->time < min){
                min = temp2->time;

                tempInt = temp->time;
                temp1->time = min;
                temp3->time = tempInt;

                tempInt = temp->l1;
                temp->l1 = temp2->l1;
                temp2->l1 = tempInt;

                tempInt = temp->l2;
                temp->l2 = temp2->l2;
                temp2->l2 = tempInt;

                tempInt = temp->l3;
                temp->l3 = temp2->l3;
                temp2->l3 = tempInt;
            }

            temp2 = temp2->next;
        }
        ++c;

        temp = temp->next;
    }
    return root;
}

The list itself is fine (I can print it with no problem) but the program is having trouble with this function specifically. Why is it giving me an error?

question from:https://stackoverflow.com/questions/65829288/memory-usage-problem-when-trying-to-sort-a-struct-list-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...