Getting segmentation error after destructor

I am doing a small data reading and verification program as part of my TAFE (Tertiary College) course, including date validation and verification.

I decided that it was best to do this with a separate class, rather than integrating it into my main class.

The problem is that I get a segmentation error (the kernel is reset) after running my test program. Nearby, as far as I can tell, an error occurs when the program terminates, popping up after calling the destructor. Until now, I have not been able to find the cause of this error, and I hoped that some enlightened soul could show me the error of my ways.

date.h

#ifndef DATE_H #define DATE_H #include <string> using std::string; #include <sstream> using std::stringstream; #include <cstdlib> using std::exit; #include <iostream> using std::cout; using std::endl; class date { public: explicit date(); ~date(); bool before(string dateIn1, string dateIn2); int yearsBetween(string dateIn1, string dateIn2); bool isValid(string dateIn); bool getDate(int date[], string dateIn); bool isLeapYear(int year); private: int days[]; }; #endif 

date.cpp

 #include "date.h" date::date() { days[0] = 31; days[1] = 28; days[2] = 31; days[3] = 30; days[4] = 31; days[5] = 30; days[6] = 31; days[7] = 31; days[8] = 30; days[9] = 31; days[10] = 30; days[11] = 31; } bool date::before(string dateIn1, string dateIn2) { int date1[3]; int date2[3]; getDate(date1, dateIn1); getDate(date2, dateIn2); if (date1[2] < date2[2]) { return true; } else if (date1[1] < date2[1]) { return true; } else if (date1[0] < date2[0]) { return true; } return false; } date::~date() { cout << "this is for testing only, plox delete\n"; } int date::yearsBetween(string dateIn1, string dateIn2) { int date1[3]; int date2[3]; getDate(date1, dateIn1); getDate(date2, dateIn2); int years = date2[2] - date1[2]; if (date1[1] > date2[1]) { years--; } if ((date1[1] == date2[1]) && (date1[0] > date2[1])) { years--; } return years; } bool date::isValid(string dateIn) { int date[3]; if (getDate(date, dateIn)) { if (date[1] <= 12) { int extraDay = 0; if (isLeapYear(date[2])) { extraDay++; } if ((date[0] + extraDay) <= days[date[1] - 1]) { return true; } } } else { return false; } } bool date::getDate(int date[], string dateIn) { string part1, part2, part3; size_t whereIs, lastFound; whereIs = dateIn.find("/"); part1 = dateIn.substr(0, whereIs); lastFound = whereIs + 1; whereIs = dateIn.find("/", lastFound); part2 = dateIn.substr(lastFound, whereIs - lastFound); lastFound = whereIs + 1; part3 = dateIn.substr(lastFound, 4); stringstream p1(part1); stringstream p2(part2); stringstream p3(part3); if (p1 >> date[0]) { if (p2>>date[1]) { return (p3>>date[2]); } else { return false; } return false; } } bool date::isLeapYear(int year) { return ((year % 4) == 0); } 

And finally, a test program

 #include <iostream> using std::cout; using std::endl; #include "date.h" int main() { date d; cout << "1/1/1988 before 3/5/1990 [" << d.before("1/1/1988", "3/5/1990") << "]\n1/1/1988 before 1/1/1970 [" << d.before("a/a/1988", "1/1/1970") <<"]\n"; cout << "years between 1/1/1988 and 1/1/1998 [" << d.yearsBetween("1/1/1988", "1/1/1998") << "]\n"; cout << "is 1/1/1988 valid [" << d.isValid("1/1/1988") << "]\n" << "is 2/13/1988 valid [" << d.isValid("2/13/1988") << "]\n" << "is 32/12/1988 valid [" << d.isValid("32/12/1988") << "]\n"; cout << "blerg\n"; } 

I left in some extraneous cout operations that I used to try to find the error.

I thank you in advance.

+6
c ++ segmentation-fault destructor
source share
5 answers

Edit:

 private: int days[]; 

in

 private: int days[12]; 
+3
source share

The problem is that you never initialize the days field in the date type. This means that when you set values โ€‹โ€‹in the constructor, you get access to uninitialized memory.

You need to explicitly initialize the days value in some way. The easiest fix is โ€‹โ€‹to use vector for the type or hard-code the array size to 12.

 private: int days[12]; 

or

 private: std:vector<int> days; ... date::date() { days.push_back(31); days.push_back(28); ... } 
+3
source share

You do not say which compiler you are using, but if I compile this code using g ++ with the -Wall and -pedantic :

 struct S { int a[]; }; int main() { S s; } 

I get a warning message:

 warning: ISO C++ forbids zero-size array 'a' 

The moral is that you should always compile using as many compiler warnings as possible - this can save you time and lead to more correct code.

+2
source share
 int days[]; 

This is a non-standard extension. You must specify the size of the array, for example:

 static const MonthCount = 12; int days[MonthCount]; 

Actually have an array to use. Otherwise, you have a "zero size array" (not standard!). Your program thromboses from memory every time you use any element of your current array.

+1
source share

I agree with the previous answers to this question, but I would add a justification for their correctness:

Segmentation errors are triggered whenever you try to access memory that you do not have access to.

http://en.wikipedia.org/wiki/Segmentation_fault

You were not allowed to "days" [0] "in days" [11] "because the computer did not give the variable" days [] "that you declared enough memory to store any elements, so when you tried to access the specified elements he chose segfault.

Any variables not declared using the "new" operator are pushed onto the "stack", which is a continuous piece of memory that the computer has allocated for use by the program. So that all the content stored on the stack is continuous, the computer will provide exactly the amount of memory that is required for use when you request it, so if you want to create an int, for example, it will give you enough memory to store this single int .

When you wrote the line int days []; the computer tried to estimate how much memory it would require, rated it as an empty array and gave you enough memory to store the specified empty array. Since the computer did not provide your array with additional space beyond what was necessary for the empty array, it knew that the memory you were trying to access in this array was not assigned to it, so it reset the segmentation error and crashed.

If you have not yet learned about the โ€œstackโ€ and the โ€œheapโ€ in your computer science class, then I'm sorry if this is a little overwhelming, but I'm probably too complicated, and I think you will most likely be soon.

+1
source share

All Articles