CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Memory Fault.

  1. #1
    Join Date
    Oct 2006
    Posts
    1

    Memory Fault.

    Hey everyone, first time poster on the forum. I'm in need of some help. I'm writing an appointment book console application. I have two classes the appointment class which stores the member variables of the time, date, and description of the appointment, and I have the appointmentbook class which has all the functionality in it. I have created a dynamic array of the appointment class, and one of the functions of the program is to be able to sort the appointments by date in the array. I keep getting a memory fault when my sort is executed and im not sure why. My add function with the sort in it looks like this

    Code:
    void AppointmentBook::add(string date, string time, string description) //add function with arguments date,time,description of type string passed from the main.cc
    	{
    		bool check = true;
    		y = 0;
    		if (num == 0)
    		{
    			appt[num].setDate(date); //call the setDate function from the current instance of the appt object
    			appt[num].setTime(time);//call the setTime function from the current instance of the appt object
    			appt[num].setDescription(description);//call the setDescription function from the current instance of the appt object
    			cout << appt[y].getDate();
    		}
    		else{
    			while (check){
    				if (date > appt[y].getDate()){
    					y++;
    				}
    				else{
    					check = false;
    				}
    			}
    	
    			for(int g = num-1; g <= y; g--){
    				appt[g+1].setDate(appt[g].getDate());
    				appt[g+1].setTime(appt[g].getTime());
    				appt[g+1].setDescription(appt[g].getDescription());
    			}
    			appt[y].setDate(date);
    			appt[y].setTime(time);
    			appt[y].setDescription(description);
    		}
    	num++;
    	}
    num is the variable that keeps track of the number of appointments in the array, and so if what i think is correct, my code does this...

    the add function is passed a new appointment, it checks to see if num == 0 which it is since its the first, so it sets appt[0]'s member variables and increments num. next appointment that is added it calls the add function and goes to the else{} block of code. It evaluates whether date is greater than appt[0], and if it is it adds one to y making y = 1 or keeps y = 0. When date is no longer > appt[y].getDate() it sets check to false and exits the loop. it is here however in the appt[y].getDate() that it incurs its memory fault. Is that because when there is only [0] set in the array and it is greater it looks to find appt[1] which doesnt exist yet? do i need to add a if (y >= num){check = false;}? Thank you so much for your time! - Jeff

  2. #2
    Join Date
    Jan 2003
    Posts
    615

    Re: Memory Fault.

    Please post AppointmentBook and Appointment header file.

  3. #3
    Join Date
    Oct 2002
    Location
    Tel Aviv
    Posts
    149

    Re: Memory Fault.

    Where do you allocate memory for appt?

  4. #4
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Memory Fault.

    Code:
    bool check = true;
    y = 0;
    if (num == 0)
    {
        appt[num].setDate(date); 
        appt[num].setTime(time);			
        appt[num].setDescription(description);
        cout << appt[y].getDate();
    }
    else
    {
        while (check)
        {
            if (date > appt[y].getDate())
            {
                 y++;
            }
            else
            {
                check = false;
            }
        // continue to set the details
    }
    appt appears to be a vector (or array) or something. What is the dimension of this array. In the while loop, you increase this dimension while a condition occurs (date > appt[y].getDate f() ) but what if this condition occurs for every item in your collection? Then you will go off the end.

    By the way, a small issue of design: it seems the date, time and description are closely linked because you are setting and getting the 3 together. So put them into one struct (or class). If that is what appt already is, then consider using constructors and copy constructors eg perhaps appt[g+1] = appt[g] would be appropriate. Maybe your function should take const Appointment & as a parameter thus:

    Code:
    void AppointmentBook::add( const Appointment & appt )
    Your function does not, by the way, insert an appointment but overwrites one. I'm not sure this is your intention. As you want your appointments sorted you might use a sorted container - std::multiset is the simplest sorted container you can use - it allows duplicates so you could have more than one appointment on a given date. You could also use std::multimap where date is the key.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured