C++ 在析构函数之后获得分段错误
我正在做一个小文件读取和数据验证程序,作为我的TAFE(一所高等院校)课程的一部分,这包括检查和验证日期 我决定最好使用一个单独的类,而不是将它集成到我的主驱动程序类中 问题是,在我的测试程序运行之后,我遇到了一个分段错误(内核转储)。正如我所知,错误发生在程序终止时,在调用析构函数后弹出。到目前为止,我还没有找到这个错误的原因,我希望一些开明的灵魂能告诉我我的错误 日期:hC++ 在析构函数之后获得分段错误,c++,segmentation-fault,destructor,C++,Segmentation Fault,Destructor,我正在做一个小文件读取和数据验证程序,作为我的TAFE(一所高等院校)课程的一部分,这包括检查和验证日期 我决定最好使用一个单独的类,而不是将它集成到我的主驱动程序类中 问题是,在我的测试程序运行之后,我遇到了一个分段错误(内核转储)。正如我所知,错误发生在程序终止时,在调用析构函数后弹出。到目前为止,我还没有找到这个错误的原因,我希望一些开明的灵魂能告诉我我的错误 日期:h #ifndef DATE_H #define DATE_H #include <string> using
#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
#如果取消日期#
#定义日期
#包括
使用std::string;
#包括
使用std::stringstream;
#包括
使用std::exit;
#包括
使用std::cout;
使用std::endl;
上课日期{
公众:
明确日期();
~date();
bool-before(字符串dateIn1,字符串dateIn2);
中间的整数年(字符串dateIn1,字符串dateIn2);
bool是有效的(字符串dateIn);
bool getDate(int date[],字符串dateIn);
布尔群岛年(国际年);
私人:
整数天[];
};
#恩迪夫
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);
}
#包括“date.h”
日期::日期(){
天数[0]=31天;
天数[1]=28天;
天数[2]=31天;
天数[3]=30天;
天数[4]=31天;
天数[5]=30天;
天数[6]=31天;
天数[7]=31天;
天数[8]=30天;
天[9]=31天;
天[10]=30;
天[11]=31;
}
bool date::before(字符串dateIn1,字符串dateIn2){
int date1[3];
int date2[3];
getDate(日期1,日期1);
getDate(日期2,日期2);
如果(日期1[2]<日期2[2]){
返回true;
}否则如果(日期1[1]<日期2[1]){
返回true;
}else if(日期1[0]<日期2[0]){
返回true;
}
返回false;
}
日期::~date(){
cout date2[1]){
年--;
}
如果((date1[1]==date2[1])&&(date1[0]>date2[1])){
年--;
}
回归年;
}
bool date::isValid(字符串dateIn){
国际日期[3];
if(getDate(日期,dateIn)){
如果(日期[1]日期[0]){
如果(p2>>日期[1]){
返回(p3>>日期[2]);
}否则{
返回false;
}
返回false;
}
}
bool日期::isLeapYear(整数年){
回报率((第%4年)=0);
}
最后是测试程序
#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";
}
#包括
使用std::cout;
使用std::endl;
#包括“date.h”
int main(){
日期d;
库特
这是非标准扩展。必须为数组指定大小,例如:
static const MonthCount = 12;
int days[MonthCount];
实际使用数组。否则您将使用“零大小数组”(非标准!)。每次使用当前数组的任何元素时,您的程序都会在内存上运行。更改:
private:
int days[];
致:
问题是,您从未实际初始化类型date
中的days
字段。这意味着在构造函数中设置值时,您正在访问未初始化的内存
您需要以某种方式显式初始化days
值。最简单的修复方法是对类型使用vector
,或者将数组大小硬编码为12
private:
int days[12];
或
private:
std:病媒日;
...
日期::日期(){
天。推回(31);
天。推回(28);
...
}
我同意前面对这个问题的回答,但我想补充一下正确性的理由:
每当您试图访问不允许访问的内存时,就会导致分段错误
您不允许访问“天[0]”到“天”[11]”,因为计算机没有给“天[]”变量,您声明的内存足够容纳任何元素,因此当您尝试访问这些元素时,它抛出了一个segfault
任何使用“new”运算符声明的非变量都放置在“堆栈”上这是计算机分割出来供程序使用的连续内存块。为了保持堆栈中存储的所有内容连续,计算机只会在您请求时提供所需的内存量,因此,例如,如果您请求创建int,它只会提供足够的memo要存储单个int
当您在几天内写这行时[];计算机尝试评估它需要多少内存,评估为空数组,并为您提供足够的内存来存储所述空数组。由于计算机没有为您的数组提供超出空数组所需的任何额外空间,因此它知道您试图访问该数组中的内存尚未分配给它o它抛出了一个分段错误并崩溃
如果您尚未了解“堆栈”和“堆”在你的计算机科学课上,如果这有点让人不知所措,那么很抱歉,但我可能把事情搞得太复杂了,我想你很快会的。你没有说你在使用哪个编译器,但是如果我使用带有-Wall
和-pedantic
标志的g++编译这段代码:
struct S {
int a[];
};
int main() {
S s;
}
我收到了警告信息:
warning: ISO C++ forbids zero-size array 'a'
寓意是,您应该始终使用尽可能多的编译器警告进行编译-这可以节省大量时间并生成更正确的代码。好的,很酷,感谢您的回答,它们很有效,huzzah。我感谢大家,我可以指出,isLeapYear
的实现是错误的吗?1900
不是一次飞跃ear.Read:+1鼓励使用-Wall
。编译器是开发人员最好的朋友,应该注意它的咆哮!感谢所有kingly的帮助,非常感谢。
private:
std:vector<int> days;
...
date::date() {
days.push_back(31);
days.push_back(28);
...
}
struct S {
int a[];
};
int main() {
S s;
}
warning: ISO C++ forbids zero-size array 'a'