Segmentation fault(core dumped)


It is somewhat big for a post but it's a simple, linear logic.
It's probably n00b mistake, but I just have to know...
Thank you

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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410

#include <iostream>

#include <string>

#include <vector>

#include <fstream>

using namespace std;

class Consumidor
{
   public:
    static int numeroIdentificacao;
    int id;
    string tipoConsumidor;
    string nomeProprietario;
    string morada;
    string codigoPostal;
    vector <double> Consumos;
    Consumidor() : id(0) 
    {tipoConsumidor = ""; nomeProprietario = "";morada = "";codigoPostal = "";}
    Consumidor(string tipo, string nom, string morad, string codpost, vector <double> consum) : id(numeroIdentificacao)
     { tipoConsumidor = tipo; nomeProprietario = nom; morada = morad;           codigoPostal = codpost; Consumos = consum;  numeroIdentificacao++; }

     int getID() {return id;}

     string getTipo() {return tipoConsumidor;}

     string getNome() {return nomeProprietario;}

     string getMorada() {return morada;}

     string getPostal() {return codigoPostal;}

     double getConsumo(int i) {return Consumos.at(i-1);}

     double getConsumoTotal();

     void setID(int i) {id = i;}

     void setTipo(string t) {tipoConsumidor = t;}

     void setNome(string n) {nomeProprietario = n;}

     void setMorada(string m) {morada = m;}

     void setPostal(string p) {codigoPostal = p;}

     void setConsumo(int i, double c) {Consumos.at(i - 1) = c;}

     void fonteFile(string); 

	 void adicionarConsumidor(); 

	 void removerConsumidor(int); 

	 void removerConsumidor(string); 

	 virtual void imprim(); 

     void imprimUm(int);  

     void imprimUm(string); 

     void imprimTotal(ofstream &);	

     void imprimTotal(vector<Consumidor*> v);

     void imprimConsumos();

     virtual double Gastos();   

     vector <Consumidor*> consumi;

};

double Consumidor :: Gastos() 
{
double d; return d;
}



class Menus : public Consumidor

{

  public:

         Menus() : Consumidor() {};

         void MainMenu();

         void MiniMenu(int);

         void MiniMenu(string);

};   



class Empresa : public Consumidor

{

   public:

	string dono;



   public:

	Empresa(string tipo, string nom, string morad, string codpost, vector<double> consum, string don) :  Consumidor(tipo, nom, morad, codpost, consum)

                   {dono = don;}

    void setDono(string don)

    {dono = don;}

    string getDono()

    {return dono;}

};



class PequenaMedia : public Empresa//********************************************************

{

   public:

	float taxapm;



   public:

	PequenaMedia(string tipo, string nom, string morad, string codpost, vector<double> consum, string don, double taxa) : Empresa (tipo, nom, morad, codpost, consum, don)

                 { taxapm = taxa; }

	void setTaxaPM(float taxa)

	{taxapm = taxa;}

	double getTaxa()

	{ return taxapm;}

    void imprim();

    double  Gastos() {return getConsumoTotal() * taxapm;}

};



class Grande : public Empresa //********************************************************

{

  public:

	float taxagrande;



   public:

	Grande(string tipo, string nom, string morad, string codpost, vector<double> consum, string don, double taxa) : Empresa (tipo, nom, morad, codpost, consum, don)

              { taxagrande = taxa; }

    void setTaxaG(float taxa)

    {taxagrande = taxa;}   

    double getTaxa()

    { return taxagrande;}      

    void imprim();

    double  Gastos() {return getConsumoTotal()*taxagrande;}

};



class Particular : public Consumidor//********************************************************

{

   public:

    Particular (string tipo, string nome, string morada, string codpost, vector<double> gastos) : Consumidor (tipo, nome, morada, codpost, gastos)

    {}

};



class Comercial : public Particular //********************************************************

{

   public:

	float taxacomercial;



   public:

	Comercial(string tipo, string nom, string morad, string codpost, vector<double> consum, double taxa) : Particular(tipo, nom, morad, codpost, consum)

	{taxacomercial = taxa;}

	 void setTaxaCM(float taxa)

	 { taxacomercial = taxa; }

	 double getTaxa()

	 { return taxacomercial;}

	 void imprim();

     double  Gastos() {return getConsumoTotal() * taxacomercial;}

};





class Domestico: public Particular//********************************************************

{

   public:

	float taxadomestico;



   public:

	Domestico(string tipo, string nom, string morad, string codpost, vector<double> consum, double taxa) : Particular(tipo, nom, morad, codpost, consum)

	

    {taxadomestico = taxa;}

	void setTaxaDM(float taxa)

	{taxadomestico = taxa;}

	double getTaxa()

	{ return taxadomestico;}

    void imprim();

    double Gastos() {return getConsumoTotal() * taxadomestico;}

};

int Consumidor :: numeroIdentificacao = 1;

double Consumidor :: getConsumoTotal()
{

       double soma = 0;

       unsigned int i = 0;

       for(i = 0; i < Consumos.size(); i++)

       {

             soma += Consumos.at(i);

       }

       return soma;

}

void Consumidor :: fonteFile(string nome)

{

     ifstream file (nome.c_str()); 

     string tipo, name, morada, codpost, dono;

     vector <double> con;

     float taxa;

     char lixo;

     while(!file.eof())

     {

       getline(cin, tipo);

       if(tipo == "PD" || tipo == "PC")

        { 

          getline(file, name);

          getline(file, morada);

          getline(file, codpost);

          file >> con.at(0) >> con.at(1)>> con.at(2)>> con.at(3)>> con.at(4)>> con.at(5)>> con.at(6)>> con.at(7)>> con.at(8)>> con.at(9)>> con.at(10)>> con.at(11);

          file >> taxa;

          file >> lixo;

          if(tipo == "PD") 

             {

                   Domestico pd = Domestico(tipo, name, morada, codpost, con, taxa); 

                   consumi.push_back(&pd);

                   return;

             }

          else if(tipo == "PC") 

             { 

                   Comercial pc = Comercial(tipo, name, morada, codpost, con, taxa); 

                   consumi.push_back(&pc);

                   return;

             }

          }

       else if(tipo == "EMP" || tipo == "EG")

        { 

          getline(file, dono);

          getline(file, name);

          getline(file, morada);

          getline(file, codpost);

          cin >> con.at(0) >> con.at(1)>> con.at(2)>> con.at(3)>> con.at(4)>> con.at(5)>> con.at(6)>> con.at(7)>> con.at(8)>> con.at(9)>> con.at(10)>> con.at(11);

          file >> taxa;

          file >> lixo;

          if(tipo == "EG")  

          { 

                  Grande eg = Grande(tipo, name, morada, codpost, con, dono, taxa); 

                  consumi.push_back(&eg); 

                  return;

          }

          else if(tipo == "EMP") 

          {

                  PequenaMedia emp = PequenaMedia(tipo, name, morada, codpost, con, dono, taxa); 

                  consumi.push_back(&emp);

                  return;

          }

        }

     }     

} 





What debugger are you using? Where did the software stop running? The stack dump should tell you something.
Im using Ubuntu Geany 0.16.

The exact problems I ran into was:
if I run a function to run the vector<Consumidor*>consumi using iterators to print (eg: (*it)->imprim(); return; ) it goes over that line

also if I try to acess the vector directly ( eg: consumi.at(i)->imprim(); ) it acuses segmentation fault

I can push_back() objects with no problem, but it fails to acess the vector to print it.


1
2
3
4
5
6
7
8
9
10
11
          if(tipo == "EG")  

          { 

                  Grande eg = Grande(tipo, name, morada, codpost, con, dono, taxa); 

                  consumi.push_back(&eg); 

                  return;

          } // at this point eg is destroyed and consumi has a bad pointer 


There is your problem. You are taking the address of temporary objects and then inserting them into a dynamic array. You can't do that. At the end of that code block your vector contains a dangling pointer to an object that has been destroyed automatically at the end of the block scope.

Based on what I saw there is no really simple solution. You have to rework the program and either use new to construct the objects or you will to make your classes copyable with the copy constructor and assignment operator so that you can copy the objects into the container. You could implement the clone pattern and clone the temporary objects so that you can push the pointer to the clone into the vector.
Last edited on
Thanks, I think it's working as I want now

BTW, sorry for the double post, I was trying to post the rest of the code and somehow doubleposted.

Last edited on
Topic archived. No new replies allowed.