-1

I am trying to make a simple array of linked lists,every case in this array containing the head of a linked list,the code contains lot of functions,creating the lists,reading data in the lists,printing each node,and deleting the node.

#include <iostream>
using namespace std;
struct processus {
    int id;
    int prio;
    int etat;
    processus* suiv;
};
void inserD(processus*& D, int x, int y, int z)
{
    processus* nouv = new processus;
    nouv->id = x;
    nouv->prio = y;
    nouv->etat = z;
    nouv->suiv = D;
    D = nouv;
}
void afficher(processus** tab, int M)
{
    int i, j = 1;
    for (i = 0; i < M; i++) {
        processus* c = tab[i];
        while (c != NULL) {
            cout << j << "-->\t";
            cout << c->id << " ";
            cout << c->prio << " ";
            cout << c->etat << endl;
            c = c->suiv;
            j++;
        }
        cout << "\n";
    }
}
void supprimer(processus*& D)
{
    processus* supp;
    while (D != 0) {
        supp = D;
        D = D->suiv;
        delete supp;
    }
}
void insertF(processus* tab, int x, int y, int z)
{
    processus* last = new processus;
    last->id = x;
    last->prio = y;
    last->etat = z;
    if (tab == 0) {
        tab = last;
        last->suiv = 0;
    }
    else {
        processus* c = tab;
        while (c->suiv != 0) {
            c = c->suiv;
        }
        c->suiv = last;
        last->suiv = 0;
    }
}
void Creation(processus** tab, int l, int m)
{
    int x, y, z, i, j;
    for (i = 0; i < l; i++) {
        tab[i] = new processus[m];
        for (j = 0; j < m; j++) {
            cout << "donner id:";
            cin >> x;
            cout << "donner prio:";
            cin >> y;
            cout << "donner etat:";
            cin >> z;
            insertF(tab[i], x, y, z);
        }
    }
}
int main()
{
    int l, m;
    cout << "entrer le nombre des listes:";
    cin >> l;
    cout << "entrer le nombre de procesuss dans chaque liste";
    cin >> m;
    processus** tab = new processus*[l];
    for (int i = 0; i < l; i++) {
        *(tab + i) = NULL;
    }
    Creation(tab, l, m);
    afficher(tab, l);
    for (int i = 0; i < l; i++) {
        delete[] tab[i];
    }
    delete[] tab;
}

the problem is that when i run the code it always add a node at the first of each case in this array and output a random data in it,and i cant find the problem in the code.

Hackaholic
  • 19,069
  • 5
  • 54
  • 72
  • 2
    Have you tried running your code line-by-line in a debugger while monitoring the control flow and the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Dec 29 '22 at 16:59
  • 1
    People need to stop making the objects they want to store in a list also be nodes for the list. Make your object, make your node, then make your list. They are separate entities; code them as such. – sweenish Dec 29 '22 at 18:14
  • Have you tried any of these: 1) `std::vector` 2) `std::array` 3) `std::list<> my_list_array[27];`? – Thomas Matthews Dec 29 '22 at 20:07

2 Answers2

1

tab[i] = new processus[m] creates a new processus instance but all of its members are uninitialised, in insertF you then iterate through the linked nodes of tab[i] but as suiv is uninitialised this has undefined behaviour.

The simplest fix is to remove the line tab[i] = new processus[m] and then change insertF to take tab by reference so it can correctly initialise it with a new node.

You should also help avoid these problems by creating a constructor for processus which initialises all the members or more simply just give each member an inline initialiser:

struct processus {
    int id = 0;
    int prio = 0;
    int etat = 0;
    processus* suiv = 0;
};
rturrado
  • 7,699
  • 6
  • 42
  • 62
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • so declaring the first array of pointers already did the tab[i] = new processus[m] job.Thats mean the fix is to remove it.I understood thanks for the explanation. – Yacine Yacine Dec 29 '22 at 20:23
0

The only problem I've seen is that modifications to tab within insertF are only local. That can be fixed passing tab by reference as done in the other methods, i.e. processus*& tab.

Apart from that:

  • Also at Creation, tab[i] = new processus[m] is not needed. tab[i] is the head pointer to a list of processes. The inner for loop will be adding processes at the end of this list.
  • Removing that line also makes redundant the delete[] tab[i]; at the end of main. What should actually be called there is supprimer(tab[i]);.

In order to simplify things:

  • I would use, at least, a std::vector<processes*> tab{};.
  • Ideally, I would also use std::vector<std::unique_ptr<process>>. This way, there would be no need to deal with memory allocation/deallocation.
  • processus instances could be created by using aggregate initialization, e.g. processus{x, y, z, D} or processus{x, y, z}.

Finally, as coding best practices:

  • Always initialize variables.
  • Try to declare variables next to their first use.
  • Try not to declare more than one variable per line (e.g. int i, j = 1;).
  • Try to use descriptive variable names.
  • Prefer using nullptr to NULL or 0 for modern C++ code (since C++11).
  • using namespace std; should better be avoided, as it pollutes the program's namespace (see here).

[Demo]

rturrado
  • 7,699
  • 6
  • 42
  • 62