[英]Visual C++: Invalid allocation size. Why is this happening?
我在学习C ++的过程中。 我在VC ++上调试某些代码时遇到了以下错误,
Debug Error
*path to filename.exe*
Invalid allocation size:429496723 bytes.
该错误恰好在调试器到达以下块时发生: 编辑 :我已经添加了整个主要功能来帮助检查maxlen是否采用任何意外值(尽管我没有注意到)
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;
}
您可能会看到这一行:
max(maxlen,record.name.size());
您是否可能表示:
maxlen = max(maxlen,record.name.size());
在将其初始化为0之后,您再也不会为maxlen分配一个值。然后,您从该值中减去1得到负的偏移量。
在您的第一个while循环中,您可能想要...
maxlen = max( maxlen, record.name.size() );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.