Naruszenie ochrony pamięci

0

Witam

Mam programik który przechodzi kompilacje elegancko, ale przy uruchomieniu wyskakuje błąd ochrony pamięci.

Program ładuje plik zewnętrzny dane.dat który zawiera liczbę generatorów i przedziały.

Kod programu:


#include <fstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <omp.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <math.h>

using namespace std;

void show( struct timeval *ti ) {
   struct timeval tf;
   static struct timeval tl = { 0,0 };

  gettimeofday( &tf, NULL ); 

   cout << "- - - - -Od startu programu: - -  " << ( tf.tv_sec - ti->tv_sec + ( tf.tv_usec - ti->tv_usec ) * 0.000001 ) << " s - - - " ; 

    cout << endl;
}

unsigned int generatorCount; // liczba_generatorow
unsigned int rangeCount; // liczba przedziałów
class Range {
private:
    float const begin;
    float const end;
public:
    Range(float begin, float end) :
            begin(begin), end(end) {
    }
    void print() {
        cout << "Range: [" << begin << "; " << end << "]" << endl;
    }
    bool isInRange(double d) {
        return d >= begin && d <= end;
    }

    float getBegin() const {
        return begin;
    }

    float getEnd() const {
        return end;
    }
};
class GaussianGenerator {
private:
    const float mean;
    const float stdDev;
    const int numberCount;
    bool hasNext;
    double next;
public:
    GaussianGenerator(float average, float variance, int numberCount) :
            mean(average), stdDev(sqrt(variance)), numberCount(numberCount) {
        this->hasNext = false;
        this->next = 0.;
    }
    ~GaussianGenerator() {

    }
    void print() {
        cout << "Generator: " << mean << " " << stdDev << " " << numberCount
                << endl;
    }
    double getGaussian() {
        if (hasNext) {
            hasNext = false;
            return next * stdDev + mean;
        } else {
            double u, v, s;
            do {
                u = ((double) rand() / (double) RAND_MAX) * 2 - 1;
                v = ((double) rand() / (double) RAND_MAX) * 2 - 1;
                s = u * u + v * v;
            } while (s >= 1 || s == 0);
            double mul = sqrt(-2.0 * log(s) / s);
            next = v * mul;
            hasNext = true;
            return mean + stdDev * u * mul;
        }
    }

    const int getNumberCount() const {
        return numberCount;
    }

};

vector<GaussianGenerator*> generatorVector;
vector<Range*> rangeVector;

int* createRangeHits(GaussianGenerator *g, vector<Range*>& rangeVector) {
    int* result = new int[rangeVector.size()];

for (unsigned int j = 0; j < rangeVector.size(); j++) {
        result[j] = 0;
    }
    double d;

    for (int i = 0; i < g->getNumberCount(); i++) {
        d = g->getGaussian();
//      cout << "d = " << d << endl;

for (unsigned int j = 0; j < rangeVector.size(); j++) {
            if (rangeVector.at(j)->isInRange(d)) {
                result[j]++;
            }
        }

    }

        return result;

}
int main() {
cout<<"----------------------------------------"<<endl;
cout<<"Start"<<endl;
cout<<"----------------------------------------"<<endl;

struct timeval ti;
gettimeofday( &ti, NULL );
    srand(time(NULL));
    ifstream inFile("dane.dat");
    if (!inFile || !inFile.good()) {
        cout << endl << "Invalid input data!";
        return 1;
    }
    else{cout <<"Loading data from file - - > OK!"<<endl;
    cout<<"----------------------------------------"<<endl;}
    float f1; // srednia, poczatek przedzialu;
    float f2; // wariancja, koniec przedzialu;
    int numberCount; // numberCount numberCount
    inFile >> generatorCount;
    for (unsigned int i = 0; i < generatorCount; i++) {
        inFile >> f1;
        inFile >> f2;
        inFile >> numberCount;
        GaussianGenerator *g = new GaussianGenerator(f1, f2, numberCount);
        generatorVector.push_back(g);

    }

    for (unsigned int i = 0; i < generatorVector.size(); i++)generatorVector.at(i)->print();
    int totalRangeHits[rangeCount];
    inFile >> rangeCount;

    for (unsigned int i = 0; i < rangeCount; i++) {
        inFile >> f1;
        inFile >> f2;
        Range* r = new Range(f1, f2);
        rangeVector.push_back(r);
        totalRangeHits[i] = 0;
    }

    for (unsigned int i = 0; i < rangeVector.size(); i++)rangeVector.at(i)->print();

    for (unsigned int i = 0; i < generatorVector.size(); i++) {
        generatorVector.at(i);
        int* rangeHits = createRangeHits(generatorVector.at(i), rangeVector);

    for (unsigned int i = 0; i < rangeCount; i++) {
            totalRangeHits[i] += rangeHits[i];
        }
    }

    for (unsigned int i = 0; i < rangeCount; i++) {
        cout << (i + 1) << " " << rangeVector.at(i)->getBegin() << " "
                << rangeVector.at(i)->getEnd() << " " << totalRangeHits[i]
                << endl;
    }
    cout << endl;
show( &ti );
cout << endl;
cout << endl;

cout<<"---------------------------------------------------------------------------------------------"<<endl;
//  system("pause");
    return 0;
}

Kod dla pliku dane.dat:


30

10.0 5.0 1000000

50.0 2.0 1000000

-8.0 1.0 1000000

10.0 5.0 1000000

50.0 2.0 1000000

-8.0 1.0 1000000

10.0 5.0 1000000

50.0 2.0 1000000

-8.0 1.0 1000000

10.0 5.0 1000000

50.0 2.0 1000000

-8.0 1.0 1000000

10.0 5.0 1000000

50.0 2.0 1000000

-8.0 1.0 1000000

10.0 5.0 1000000

50.0 2.0 1000000

-8.0 1.0 1000000

10.0 5.0 1000000

50.0 2.0 1000000

-8.0 1.0 1000000

10.0 5.0 1000000

50.0 2.0 1000000

-8.0 1.0 1000000

10.0 5.0 1000000

50.0 2.0 1000000

-8.0 1.0 1000000

10.0 5.0 1000000

50.0 2.0 1000000

-8.0 1.0 1000000

25

8.881966011250105  11.118033988749895

7.76393202250021  12.23606797749979

6.645898033750315  13.354101966249685

5.52786404500042  14.47213595499958

4.4098300562505255  15.590169943749475

3.2917960675006306  16.70820393249937

1.0557280900008408  18.94427190999916

-1.180339887498949  21.18033988749895

49.292893218813454  50.707106781186546

48.58578643762691  51.41421356237309

47.878679656440355  52.121320343559645

47.17157287525381  52.82842712474619

46.46446609406726  53.53553390593274

45.757359312880716  54.242640687119284

44.34314575050762  55.65685424949238

42.928932188134524  57.071067811865476

-8.5  -7.5

-9.0  -7.0

-9.5  -6.5

-10.0  -6.0

-10.5  -5.5

-11.0  -5.0

-12.0  -4.0

-13.0  -3.0

-1000.0 1000.0

Gdzie jest ewentulany błąd zapisu do pamięci?
Z góry dziękuję za pomoc.

1

Instrukcje nie działają wstecz:

        int totalRangeHits[rangeCount];
        inFile >> rangeCount;

Po pierwsze to działa tylko w C99, po drugie rozmiar tablicy będzie taki jaki miała zmienna rangeCount przed wczytaniem z pliku.

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0