EXC BAD ACCESS when filling private array in class member function??

Hey all,

I'm a rookie to c++ and programming in general, so please forgive if I use improper lingo. Anywho... I have been stuck for two days debugging a programming project and cannot figure out what I'm doing wrong.

An incomplete set of code was provided by our professor for us to fill. The class declaration (which was provided and can't be modified):
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
class Date {
private:
	int monthValue;
	int dayValue;
	int yearValue;
	
	static const int MONTHS = 13;
	
	typedef enum {
		EMPTY = 0,
		JAN = 31,
		FEB = 28,
		MAR = 31,
		APR = 30,
		MAY = 31,
		JUN = 30,
		JUL = 31,
		AUG = 31,
		SEP = 30,
		OCT = 31,
		NOV = 30,
		DEC = 31
	};
	
	int daysPerMonth[MONTHS]; 
	
	bool IsLeap() const;
	
public:
	Date();
	
	Date(const int month,
		 const int day,
		 const int year);
	
        // Initializes the daysPerMonth[] array to EMPTY, JAN, FEB etc.
	// Validates the date and sets the object.
	void SetDate(const int month, const int day, const int year);
	
	// <Other class member functions...>
};


My problem is coming up when i use dayNum = dayNum + daysPerMonth[monthNum]; in one of Date's member functions.

I get gdb message "EXC_BAD_ACCESS" on this line of code. I realize that I'm doing something wrong memory management-wise, but we haven't covered much about memory, so I think there should be a simple fix that doesn't require alloc() (until later in the semester). I'm thinking the error is in the setter, where the assignment requires us to initialize the array to the enumerated set of days per months. Here's my best attempt:
1
2
3
4
5
6
7
8
9
10
11
void Date::SetDate(const int month,
			 const int day,
			 const int year)
{
	int daysPerMonth[MONTHS] =
	{EMPTY, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
	
	// <Non-problamatic code block>
        //
        //
}


I made various other attempts to change the SetDate code, all resulting in the same error or new syntax errors.

Thanks in advance for any explanation you can give for why this code fails.

-John
Last edited on
dayNum = dayNum + daysPerMonth[monthNum];

Can we have the code snippet that contain above code ? Try to print the value of monthNum before you execute the above statement. This is to ensure you are passing in a "valid" index for daysPerMonth array.
1
2
3
4
5
6
7
8
9
10
// Returns the day of the year (1-366) the date falls on.
int Date::DayOfYear() const
{
	int dayNum = 0;
	int monthNum = 1;
	
	while (monthNum < monthValue) {
		dayNum = dayNum + daysPerMonth[monthNum];
		monthNum++;
	}


Immediately before the program enters the while loop, dayNum does hold zero, and monthNum holds 1, verified by the xcode debugger.
Ok I also narrowed the problem down. The values in the daysPerMonth array are garbage. Since DayOfYear() is a member function, why isn't it being passed the array, or was the array never filled correctly?
1
2
3
4
5
6
7
8
9
10
11
void Date::SetDate(const int month,
			 const int day,
			 const int year)
{
	int daysPerMonth[MONTHS] = //here you declare ANOTHER daysPerMonth array that is local to this method!!!!!
	{EMPTY, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
	
	// <Non-problamatic code block>
        //
        //
}


I believe you want to populate the daysPerMonth that is inside your class is it ? That is why for some of us, we name out data members like m_daysPerMonth. The m_ prefix remind us we are updating or reading our class data members.

The fix is simple.
1
2
3
4
5
6
7
8
9
void Date::SetDate(const int month,
			 const int day,
			 const int year)
{
	daysPerMonth[0] = Date::EMPTY; 
        daysPerMonth[1] = Date::JAN;
        //fill up the rest yourself
	
}
Topic archived. No new replies allowed.