简体   繁体   中英

Exception thrown: write access violation. this was nullptr (C++)

I'm trying to initialize a Double-Link List and creating new nodes within it However, I keep getting this error when trying to create a new node "temp", filling it with data, then inserting it into the dLink list. Here's the code:

// Node of the Double-Link List
struct node {
    double x, y;
    node *prev;
    node *next;
};
// Double-Link List Function
struct dList {
    node *head;
    node *rear;
};
// Function to check if a list is empty
bool isEmpty(dList *L)
{
    if (L == NULL)
        return true;
    return false;
}
// Function to insert a node at the rear of a list
void insertAtRear(dList *L, double a, double b)
{
    node *temp = new node;
    temp->x = a;
    temp->y = b;
    if (isEmpty(L))
    {
        L->head = temp;
        L->rear = temp;
        return;
    }
    temp->prev = L->rear;
    L->rear->next = NULL;
    L->rear = temp;
    return;
}
// Main Function
int main() {
    dList *L1=NULL;
    dList *L2=NULL;
    string fileName1, fileName2;
    cout << "Please insert the name of the first csv file in which the information is stored:" << endl;
    cin >> fileName1;
    readFile(L1, fileName1);
    cout << "Please insert the name of the second csv file in which the information is stored:" << endl;
    cin >> fileName2;
    readFile(L2, fileName2);
    system("pause");
    return 0;
}

I didn't include the readFile function seeing as it's not the issue, just keep in mind that I'm calling insertAtRear() from within that function

I tried setting *prev and *next as nullptr when initializing them in the structure init. I'm aware that the issue is related to pointers and them not being correctly initialized but I can't seem to figure out how to fix the issue.

@Yksisarvinen is right, did you initialize L1 & L2 in the readFile function? they appear to be null.

"I'm aware that the issue is related to pointers and them not being correctly initialized"

You can initialize objects correctly with class constructors

struct node {
  double x, y;
  node *prev;
  node *next;
  node(const double &x, const double &y) : x(x), y(y), prev(nullptr), next(nullptr) {}
  node(const node&) = delete;
 ~node(void) {}
};

struct dList {
  node *head;
  node *rear;
  dList(node *head=nullptr, node *rear=nullptr) : head(head), rear(rear) {}
  dList(const dList&) = delete;
 ~dList(void)
  {
    for (; head != nullptr; head = rear)
    {
      rear = head->next;
      head->next = head->prev = nullptr;
      delete head;
    }
  }
};

implementation of isEmpty & insertAtRear should be something like:

bool isEmpty(dList *L)
{
  if (L == nullptr) throw "error: null dList pointer!!!!";
  return ((L.head == nullptr) || (L.rear == nullptr));
}

bool insertAtRear(dList *L, double a, double b)
{
  if (L == nullptr) return false;
  node *temp = new node(a, b);
  if (isEmpty(L)) L->head = L->rear = temp;
  else { temp->prev = L->rear; L->rear = L->rear->next = temp; }
  return true;
}

and main function can be:

int main() {
  dList L1;
  dList L2;
  ...
  readFile(&L1, fileName1);
  ...
  readFile(&L2, fileName2);
  system("pause");
  return 0;
}

I hope this is helpful

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM