!!!!Core Dumped error,help!!!!!!

Im writing a program that takes different txt files with different data type each, text1.txt is int, text2.txt is double, etc, using templates so i can use the same function with the different data type, and calculate some statistical operations.

I allocated a dynamic array, but i thing im having issues with when i should dealocate. In my computer the program sometimes runs completely and sometimes no. But when i use my schools server gives me this error
1
2
corrupted size vs. prev_size
Aborted (core dumped)


Help please

this is the header file

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
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
using namespace std;

template <class myType>
class statsType{

    int setSize;
    myType *dataSet;
    string fileName;

public:

    statsType(){ //set the filename to the empty string, the setSize to 0, and dataSet pointer to nullptr.

    fileName = "";
    setSize = 0;
    dataSet = nullptr;

}
    statsType(string fileName){

        this->fileName = fileName;
        setSize = 0;
        dataSet = nullptr;
        setFileName(fileName);

    setFileName();
}
   ~statsType(){
        dataSet = nullptr;
        setSize = 0;
        fileName = "";
        delete [] dataSet;
}
    string getFileName()const{
        return fileName;
}
    bool setFileName(string fileName){

        if(fileName.size() > 4 && (fileName.substr(fileName.size() - 3) == "dat" )|| fileName.substr(fileName.size() - 3) == "txt") {

            this->fileName= fileName;

            return true;
            }

        else {

            cout << "File does not exist"<< endl;

        return false;

    }
    }


    bool readDataFile(){

        ifstream inFile ;
        inFile.open(fileName);

        inFile.ignore(1000, '\n');
        inFile.ignore(1000, '\n');
        inFile.ignore(1000, '\n');
        inFile.ignore(1000, '\n');
        inFile >> setSize;
        dataSet = new myType[setSize];

        for(int i =1; i <= setSize; i++) {
            inFile >>dataSet[i];
        }

        //delete [] dataSet;
        inFile.close();


}

    int getSetSize() const{ //returns the current set size.

        return setSize;
    }




    void printDataSet() const{ //printDataSet() function should print data items of type myType with nine (9) values per line.

        for (int i = 1; i <= setSize; i++) {

            cout <<setw(5)<<dataSet[i] << " ";
            if(i%9 ==0)
                cout << endl;
        }
        cout<<endl;

}


};




   



this is the main
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
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>

#include "statsType.h"

using namespace std;

int main()
{
// **********************************************************
//  Initalizations and headers

	string	bars, stars;
	bars.append(50,'-');
	stars.append(70,'=');

	cout << endl << "Assignment #7 - Statistics Testing" << endl << endl;

// **********************************************************
//
// **********************************************************
//  Some tests for the statsType class for integer type.

	statsType <int> set1;
	int	min1, max1, med1, sum1, ave1;
	int	sStd1, pStd1, sk1, fp1;

	cout << stars << endl;
	cout << "Data Set 1 - Integers" << endl << endl;

	set1.setFileName("dataFile1.txt");
	set1.readDataFile();

	cout << "Data Set 1 -> Unsorted:" << endl;
	set1.printDataSet();



// **********************************************************
//  Some tests for the statsType class for double type.

	statsType <float> set2;
	float	min2, max2, med2, sum2, ave2;
	float	sStd2, pStd2, sk2, fp2;

	cout << stars << endl;
	cout << "Data Set 2 - float" << endl << endl;

	set2.setFileName("dataFile2.txt");
	set2.readDataFile();
        set2.printDataSet();





// **********************************************************
//  Some tests for the statsType class for long double type.

	statsType <double> set3;
	double	min3, max3, med3, sum3, ave3;
	double	sStd3, pStd3, sk3, fp3;

	cout << stars << endl;
	cout << "Data Set 3 - double" << endl << endl;


        set3.setFileName("dataFile3.txt");
	set3.readDataFile();
	set3.printDataSet();




// **********************************************************
//  Some tests for the statsType class for long double type.

	statsType <long long> set4;
	long long	min4, max4, med4, sum4, ave4;
	long long	sStd4, pStd4, sk4, fp4;

	cout << stars << endl;
	cout << "Data Set 4 - long long" << endl << endl;


	set4.setFileName("dataFile4.txt");
	set4.readDataFile();
        set4.printDataSet();


//  Some tests for the statsType class for long double type.

	statsType <long double> set5;
	long double	min5, max5, med5, sum5, ave5;
	long double	sStd5, pStd5, sk5, fp5;

	cout << stars << endl;
	cout << "Data Set 5 - long double" << endl << endl;


	set5.setFileName("dataFile5.txt");
	set5.readDataFile();
        set5.printDataSet();


	return 0;
}
Last edited on
You are setting dataSet to null before using it in the delete statement!
Usually we wait until after we delete the allocated memory before nulling the pointer.

You're also writing outside of the array. Don't you know that array indices start at 0 and go the the size - 1 ? The actual size is out-of-bounds (more specifically, one-past-the-end).

 
        for(int i = 1; i <= setSize; i++)  // should be for( int i = 0; i < setSize; ++i ) 

Last edited on
Thanks, going outside the array was causing the problem
Now works perfect
You can simplify your main to something like this:

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
template<typename T>
void test(int n, const string& type_name)
{
    static const string stars(70, '=');
    //T min1, max1, med1, sum1, ave1;
    //T sStd1, pStd1, sk1, fp1;

    string filename = "dataFile" + to_string( n ) + ".txt";
    statsType< T > set1( filename );
    set1.readDataFile();

    cout << stars << "\nData Set " << n << " - " << type_name << '\n';
    cout << "Data Set " << n << " -> Unsorted:\n";
    set1.printDataSet();
}

#define TEST( n, t ) test< t >( n, #t )

int main()
{
    cout << "\nAssignment #7 - Statistics Testing\n\n";
    TEST( 1, int );
    TEST( 2, float );
    TEST( 3, double );
    TEST( 4, long long );
    TEST( 5, long double );
}

I have another issue, when i get the data from the float txt i need to look for the min and max, but it is finding the number as an int, so, instead of 2.02 , im getting 2.000

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

/*i get the data and print it out the same as above, and it shows the numbers
 with the precision i need, it shows 2.020000, but the min and max function only 
gets 2.000, the same with max it should  17630.970703 not 17630.0000*/

    void getLimits(myType &min, myType &max) const{ //find and return the minimum and maximum values in the set (via reference).


        min = dataSet[0];
        max = dataSet[0];
        for(int i=1; i<setSize-1; i++){
            if(max < dataSet[i] ) {
                max = dataSet[i];
            }

            else if(min > dataSet[i]){
                min=dataSet[i];
            }
        }

    }


 bool readDataFile(){

        ifstream inFile ;
        inFile.open(fileName);

        inFile.ignore(1000, '\n');
        inFile.ignore(1000, '\n');
        inFile.ignore(1000, '\n');
        inFile.ignore(1000, '\n');
        inFile >> setSize;
        dataSet = new myType[setSize];

        for(int i =1; i <= setSize; i++) {
            inFile >>dataSet[i];
        }

       
        inFile.close();


}

 void printDataSet() const{ //printDataSet() function should print data items of type myType with nine (9) values per line.

        for (int i = 0; i < setSize-1; i++) {

            cout <<setw(5)<<dataSet[i] << " ";
            if((i+1)%9 ==0)
                cout << endl;
        }
        cout<<endl;

}


this is how im calling the functions in main
1
2
3
4
5
6
7
8
9
10

statsType <float> set2;
float	min2, max2, med2, sum2, ave2;
float	sStd2, pStd2, sk2, fp2;

set2.getLimits(min2, max2);

cout << "   Minimum:            " << min2 << endl;
cout << "   Maximum:            " << max2 << endl;
Last edited on
Fix your array bounds, man! They're all wrong.

In readDataFile you're accessing from dataSet[1] to dataSet[setSize]; the latter is out-of-bounds.
i needs to go from 0 to setSize - 1, which we canonically write like this:

 
    for( int i = 0; i < setSize; ++i ) // i will go from 0 to setSize - 1 

So not i <= setSize or i < setSize - 1.

As for your problem of truncation of your floating point values, I don't see offhand what's happening there. You need to post a complete program and an input file that demonstrates the problem.
Last edited on
Topic archived. No new replies allowed.