EXC_BAD_ACCESS error

The code below is a node class, for implementation of linked list class. However, when running the code, there's error "EXC_BAD_ACCESS" in the member function Node<T>::Next(). Can anyone help me?

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
// node template class: element for linked list class
template <typename T>
class Node
{
private:
    T data;
    Node<T>* next;
    
public:
    Node<T>();
    Node<T>(const T& dt);
    T Data() const;
    void Data(const T& dt);
    Node<T>* Next();
    void Next(Node<T>* nd);
};

// Node.cpp implementation of Node class
#include "Node.hpp"

template<typename T>
Node<T>::Node()
{
    data=0;
    next=NULL;
}

template<typename T>
Node<T>::Node(const T& dt)
{
    data=dt;
    next=NULL;
}

template<typename T>
T Node<T>::Data() const
{
    return data;
}

template<typename T>
void Node<T>::Data(const T& dt)
{
    data=dt;
}

template<typename T>
Node<T>* Node<T>::Next()
{
    return next;  // Here error! Thread1: EXC_BAD_ACCESS(code=1,address=0x8)
}

template<typename T>
void Node<T>::Next(Node<T>* nd)
{
    next=nd;
}
 
Last edited on
> However, when running the code, there's error "EXC_BAD_ACCESS" in the member function Node<T>::Next().

To see what is going wrong, run the program with these additions:

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
63
64
65
66
67
68
69
70
71
72
#include <cstddef> // *** required: defines NULL ***

// node template class: element for linked list class
template <typename T>
class Node
{
private:
    T data;
    Node<T>* next;

public:
    Node<T>();
    Node<T>(const T& dt);
    T Data() const;
    void Data(const T& dt);
    Node<T>* Next();
    void Next(Node<T>* nd);
/////////////// added /////////////
private: void print_this( const char* desc ) const ;
///////////////////////////////////
};

// Node.cpp implementation of Node class
#include "Node.hpp"

/////////////// added /////////////
#include <iostream> // *** temporary; for diagnosing the problem

template < typename T > void Node<T>::print_this( const char* desc ) const
{ std::cout << desc << " node at address " << this << '\n' ; }
///////////////////////////////////

template<typename T>
Node<T>::Node()
{
    data=0;
    next=NULL;
    print_this( "constructed" ) ; // **** added ****
}

template<typename T>
Node<T>::Node(const T& dt)
{
    data=dt;
    next=NULL;
    print_this( "constructed" ) ; // **** added ****
}

template<typename T>
T Node<T>::Data() const
{
    return data;
}

template<typename T>
void Node<T>::Data(const T& dt)
{
    data=dt;
}

template<typename T>
Node<T>* Node<T>::Next()
{
    print_this( "access" ) ; // **** added ****
    return next;  // Here error! Thread1: EXC_BAD_ACCESS(code=1,address=0x8)
}

template<typename T>
void Node<T>::Next(Node<T>* nd)
{
    next=nd;
}
Display in the console:

constructed node at address0x7fff5fbff750
access node at address0x0

Below is my main function and LinkedList class:

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
// LinkedList class

#include "Node.hpp"

// template class of linked list
template <typename T>
class LinkedList
{
private:
    Node<T>* head;
    
public:
    LinkedList();
    //friend ostream& operator << (ostream& os, const LinkedList<T>& Llist);
    void InsertNode(T dt);
    
    //void Reverse();
    //void Reverse(int start, int end);
};

// LinkedList class implementation (partial)
template <typename T>
LinkedList<T>::LinkedList()
{
    head=NULL;
}

template<typename T>
void LinkedList<T>::InsertNode(T node_data)
{
    // create a ListNode
    Node<T> nd(node_data);
    Node<T>* tmp=&nd;
    
    // insert ListNode, if the Linked List is empty, return
    Node<T>* last=head;
    if(last)
    {
        head->Next(tmp);
        return;
    }
    while(last->Next()!=NULL)
    {
        last=last->Next();
    }
    last->Next(tmp);
    return;
}


// Main 
#include <iostream>
#include "LinkedList.hpp"

int main() {
    LinkedList<int> mylist;
    mylist.InsertNode(4);
    //mylist.InsertNode(5);
    //cout << mylist << endl;
    
    return 0;
}
Last edited on
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
template<typename T>
void LinkedList<T>::InsertNode(T node_data)
{
	//////////////////////////////////////////////// 
	// we can't do this. nd has automatic storage duration
	// the node will be destroyed when we return from the function
	// 
	// create a ListNode
	// Node<T> nd(node_data);
	////////////////////////////////////////////////

	Node<T>* tmp = new Node<T>(node_data); // create a node with dynamic storage duration
	// *** note: you would need to define the other foundation operations (destructor etc.)

	if (head == NULL)
	{
		// head->Next(tmp); // ******
		head = tmp; // *** if head was null, this node becomes the head
		return;
	}

	// insert ListNode, if the Linked List is empty, return
	Node<T>* last = head;
	while (last->Next() != NULL)
	{
		last = last->Next();
	}
	last->Next(tmp);
	return;
}
I got it! The Node object created in the InsertNode function will last until the end of the function. However, the Node pointer which I assign to the next member of the last node, is still there. So the pointer points to the beginning of the stack.
Thank you! I think it will works now.
Topic archived. No new replies allowed.