QT Write access violation

2.2k Views Asked by At

Hey guys i'm trying to make linked list by procedural programming in cpp in QT and whenever i try to add something to the list i get this error:

c:\users\marcin\documents\dev cpp\proc_list\proc_list.cpp:11: error: Exception at 0x13fc325cb, code: 0xc0000005: write access violation at: 0x1, flags=0x0 (first chance)

From what i've already read the problem should be that i try to access null pointer, but tried checking it already and it looks fine. Here's the faulty code:

void append_rec(QString name, int age, int number, float balance, item *first){
    item *newrec;
    item *temp;

    newrec = new item(this);
    temp = new item;

    newrec->next = NULL;
    newrec->name = name;
    newrec->number = number;
    newrec->balance = balance;
    newrec->age = age;
    temp = first;

    while(temp->next!= NULL)
        temp = temp->next;

    temp->next = newrec;
}

and the problem (as the debugger says pops out on the newrec->next = NULL; line. I'm just starting to learn cpp and seriously can't find the solution.

EDIT

Code for the item struct (for this assignment i'm not allowed to use classes):

#ifndef PROC_LIST_H
#define PROC_LIST_H

#include <qstring.h>

struct item{
    item *next;
    QString name;
    int age;
    int number;
    float balance;
};

void append_rec(QString name, int age, int number, float balance, item * first);
void display_list( item * first );

#endif // PROC_LIST_H

EDIT 2

The main window file to cover all the things i've do with my list.

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "proc_list.cpp"

item *first = NULL;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_front_add_clicked()
{
    append_rec(ui->name_edit->text(),
               ui->age_edit->text().toInt(),
               ui->accnum_edit->text().toInt(),
               ui->balance_edit->text().toFloat(),
               first);
}
1

There are 1 best solutions below

1
On BEST ANSWER

First thing I noticed was that you mentioned procedural programming, and then used the "this" pointer in a function. Simply remove it. You don't even have a constructor for the "item" struct to use.

Secondly, you do temp = new item item and immediately after you do this this temp = first. BAM memory leakage. But that's not a major problem.

Also notice your list base pointer is from the very start NULL and trying to dereference its member temp->next from your while may return garbage results and MAYBE evaluate to true even if its just garbage. STL lists for example have an "end" pointer to avoid that sort of things, you could do that as well. Hope I helped!

EDIT:

Well I managed to solve the problems in plain C++ but I may have did it overcomplicated. I chose to use a pointer-to-pointer because when the list is empty we will be able to change the base pointer through the argument. However I don't think this is the optimal solution but it works. Note you now need to pass the item's address when calling from main.

#include <iostream>

using namespace std;

struct item{
    item *next;
    string name;
    int age;
    int number;
    float balance;
};

void append_rec(string name, int age, int number, float balance, item** first){
    item *newrec = new item;

    newrec->next = NULL;
    newrec->name = name;
    newrec->number = number;
    newrec->balance = balance;
    newrec->age = age;

    while(*first!= NULL) // The pointer we are pointing to isnt valid
        first = &(*first)->next; // Point to the next pointer in the list

    *first = newrec; // Change the value of the pointer we are pointing to
}
void display_list( item * first )
{
    item* temp = first;
    while ( temp != nullptr )
    {
        cout << temp->name << '\n';
        cout << temp->number << '\n';
        cout << temp->balance << '\n';
        cout << temp->age << '\n';
        cout << "<===================||=======================>";

        temp = temp->next;
    }
}

item *first = NULL;
int main()
{
    append_rec("something 1", 1, 1, 1.f, &first);
    append_rec("something 2", 2, 2, 2.f, &first);
    append_rec("something 3", 3, 3, 3.f, &first);
    append_rec("something 4", 4, 4, 4.f, &first);

    display_list(first);
}