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.
CodePudding user response:
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;
};
CodePudding user response:
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 innerfor
loop will be adding processes at the end of this list. - Removing that line also makes redundant the
delete[] tab[i];
at the end ofmain
. What should actually be called there issupprimer(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}
orprocessus{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
toNULL
or0
for modern C code (since C 11). using namespace std;
should better be avoided, as it pollutes the program's namespace (see here).