Problem with 2 objects of same class

Program crashes when i create 2 objects of "imagen" class, it works with 1 object, here is the code:

MAIN.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <tr1/array>

#include "Imagen.hpp"

using namespace std;
using namespace Jose;

int main() {
   imagen prueba,aux; 
   bool ok;

   prueba.leer("a1.ppm",ok);
   system ("pause");
}



IMAGEN.HPP
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
#ifndef _Imagen_hpp_
#define _Imagen_hpp_
#include <string>
#include <tr1/array>
#include <cassert>
#include <iostream>

namespace Jose {
    class imagen {
        static const unsigned MaxTam = 500;
        struct RGB {
            short r;
            short g;
            short b;
         };
        typedef std::tr1::array <RGB, MaxTam> TFila;
        typedef std::tr1::array <TFila, MaxTam> TMatriz;
        std::string tipo;
        unsigned columnas;
        unsigned filas;
        short MaxRGB;
        TMatriz dato;
    public:
        imagen ();
        void leer (const std::string & nombrefich, bool & ok);
    };
}
#endif 


IMAGEN.CPP
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
#include <string>
#include <tr1/array>
#include <cassert>
#include <iostream>
#include <fstream>

#include "Imagen.hpp"

namespace Jose {
      imagen::imagen () {
         columnas=0;
         filas=0;
         MaxRGB=0;
         for (unsigned i=0;i<filas;i++) {
            for (unsigned j=0;i<columnas;i++) {
               dato[i][j].r=0;
               dato[i][j].g=0;
               dato[i][j].b=0;
            }
         }
      }

   void imagen::leer (const std::string & nombrefich, bool & ok) {
      std::ifstream f_ent;
      unsigned num_cuenta;
      float saldo_cuenta;
      std::string nombre_cuenta;
      f_ent.open(nombrefich.c_str());
      if (f_ent.fail()) {
         ok=false;
      } else {
         ok=true;
         f_ent>>tipo>>columnas>>filas>>MaxRGB;
         for (unsigned i=0;i<filas;i++) {
			   for (unsigned j=0;j<columnas;j++) {
			      f_ent>>dato[i][j].r;
			      f_ent>>dato[i][j].g;
			      f_ent>>dato[i][j].b;
            }
         }
         ok=!f_ent.fail();
         f_ent.close();
      }
   }

   
}


Last edited on
You're most likely overflowing the stack. This is because imagen objects are very large (more than 1.5 MB each), and the stack is quite small by default (it doesn't really matter that you have 4 GB of RAM or something). As a simple test, move declarations of prueba and aux to the global scope. If the program works, it's the stack.

How to fix it? Allocate your matrix from the heap. I suggest to use std::vector for it (or boost::multi_array). This method has additional advantages: there is no limit on the max size (other than the amount of RAM), and you are not wasting memory for unused entries.
His class isn't that large.

The problem looks to me like he's corrupting the heap:

1
2
3
4
5
6
7
8
         f_ent>>tipo>>columnas>>filas>>MaxRGB;
         for (unsigned i=0;i<filas;i++) {
			   for (unsigned j=0;j<columnas;j++) {
			      f_ent>>dato[i][j].r;
			      f_ent>>dato[i][j].g;
			      f_ent>>dato[i][j].b;
            }
         }


You never set the dims of dato, so you are stepping outside the bounds of the arrays here.

I'm not even sure if you can set the size of an array at runtime like this. You might need to use a vector instead so you can resize.
Unfortunately the OP is guilty of cross posting - and I was answering him in another thread.

Unfortunately the problem is stack overflow as suggested by Abramus - objects of the imagen class are very very
big (as he is using the tr1::array class which really is an array) and he is creating a very
big array here:
1
2
3
4
5
6
7
8
       
          typedef std::tr1::array <RGB, MaxTam> TFila;
        typedef std::tr1::array <TFila, MaxTam> TMatriz;
        std::string tipo;
        unsigned columnas;
        unsigned filas;
        short MaxRGB;
        TMatriz dato; //<<===Very big array 
there is any way to do it without classes that already exist? I have to do the entire program by myself without using any external code
solved, just short array
Now you need to sort out those loops - as mentioned earlier by Disch
Topic archived. No new replies allowed.