What does this error code mean? exit code -1073741819 (0xC0000005)

I am trying to recursively sort by job title. Job Title is declared in the Employee class. Whenever I attempt to recursively sort the stack, the popping is done for each stack data member, but the compiler throws this error soon after. I am curious about what this error means as I have done research but it does not seem to be a definite cause.

Note: The program begins in main by getting the employee data and has a few options as to what can be done with the data. In this entering 5 in the main function would sort the data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  #include <iostream>

employees.h

class Employee{
public:
    void storedata(){
        std::cout<<"\nPlease enter first name: ";
        std::cin>>first_name;
        std::cout<<"\nPlease enter last name: ";
        std::cin>>last_name;
        std::cout<<"\nPlease enter job title: ";
        std::cin>>job_title;
    }
    void getdata(){
        std::cout<<"\nFirst name: "<<first_name<<"\nLast name: "<<last_name;
        std::cout<<"\nJob Title: "<<job_title;
    }
    char job_title[50];
private:
    char first_name[20];
    char last_name[20];

};

struct Node{
    Employee worker;
    Node *next;
};


class Stack{
private:
    Node *Top;
public:
    Stack(){
        Top=NULL;
    }
    void Push(Employee w);
    void Pop();
    void disp();
    void return_top();
    void Insert_s(Stack & s, Employee j);
    void Sort (Stack & s);

};
end header


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
header_definitions.cpp

void Stack::disp() {
    Node *temp =Top;

    while(temp!=NULL){
        temp->worker.getdata();
        temp =temp->next;
    }
}

void Stack::Push(Employee w) {
    Node *temp;
    temp = new Node;
    temp->worker=w;
    temp->next =Top;
    Top = temp;
}
void Stack::Pop() {
    if(Top !=NULL){
        Node *temp =Top;
        Top = Top->next;
        std::cout<<"\nThe name being popped: ";
        temp->worker.getdata();
        delete temp;
    }
    else
        std::cout<<"\nThe Stack is empty...";
}
void Stack:: Insert_s(Stack & s, Employee j){
    Node *temp =Top;
    if(Top== NULL||Top->worker.job_title> j.job_title){
        s.Push(j);
    }
    else{
        Employee x;
        x = temp->worker;
        Insert_s(s,j);
        s.Push(x);
    }
}

void Stack::Sort (Stack & s){
    Employee j;
    Node *temp= Top;
    j = temp->worker;
    std::cout<<"\nNew data assigned to j: ";
    j.job_title;
    if (Top!=NULL){
        s.Pop();
        Sort(s);
        Insert_s(s,j);
    }
    if(Top==NULL){
        std::cout<<"\nfailing at NULL";
    }
}
void Stack:: return_top(){
    std::cout<<"\nThe employee at the top of the list is: ";
    Top->worker.getdata();

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
main.cpp

#include <iostream>
#include <iomanip>
#include "employees.h"
using namespace std;
void show_menu();
int find_search_pos();
int main() {
    Employee record[10],temp;
    int num_of_employees;
    Stack database;
    int pos = 0,choice =-1;

    while (choice !=7) {
        show_menu();
        cin >> choice;
        switch (choice) {

            case 1:
                cout<<"\nHow many employees would you like to enter data for: ";
                cin>>num_of_employees;

                for(int i = 0; i< num_of_employees; i++) {
                    cout<<"\nEnter data for employee number "<<pos+1<<".\n";
                    temp.storedata();
                    record[pos] = temp;
                    database.Push(record[pos]);//store 1 of 10 employees
                    pos++;
                }
                break;
            case 3:
                cout<<"\nEnter data for employee number "<<pos+1<<".\n";
                temp.storedata();
                record[pos] = temp;
                database.Push(record[pos]);//store 1 of 10 employees
                pos++;
                break;
            case 4:
                cout<<"\nRemoving employee...";
                database.return_top();
                database.Pop();
                break;
            case 5:
                cout<<"\nSorting List: ";
                database.Sort(database);

            case 6:cout<<"\n\ndisplay data: ";
            database.disp();
            break;


        }
    }






Last edited on
0xC0000005 is windows telling you that your program tried to access memory that it doesn't own.
Typically, this is either a NULL pointer or a pointer to some memory you used to have.

What you need to do now is learn all about the debugger that comes with your development environment.

Like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
$ g++ -g foo.cpp
$ gdb -q ./a.out 
Reading symbols from ./a.out...done.
(gdb) run
Starting program: ./a.out 
1

How many employees would you like to enter data for: 2

Enter data for employee number 1.

Please enter first name: Fred 

Please enter last name: Flintstone

Please enter job title: Breaker

Enter data for employee number 2.

Please enter first name: Barney

Please enter last name: Rubble

Please enter job title: Mover
5

Sorting List: 
New data assigned to j: 
The name being popped: 
First name: Barney
Last name: Rubble
Job Title: Mover
New data assigned to j: 
The name being popped: 
First name: Fred
Last name: Flintstone

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400d92 in Stack::Sort (this=0x7fffffffda50, s=...) at foo.cpp:90
90	    j = temp->worker;
(gdb) bt
#0  0x0000000000400d92 in Stack::Sort (this=0x7fffffffda50, s=...) at foo.cpp:90
#1  0x0000000000400e3b in Stack::Sort (this=0x7fffffffda50, s=...) at foo.cpp:95
#2  0x0000000000400e3b in Stack::Sort (this=0x7fffffffda50, s=...) at foo.cpp:95
#3  0x0000000000401321 in main () at foo.cpp:146
(gdb) p temp
$1 = (Node *) 0x0
(gdb) list
85	}
86	
87	void Stack::Sort (Stack & s){
88	    Employee j;
89	    Node *temp= Top;
90	    j = temp->worker;
91	    std::cout<<"\nNew data assigned to j: ";
92	    j.job_title;
93	    if (Top!=NULL){
94	        s.Pop();

In short, temp is a NULL pointer and you didn't check it.

A GUI based debugger works in more or less the same way, and will give you the same information.
But for example, when it stops, it will be pointing you directly to the line of code causing the problem.
Thank you for that! I actually am researching how to use the debugger tool in my ide.
any suggestions on how I could get rid of this issue? I did some research on the topic and the error the compiler was throwing.
Check for null before using a pointer. Top is null in this case.
Topic archived. No new replies allowed.