Search code examples
c++debuggingvisual-studio-2008visual-studio-debugging

Visual C++: Invalid allocation size. Why is this happening?


Im the process of learning C++. I got stuck debugging some of my code on VC++, with the following error-

Debug Error
*path to filename.exe*
Invalid allocation size:429496723 bytes.

The error precisely occurs when the debugger reaches the following block: EDIT: Ive added the entire main function to help check if maxlen is taking on any unexpected values(none that I notice though)

int main(){

    vector <Student_info> students;

    Student_info record;

    string::size_type maxlen=0;// length of the longest name

    //read and store all the students data.

    //maxlen contains the length of the name of the longest student.

    while (read(cin,record)){
    // find the length of the longest name
        max(maxlen,record.name.size());
        //add the student record to the vector.

        students.push_back(record);
    }
    //arrange the records alphabetically.
    sort(students.begin(),students.end(),compare);// this may look weird but the fact that the names have to be compared is checked using the predicate.

        //write out the names and grades.


        for(vector<Student_info>::size_type i =0;i!=students.size();i++){

            //padding to ensure that there is vertical alignment.
            cout<<students[i].name<<string(maxlen+1-students[i].name.size(),' ');// debugger stops here!
            //compute grade.
            try{
                double final_grade=grade(students[i]);
                streamsize prec=cout.precision();
                cout<<setprecision(3)<<final_grade<<setprecision(prec);
            }
            catch(domain_error e){
            //catch error if hw vector is empty!

                e.what();

            }
            cout<<endl;

            }
        return 0;
        }

Solution

  • You might look at the line:

    max(maxlen,record.name.size());
    

    Is it possible that you meant:

    maxlen = max(maxlen,record.name.size());