Double link list question

Hi guys, I have a small problem with double link list opertion. The detailed program is in below(not the full version, only the two functions with problem). These two function work well when they are compile in single cpp file. However, when I want to seperate the function in two cpp file, it will cause error and the exe window will soon exit. Can anyone figure out why? Thank you in advance :)
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
//main.cpp
int main(){
        cout << "Creating a new head..." << endl;
        TrainCar* Head= addHead();
        printList(Head);
        cout << endl;
return 0;
}

//addHead() function is in List_operation.cpp
ListNode* addHead()
{
    ListNode* Head = new ListNode;
    Head->data = 10 ;
    Head->prev = nullptr; 
    Head->next = nullptr;
    return Head;
}

//printList fucntion is in print_List.cpp
void printList(const ListNode* head) 
{
    if(head == nullptr)
    {
        cout << "No node!" << endl;
        return;
    }

    while(true)
    {
        cout << "[";
        cout << head->data;
        cout << "]";
        if(head->next)
            cout << " -> ";
        else
            break;
        head = head->next;
    } 

    //head now actually points to the last/tail node after the previous loop
    cout << "In reverse: " << endl;
    while(head) 
    {
        cout << "[";
        cout << head->data;
        cout << "]";
        if(head->prev)
            cout << " <- ";
        else
            break;
        head = head->prev;
    } 
    cout << endl;
}

//two function will be use in main.cpp and linked by a .h file 
Last edited on
At a glance, this is not enough code to help us diagnose the issue. Show us a minimal but complete example of the code that doesn't run correctly.

How do you know that the crash happens on line 6?
(*Head).prev = nullptr; //this cause exe crash,
Assigning a pointer to be null does not cause a crash. Unless you already had undefined behavior before that point, and it just so happened to manifest at that point..

Edit: I do see one issue, although it wouldn't cause a crash if the data itself is correct. You change your 'head' variable up to the point where it is assigned nullptr (the last time line 29 is called). At this point, there is no way that the loop on line 34 will ever be called. You need to save the last valid node pointer, and then go in the reverse direction.
Last edited on
it would seem to me that Head is null, making *head or head-> a problem.
how it got to be null there is what you need to debug.
What is the constructor for ListNode?

Have you defined a copy constructor? Is there a destructor?

What is the class definition?

1
2
3
4
5
6
(*Head).data  = 10;

is just:

[code]
Head->data = 10;


If your constructor and default initialisations for ListNode are as required, then addhead() becomes simply:

1
2
auto addHead() {
    return new ListNode(10);

It shows that the error is 139(SIGSEGV) and I search on the internet this equates to a read or write of a memory address that's not mapped in the process. I do not see where this error come from.
Last edited on
As a supplement, although it shows this error, the other function such as sorting,swapping nodes work fine and the cout function can show the expected text, but the exe window still gonna close really fast.

I think if these function can be processed means that I do create and pass the correct memory address of head, and I'm curious about how this error comes.

I also saw some error is SIGHUP P1990 Term Hangup detected on controlling terminal or death of controlling process.
Last edited on
the cout function can show the expected text, but the exe window still gonna close really fast.

Console Closing Down - http://www.cplusplus.com/forum/beginner/1988/
what about the 139 error? I don't think I used a memory address that's not mapped in the process. Thank you for your help.
Time to do some step by step debugging. Slow and tedious, but the seg fault shows your program is trying to use memory it doesn't own.
I do some testing and i think the error happens in this function is there some code i need to add if new operator is operated in different cpp and return address to main.cpp?

//addHead() function is in List_operation.cpp
ListNode* addHead()
{
ListNode* Head = new ListNode;
Head->data = 10 ;
Head->prev = nullptr;
Head->next = nullptr;
return Head;
}
Last edited on
> is there some code i need to add if new operator is operated in different cpp and return address to main.cpp?

No. The problem does not appear to be in the code you posted.

Does this run without errors? (after moving add_head to List_operation.cpp)

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
#include <iostream>

struct list_node
{
    int data = 0 ;
    list_node* next = nullptr ;
    list_node* prev = nullptr ;
};

list_node* add_head( list_node* curr_head, int v )
{
    auto new_head = new list_node{ v, curr_head } ; // note: prev is defaulted to nullptr
    if(curr_head) curr_head->prev = new_head ;
    return new_head ;
}

void print_list( const list_node* head )
{
    for( auto pn = head ; pn ; pn = pn->next ) std::cout << pn->data << " => " ;
    std::cout << "nullptr\n" ;
}

list_node* remove_head( list_node* curr_head )
{
    auto new_head = curr_head ? curr_head->next : nullptr ;
    delete curr_head ; // note: delete nullptr is a no op.
    return new_head ;
}

void destroy_list( list_node* head ) { while(head) head = remove_head(head) ; }

int main()
{
    list_node* head = nullptr ;
    for( int v : { 12, 32, 56, 64, 78, 81, 99 } )
    {
        head = add_head( head, v ) ;
        print_list(head) ;
    }

    while(head)
    {
        head = remove_head(head) ;
        print_list(head) ;
    }
}


http://coliru.stacked-crooked.com/a/310af6f8c96b0df5
Thank you all for your help! Problem solved. After checking, I found out that it is the memory leak problem and I need to deallocate memory if I want to compile them in separate cpp. Thanks.
Topic archived. No new replies allowed.