Access violation?

So I am trying to write a program with structs, function prototypes, references, pointers (for school).

I am able to execute this program, but after running the console and entering some info in the "Transact_AjouterUnNouveauClient(BaseDeDonnées base)" function, it stops and reads the following;

Exception thrown: read access violation.
_Pnext was 0xFFFFFFFFFFFFFFFF.


Here is my code:
https://pastebin.com/1j1nVZxW
That is a lot of code to ask someone to debug for you.

An Access Violation (or Segfault for people using Linux) occurs when you try to dereference a pointer to memory you are not allowed to touch.

In simple terms, it means you are not being careful with your pointers.


To find out where it is happening, sprinkle some output statements throughout your function and see how many of them you get before the access violation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Transact_AjouterUnNouveauClient(BaseDeDonnées base)
{
std::cerr << "one\n";
    IU_FlushTampons();
    clrscr();
    int affichage_x;
    base.nombreDeClients++;
    string date;
    char c;

std::cerr << "two\n";
   
 
    base.clients[base.nombreDeClients - 1].clientDepuis = time(0);
std::cerr << "three\n";
    date = dateAString(base.clients[base.nombreDeClients - 1].clientDepuis);
std::cerr << "four\n";
...

and so on.

Keep narrowing it down until you understand where, exactly, the error is occurring.
This will also tell you which pointer has a bad value.

At this point, you can track back to see where the pointer gets its value, and you will then find the logic error that you made to get it that way.

Hope this helps.
Not only is it a fair amount of code, but it can only be run on Windows! (So I can't run it, for instance.)

In simple terms, it means you are not being careful with your pointers.

Or you are writing beyond the bounds of an array, either causing a segfault thereby or overwriting something that causes a segfault later. (But an array is a kind of pointer under the hood, I guess.)

I do notice that you are passing BD by value where it should presumably be passed by reference.
And I also happened to notice that you are indexing the compte array with 1, 2, 3 where it should be indexed with 0, 1, 2.
Last edited on
closed account (z05DSL3A)
And I also happened to notice that you are indexing the compte array with 1, 2, 3 where it should be indexed with 0, 1, 2.
Damn, I was just going to mention that...
I’ve removed comments and now the code fits inside a forum post (just in case this code came back in the future):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <string>
#include <windows.h>
#include <time.h>
#include "../console(v1.9).h"

using namespace std;

const double MARGE_CREDIT_MAX = 10000;
const double SOLDE_COMPTE_MAX = 1000000;

const int NB_CLIENTS_MAX = 70;

const int NB_COMPTES_PAR_CLIENT_MAX = 3;

enum Commandes {
    ajouter,
    depot,
    retrait,
    virement,
    afficherInfo,
    listerClients,
    supprimer,
    quitter
};

struct Nom
{
    string prenom;
    string nom;
};

struct Adresse
{
    string noCivique;
    string rue;
    string ville;
    string codePostal;
};

struct InfoPersonnel
{
    Nom     nom;
    Adresse adresse;
    string  telephone;
    string  nas;
};

struct Compte
{
    double solde;
    double margeCredit;
};

struct Client
{
    InfoPersonnel   info;
    Compte          compte[NB_COMPTES_PAR_CLIENT_MAX];
    time_t          clientDepuis;
};

struct BaseDeDonnées
{
    int nombreDeClients;
    Client clients[NB_CLIENTS_MAX];
};

void IU_FlushTampons();
double arrondir(double v, int p);
void IU_Bip();
void IU_Bip_DialogueErreur(string message);

void IU_FlushTampons()
{
    if (cin.fail()) cin.clear();
    cin.ignore(cin.rdbuf()->in_avail());
    while (_kbhit()) _getch();
}

double arrondir(double v, int p = 2)
{
    double e = pow(10, p);
    return round(v * e) / e;
}

void IU_Bip() { cout << "\a"; }

void IU_Bip_DialogueErreur(string message)
{
    IU_Bip();
    MessageBoxA( NULL, message.c_str(), "ERREUR", MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL );
}

string dateAString(time_t date)
{
    string date_en_lettres;
    tm ltm;
    localtime_s(&ltm, &date);

    date_en_lettres = to_string(ltm.tm_mday) + "/" + to_string(1 + ltm.tm_mon) + "/" + to_string(1900 + ltm.tm_year);

    return date_en_lettres;
}

void IU_AfficherMenuPrincipal()
{
    gotoxy(16, 0); cout << "Compagnie 420-B21";
    gotoxy(16, 4); cout << "1. Ajouter un client";
    gotoxy(16, 6); cout << "2. Dépôt";
    gotoxy(16, 8); cout << "3. Retrait";
    gotoxy(16, 10); cout << "4. Virement";
    gotoxy(16, 12); cout << "5. Afficher les informations d'un client";
    gotoxy(16, 14); cout << "6. Lister les clients et leur crédit actuel";
    gotoxy(16, 16); cout << "7. Supprimer un client";
    gotoxy(16, 18); cout << "8. Quitter";
    gotoxy(16, 24); cout << "Entrez votre choix: ";
}

Commandes IU_LireUnChoixValideDuMenuPrincipal()
{
    Commandes cmd = quitter;
    COORD choixCoord = { wherex(),wherey() };
    bool choixValide = false;
    int choix;
    char lettre_choix;
    do
    {
        clreol();
        IU_FlushTampons();
        gotoxy(choixCoord.X, choixCoord.Y);
        choix = _getch();

        if (isdigit(choix))
        {
            choix = choix - '0';
            choixValide = true;
            cout << choix;
        }
        else {
            lettre_choix = choix;
            cout << lettre_choix;
        }

    } while (choix < 1 || choix > 8 || !choixValide);

    switch (choix)
    {
    case 1:
        cmd = ajouter;
        break;
    case 2:
        cmd = depot;
        break;
    case 3:
        cmd = retrait;
        break;
    case 4:
        cmd = virement;
        break;
    case 5:
        cmd = afficherInfo;
        break;
    case 6:
        cmd = listerClients;
        break;
    case 7:
        cmd = supprimer;
        break;
    case 8:
        cmd = quitter;
    }
    return cmd;
}

void IU_MessageDeFinDuProgramme()
{

}

int IU_LireUnNoDeClient(int max);
int IU_LireUnNoDeCompteValide();
double IU_LireUnMontantValide(double max);
void IU_AfficherLesInformationsDuClient(Client client);
Client UI_LireLesInfosNouveauClient();

void Transact_AjouterUnNouveauClient(BaseDeDonnées base)
{

    IU_FlushTampons();
    clrscr();
    int affichage_x;
    base.nombreDeClients++;
    string date;
    char c;

    base.clients[base.nombreDeClients - 1].clientDepuis = time(0);
    date = dateAString(base.clients[base.nombreDeClients - 1].clientDepuis);

    gotoxy(0, 0); cout << "Transaction: ajouter un nouveau client";
    gotoxy(0, 2); cout << "Création du client #" << base.nombreDeClients;

    gotoxy(0, 4); cout << left << setw(21) << "Prénom" << ": ";
    cin >> base.clients[base.nombreDeClients - 1].info.nom.prenom;
    gotoxy(0, 5); cout << left << setw(21) << "Nom" << ": ";
    cin >> base.clients[base.nombreDeClients - 1].info.nom.nom;

    gotoxy(0, 7); cout << left << setw(21) << "Numéro civique" << ": ";
    cin >> base.clients[base.nombreDeClients - 1].info.adresse.noCivique;
    gotoxy(0, 8); cout << left << setw(21) << "Rue" << ": ";
    cin >> base.clients[base.nombreDeClients - 1].info.adresse.rue;
    gotoxy(0, 9); cout << left << setw(21) << "Ville" << ": ";
    cin >> base.clients[base.nombreDeClients - 1].info.adresse.ville;
    gotoxy(0, 10); cout << left << setw(21) << "Code postal" << ": ";
    cin >> base.clients[base.nombreDeClients - 1].info.adresse.codePostal;
    gotoxy(0, 11); cout << left << setw(21) << "Téléphone" << ": ";
    cin >> base.clients[base.nombreDeClients - 1].info.telephone;

    gotoxy(0, 13); cout << left << setw(21) << "Numéro d'assurance sociale" << ": ";
    cin >> base.clients[base.nombreDeClients - 1].info.nas;

    gotoxy(0, 15); cout << "Marge de crédit du compte #1 (Max de " << fixed << setprecision(2) << MARGE_CREDIT_MAX << " $)" << ": "; affichage_x = wherex();
    cin >> base.clients[base.nombreDeClients - 1].compte[1].margeCredit;
    gotoxy(affichage_x, 15); cout << base.clients[base.nombreDeClients - 1].compte[1].margeCredit;

    gotoxy(0, 16); cout << "Marge de crédit du compte #2 (Max de " << fixed << setprecision(2) << MARGE_CREDIT_MAX << " $)" << ": "; affichage_x = wherex();
    cin >> base.clients[base.nombreDeClients - 1].compte[2].margeCredit;
    gotoxy(affichage_x, 16); cout << base.clients[base.nombreDeClients - 1].compte[2].margeCredit;

    gotoxy(0, 17); cout << "Marge de crédit du compte #3 (Max de " << fixed << setprecision(2) << MARGE_CREDIT_MAX << " $)" << ": "; affichage_x = wherex();
    cin >> base.clients[base.nombreDeClients - 1].compte[3].margeCredit;
    gotoxy(affichage_x, 17); cout << base.clients[base.nombreDeClients - 1].compte[3].margeCredit;

    gotoxy(0, 19); cout << "Date de création de ce dossier: " << date;

    gotoxy(0, 22); cout << "Appuyez sur une touche pour continuer";
    c = _getch();
}

void Transact_FaireUnDepot(  )
{

}

void Transact_FaireUnRetrait(  )
{

}

void Transact_FaireUnVirement(  )
{

}

void Transact_AfficherTouteslesInfosDuClient(  )
{

}

void Transact_ListerLesClientsEtLeurCréditActuel(  )
{

}

void Transact_SupprimerUnClient(  )
{

}

void main()
{

    SetConsoleCP(1252);
    SetConsoleOutputCP(1252);

    BaseDeDonnées BD;
    BD.nombreDeClients = 0;
    Commandes cmd;
    do
    {
        IU_AfficherMenuPrincipal();
        cmd = IU_LireUnChoixValideDuMenuPrincipal();

        switch (cmd)
        {
        case ajouter: Transact_AjouterUnNouveauClient(BD); break;
        case depot: Transact_FaireUnDepot(); break;
        case retrait: Transact_FaireUnRetrait(); break;
        case virement: Transact_FaireUnVirement(); break;
        case afficherInfo: Transact_AfficherTouteslesInfosDuClient(); break;
        case listerClients: Transact_ListerLesClientsEtLeurCréditActuel(); break;
        case supprimer: Transact_SupprimerUnClient(); break;
        case quitter: IU_MessageDeFinDuProgramme(); break;
        }

    } while(cmd != quitter );
    _getch();
}

while (_kbhit()) _getch();

What is the while statement meant to do. I think might be 2-steps behind..Hence, I am just trying to understand the code
thx
Topic archived. No new replies allowed.