Array-Grenzen werden Grundlos überschritten...



  • x.Resize(x.Zeil,x.Spalt);   
     // cout << setiosflags(ios::right);
      for (int i=0; i<x.Zeil; i++)
        for (int j=0; j<x.Spalt; j++)
      {
        s >> ws;
        s >> x.Mat[i][j];
        x.Mat[i][j] = x.Mat[i][j] / x.farbtiefe;
        s >> ws;
        cout <<" ("<<i<<","<<j<<")"<< x.Mat[i][j];   
      }
      return s;
    

    Das ist ein Teil aus meiner Einlese - Funktion. Ich lese dabei eine PGM Datei in die Matrix x ein.
    Mittels Debug Ausgaben habe ich herausgefunden, dass die Daten x.Zeil und X.Spalt (Dimension der Matrix) sauber übergeben werden. Dennoch ruft mein Programm den Eintrag (0,158) auf, obwohl die Forschleife ja offenbar nur bis x.spalt geht (in diesem Beispiel 157). Ich kann mir diesen Fehler einfach überhaupt gar nicht erklären. Hat hier vielleicht jemand ne Idee, woran es liegen könnte? Oder hatte schon mal jemand ein ähnliches Problem? NAbei der komplette wichtige Quellcode:

    class GreyScale
    {
      private:
        float** Mat;                          // Zeiger auf Feld fuer Matrixelemente
        int     Zeil,Spalt,farbtiefe;                        // Matrixlaenge
    
      public:
        GreyScale  (int m=0,int n=0);                    // Konstruktor mit Laengenangabe
        ~GreyScale () { delete[] Mat; }          // Destruktor
        GreyScale  (const GreyScale&);              // Kopierkonstruktor
    
        float& operator () (int,int);            // Matrixelement schreiben
        float  operator () (int,int) const;      // Matrixelement lesen
    
        GreyScale& operator =  (const GreyScale&);  // Zuweisung
        GreyScale& operator += (const GreyScale&);  // Zuweisungen mit arithm. Operation
        GreyScale& operator -= (const GreyScale&);
        GreyScale& operator *= (float);
        GreyScale& operator /= (float);
    
        GreyScale& Resize   (int,int);                      // neue Laenge festlegen
        int     GetHeight  () const { return Zeil; }    // Zeilenanzahl
        int     GetWidth  () const { return Spalt; }    // Zeilenanzahl
        int     GetDeepth  () const { return farbtiefe; }    // Zeilenanzahl
    
        static void MatFehler (const char str[]);   // Fehlermeldung ausgeben
    
        friend GreyScale   operator +  (const GreyScale&, const GreyScale&); // Addition
        friend GreyScale   operator -  (const GreyScale&, const GreyScale&); // Subtraktion
        friend GreyScale   operator -  (const GreyScale&);                // Vorzeichen
    
        friend GreyScale   operator *  (const GreyScale&, const GreyScale&); // Matrixprodukt
        friend GreyScale   operator *  (double, const GreyScale&);        // Vielfache
        friend GreyScale   operator *  (const GreyScale&, float);
        friend GreyScale   operator /  (const GreyScale&, float);
    
        friend bool     operator == (const GreyScale&, const GreyScale&); // Vergleich
        friend bool     operator != (const GreyScale&, const GreyScale&);
    
        friend istream& operator >> (istream&, GreyScale&);            // Eingabe
        friend ostream& operator << (ostream&, const GreyScale&);      // Ausgabe
    
    };
    
    GreyScale::GreyScale (int m,int n)
    {
      if (m<0)
        MatFehler("Nur Matrizen mit positiver Zeilenanzahl!");
      if (n<0)
        MatFehler("Nur Matrizen mit positiver Spaltenanzahl!");    
    
      Zeil=m;
      Spalt=n;
    
      Mat=new float*[m];
      for(int i=0; i<m; i++)
        Mat[i]=new float [n];
      if (Mat==NULL)
        MatFehler("Nicht genuegend Speicher!");
    
      for (int i=0; i<m; i++)
        for(int j=0; j<n; j++)
          Mat[i][j]=0;
    }
    
    GreyScale& GreyScale::Resize (int m, int n)
    {        
      delete[] Mat;      
      if (m<0)
        MatFehler("Nur Matrizen mit positiver Zeilenanzahl!");
      if (n<0)
        MatFehler("Nur Matrizen mit positiver Spaltenanzahl!");    
    
      Zeil=m;
      Spalt=n;
    
      Mat=new float*[m];
      for(int i=0; i<m; i++)
        Mat[i]=new float [n];
      if (Mat==NULL)
        MatFehler("Nicht genuegend Speicher!");
    
      for (int i=0; i<m; i++)
        for(int j=0; j<n; j++)
          Mat[i][j]=0;
      return *this;    
    }
    
    istream& operator >> (istream &s, GreyScale &x)
    {
      char b;
      b=s.get();
      cerr << b; 
      if (b != 'P') GreyScale::MatFehler("Falsches Dateiformat!");
      b=s.get();
      cerr << b; 
      if (b != '2') GreyScale::MatFehler("Falsches Dateiformat!"); 
      s >> ws;
      while( s.peek() == '#')
      {
        s.ignore( 10000, '\n');  // ignore characters until line end
        s >> std::ws;            // eat whitespaces
      }
      s >> x.Spalt;
      cerr << x.Spalt;
      s >> x.Zeil; s >> ws;
      cerr << x.Zeil;
      s >> x.farbtiefe; 
      cerr << x.farbtiefe;
      s >> ws;
    
      x.Resize(x.Zeil,x.Spalt);   
     // cout << setiosflags(ios::right);
      for (int i=0; i<x.Zeil; i++)
        for (int j=0; j<x.Spalt; j++)
      {
        s >> ws;
        s >> x.Mat[i][j];
        x.Mat[i][j] = x.Mat[i][j] / x.farbtiefe;
        s >> ws;
        cout <<" ("<<i<<","<<j<<")"<< x.Mat[i][j];   
      }
      return s;
    }
    

    Danke schon mal!



  • http://www.c-plusplus.net/forum/viewtopic-var-t-is-120832.html
    Auch wenn du nicht VC 6 hast kannst du da was lernen


Anmelden zum Antworten