locked
help please RRS feed

  • Question

  • I have a two classes on called grade and another called quarter_grade which is an array of grade. There is a run time error that occurs when I try to initialize the grade class in a certain element. 

     0xC0000005: Access violation reading location 0x0000001c.

    here is the code in the quarter_grade class

    for(int i = 0; 1 < num_classes; i++)
        {
            int a;
            double b;
            string c;
            cout << "Enter a course #" << i + 1 << ". " << endl;
            cin >> c;
            //grades[i].set_course(c);
            cout << "Enter the amount of units. " << endl;
            cin >> a;
            //grades[i].set_units(a);
            cout << endl << "Enter decimal grade " << endl;
            cin >> b;
            //grades[i].set_grades(b);

            grades[i] = Grades( c, a, b );
            qunits += grades[i].units;
            total_grades += grades[i].grade;
            grades -> print();
    }


    the run time error happens
    at
    grades[i] = Grades( a, b, c)


    I am a beginner programmer so I dont know what that means. please help ??

    thanks Will
    Monday, June 15, 2009 8:02 PM

Answers

  • So it's pointing at NULL and you then assign an object to grades[i], so it tries to store there but NULL is not valid memory.

    This is called a dangling pointer: It doesn't point to anything useful because you have not allocated any memory to point at.  If you want an array of Grade* then you need to create one.  You can do it like this if you know at compile time how many you need:

    Grade* grades[a_constant];

    If you don't know ahead of time how many you need you can do it this way:

    grades = new Grades[a_variable];

    • Proposed as answer by Brian Muth Tuesday, June 16, 2009 2:20 AM
    • Marked as answer by William Ibekwe Tuesday, June 16, 2009 3:13 AM
    Tuesday, June 16, 2009 1:49 AM

All replies

  • It means the code is accessing an invalid memory address. You need to show the definition of grades and the implementation of Grades() for more advice.
    Monday, June 15, 2009 8:07 PM
  • grades is a pointer to an array of class type Grades.

    here is the implementation of the constructor Grades()
    Grades:: Grades( string course, int units, double grade )
    {
        this-> course = course;
        this-> units = units;
        this-> grade = grade;
    }
    Monday, June 15, 2009 8:31 PM
  • Please show the exact definition of grades , as Brian already asked.

    My hypothesis is that you are writing out of array bounds for some value of index i.

    Has the array enough space to store num_classes instances of Grades ?

    Giovanni

    Monday, June 15, 2009 10:23 PM
  • There is a typo error in the "for" statement that could make it run forever (until it crashes).
    Monday, June 15, 2009 11:24 PM
  • the exact definition is

    Grade * grades under as a private member
    i initialized  it in the the default constructor to NULL;
    Tuesday, June 16, 2009 1:14 AM
  • So it's pointing at NULL and you then assign an object to grades[i], so it tries to store there but NULL is not valid memory.

    This is called a dangling pointer: It doesn't point to anything useful because you have not allocated any memory to point at.  If you want an array of Grade* then you need to create one.  You can do it like this if you know at compile time how many you need:

    Grade* grades[a_constant];

    If you don't know ahead of time how many you need you can do it this way:

    grades = new Grades[a_variable];

    • Proposed as answer by Brian Muth Tuesday, June 16, 2009 2:20 AM
    • Marked as answer by William Ibekwe Tuesday, June 16, 2009 3:13 AM
    Tuesday, June 16, 2009 1:49 AM
  • Welcome to the forum, willibeamin.

    As an aside, in future, try to use a more meaningful title to your original question, as it will more likely attract an answer. Secondly, try to post real code snippets rather than a description of what a variable is... it cuts down on the repeated questioning, as illustrated in this thread.
    Tuesday, June 16, 2009 2:19 AM
  • thank you so much,
    Sorry for the confusion.
    Tuesday, June 16, 2009 3:15 AM