-1

I have been working all day on not just this but 3 other assignments(for the same class) and this is the last issue I cannot figure out on my own, I am new with templates so not so sure as to how they work 100%.

Without templates this code runs perfectly, but with templates I am receiving a segmentation fault in the prioqueueUNS file at the if(head == NULL) and I cannot figure out why this is happening because I am defaulting head to NULL in the constructor, so any help would be very appreciated

int main

#include "node.h" 
#include "prioqueueUNS.cpp"
int main() { 
    PrioQueueUNS<int> list; 
    list.insertItem(1);
} 

node.h

#ifndef node_h
#define node_h
using namespace std; 
template<class Type> 
struct node { 
    Type data; 
    node<Type> *next; 
}; 
#endif

prioqueueUNS.cpp

#ifndef prioqueueUNS_cpp
#define prioqueueUNS_cpp
#include "node.h
using namespace std; 

template<class Type> 
class PrioQueueUNS { 
private: 
    node<Type> *head; 
    node<type> *tail;
    int sizee; 
    int size;
    int min; 

public: 
    PrioQueueUNS() { 
        head = NULL;
        tail = NULL;
    } 

    PrioQueueUNS(Type *dataArray, int n) { 
        head = NULL;
        tail = NULL;
    } 

    void insertItem(Type n) { 
        node<Type> *temp;
        temp->data = n;
        temp->next = NULL;

        if (head == NULL) { //<-- segment faulting when trying to access head
            head = temp;
            tail = temp;
            min = n; 
        } 
     }
 };
HMD
  • 2,202
  • 6
  • 24
  • 37
Colby Grappe
  • 47
  • 1
  • 7
  • Possible duplicate of [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Raedwald Apr 20 '18 at 06:53

2 Answers2

3
node<Type> *temp;
temp->data = n;

You create a pointer (temp) but it points to nothing, so temp->data tries to access a data field of something that does not exist.

You could fix this by using new but this would require you to destroy the object afterwards.

Jerome Reinländer
  • 1,227
  • 1
  • 10
  • 26
  • so with this, my seg fault is not coming from temp, its coming from if(head == NULL), if i put a cout after all the temp stuff, it will still run through and seg fault on head – Colby Grappe Apr 20 '18 at 06:56
  • but it fixed the issue i think – Colby Grappe Apr 20 '18 at 06:57
  • if you see this, do you have any clue why my linked list can take in roughly 1.4 million values before it seg faults now – Colby Grappe Apr 20 '18 at 07:11
  • @ColbyGrappe Sorry, can't help you with that. My first intuition was that you may be running out of memory (have you checked that?), but I am not sure if this can cause a segmentation fault and 1.4 million values should still be in the mega byte range, which should be fine, I guess. You might want to open a new question with this very specific question and information. – Jerome Reinländer Apr 20 '18 at 07:39
0

You never allocate memory for your node. Also declaration of pointer temp is also not correct:-

 node<Type> *temp;// temp is a pointer only you have to allocate memory first
 temp->data = n;
 temp->next = NULL;

Correct it like below:-

For an integer:-

node<int> *temp = new node<int>();

For a flot:-

 node<flot> *temp = new node<flot>();     

Also correct below declaragtion:-

node<type> *tail;  to node<Type> *tail;
Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17