Reading files, using two instances

Hello, i want to know if someone can help me solving the following problem:

The problem is: I do have a template class and I'm using two instances of the same types. I want to read from two files using the same method but the two instances. But when I try reading the two plain text files it give me a big error. This doesn't happen when I read a file with only one instance.

This doesn't have sense for me, but the computer not lie. I hope someone can help me.

Class code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
template <typename ElementoPTR, typename Elemento>
class Arreglo
{
 public:
     Arreglo();
     //~Arreglo();
     void Lectura(ifstream &);
     void Impresion(ofstream &);
     void MayorPromedio(ofstream &);
     void Ordenar();
     bool Search(string nombre);
     void getter(Elemento Lista[], int& cantidad);
     void setter(Elemento Lista[], int& cantidad);
     void getterCantidad(int &);
     void setCantidad(int i, int j);
     void GetterLista(ElementoPTR ListaAux);
     void SetterLista(ElementoPTR ListaAux, ElementoPTR ListaAux2, int size1, int size2);
     //float Promedio(string nombre);
 private:
     ElementoPTR Nomina;
     int Cantidad;
};


Reading method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template <typename ElementoPTR,typename Elemento>
void Arreglo<ElementoPTR,Elemento>::Lectura (ifstream &Entrada){
 
  int indice(0);
    string LastName, FirstName;
    Entrada >> Cantidad;
    Nomina = new Elemento[Cantidad];
    Entrada >> LastName >> FirstName;
    while(!Entrada.eof()){
        Nomina[indice].FullName = LastName + ' ' + FirstName;
        Entrada >> Nomina[indice].LIMITEQUIZES;
        Nomina[indice].Quizes = new float[Nomina[Cantidad].LIMITEQUIZES];
        for(int j=0; j < Nomina[indice].LIMITEQUIZES; j++)
          Entrada >> Nomina[indice].Quizes[j];
        indice++;
        Entrada >> LastName >> FirstName;
    }
}


Menu code (called by the driver):
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
struct Element 
{
  string FullName;
  int LIMITEQUIZES;
  FloatPTR Quizes;
};
typedef Element Elemento;
 
typedef Elemento* ElementoPTR;
 
void Menu()
{
 
Arreglo<ElementoPTR, Elemento> comp3110;
    Arreglo<ElementoPTR, Elemento> curso;
    ifstream Entrada, file;
    ofstream Salida, Salida1;
    Entrada.open("Asign1.ent");
    file.open("Asign2.ent");
    assert(!file.fail());
    Salida.open ("Asign1.sal");
    Salida1.open ("Asign1a.sal");
    
    
    comp3110.Lectura(Entrada);
    curso.Lectura(file);
               
}


Error message:
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
*** glibc detected *** ./a.out: free(): invalid next size (fast): 0x088d0758 ***
======= Backtrace: =========
/lib/libc.so.6[0x9836c5]
/lib/libc.so.6(cfree+0x59)[0x983b09]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0x2ffe5c1]
/usr/lib/libstdc++.so.6(_ZNSs4_Rep10_M_destroyERKSaIcE+0x1d)[0x2fdb18d]
/usr/lib/libstdc++.so.6(_ZNSsD1Ev+0x57)[0x2fddd37]
./a.out[0x8049111]
./a.out(__gxx_personality_v0+0x2f3)[0x8048d4b]
./a.out(__gxx_personality_v0+0x3e6)[0x8048e3e]
/lib/libc.so.6(__libc_start_main+0xdc)[0x92fe9c]
./a.out(__gxx_personality_v0+0x79)[0x8048ad1]
======= Memory map: ========
006a0000-006a1000 r-xp 006a0000 00:00 0          [vdso]
007b8000-007c3000 r-xp 00000000 08:01 23265361   /lib/libgcc_s-4.1.2-20080825.so.1
007c3000-007c4000 rwxp 0000a000 08:01 23265361   /lib/libgcc_s-4.1.2-20080825.so.1
008fb000-00916000 r-xp 00000000 08:01 23265294   /lib/ld-2.5.so
00916000-00917000 r-xp 0001a000 08:01 23265294   /lib/ld-2.5.so
00917000-00918000 rwxp 0001b000 08:01 23265294   /lib/ld-2.5.so
0091a000-00a6d000 r-xp 00000000 08:01 23265297   /lib/libc-2.5.so
00a6d000-00a6f000 r-xp 00153000 08:01 23265297   /lib/libc-2.5.so
00a6f000-00a70000 rwxp 00155000 08:01 23265297   /lib/libc-2.5.so
00a70000-00a73000 rwxp 00a70000 00:00 0 
00a7c000-00aa3000 r-xp 00000000 08:01 23265319   /lib/libm-2.5.so
00aa3000-00aa4000 r-xp 00026000 08:01 23265319   /lib/libm-2.5.so
00aa4000-00aa5000 rwxp 00027000 08:01 23265319   /lib/libm-2.5.so
02f4b000-0302b000 r-xp 00000000 08:01 10136184   /usr/lib/libstdc++.so.6.0.8
0302b000-0302f000 r-xp 000df000 08:01 10136184   /usr/lib/libstdc++.so.6.0.8
0302f000-03030000 rwxp 000e3000 08:01 10136184   /usr/lib/libstdc++.so.6.0.8
03030000-03036000 rwxp 03030000 00:00 0 
08048000-0804a000 r-xp 00000000 08:11 6751264    /home/math/harol_ps/Desktop/TODASASIG4/Asignacion4B/a.out
0804a000-0804b000 rw-p 00001000 08:11 6751264    /home/math/harol_ps/Desktop/TODASASIG4/Asignacion4B/a.out
088c8000-088e9000 rw-p 088c8000 00:00 0          [heap]
b7f3a000-b7f3c000 rw-p b7f3a000 00:00 0 
bf872000-bf887000 rw-p bffe9000 00:00 0          [stack]
Aborted


I will be really thankful for your help, time and suggestions.
It looks like you are corrupting your heap somewhere.
The code you posted does not appear to cause the problem.

I do suggest you combine lines 8, 9, and 16, even though you have been careful to avoid EOF problems:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template <typename ElementoPTR,typename Elemento>
void Arreglo<ElementoPTR,Elemento>::Lectura (ifstream &Entrada){
 
    int indice(0);
    string LastName, FirstName;
    Entrada >> Cantidad;
    Nomina = new Elemento[Cantidad];
    while (Entrada >> LastName >> FirstName) {
        Nomina[indice].FullName = LastName + ' ' + FirstName;
        Entrada >> Nomina[indice].LIMITEQUIZES;
        Nomina[indice].Quizes = new float[Nomina[Cantidad].LIMITEQUIZES];
        for(int j=0; j < Nomina[indice].LIMITEQUIZES; j++)
          Entrada >> Nomina[indice].Quizes[j];
        indice++;
    }
}

I think it is likely that you are trying to free the same piece of memory twice.

Good luck!
template <typename ElementoPTR, typename Elemento> don't do that. You are asking for a misuse.
Instead
1
2
3
4
template <typename Elemento>
class Arreglo{
  typedef Elemento* ElementoPTR;
};


Don't loop on eof, in general you will detect it too late
Instead while( Entrada>>LastName>>Firstname ){

Nomina[indice].Quizes = new float[Nomina[Cantidad].LIMITEQUIZES]; //line 12 That can't be good. Nomina[Cantidad] is out of bounds. And if it weren't, it contains garbage at this point.
I think it should be Nomina[indice]

Last, make yourself a favor and use std::vector
Topic archived. No new replies allowed.