解析 csv 文件,"malloc: *** error for object 0x7ffeeb4f4b80: pointer being freed was not allocated"出现此错误



我正在尝试解析一个格式如下的csv文件,

ID,等级

中间可能有空白,我不知道文件会有多少行,然后我必须制作一个成绩直方图。

这是我的代码:

int histogram(const char filename[], int histogram[10], 
const int minAcceptableID, const int maxAcceptableID,
int*& rejects) {

int numerror = 0;
int maxnumerror = 10;

std::ifstream file(filename);
if(!file.is_open()){
return -1;
}
int numlines=0;
int const maxlength = 100;
char line[maxlength];
bool done= false;
while(!done){
numlines++;
file.getline(line,maxlength);
if(!file.getline(line,maxlength)){
if(file.eof()){
done = true;
}
else{
return -1;
}
}
enum state {ID, COMMA, GRADE, EOL, WS1, WS2, WS3, WS4};
state instate;
int stid=0;
int fgrade=0;
for(int i=0; i<maxlength ; i++){
if( line[i]== ' ' || line[i] =='t'){
instate=WS1;
}
if(line[i]>='0' && line[i]<='9'){
instate=ID;
}
switch(instate){
case WS1:
cout<<"WS1 "<<endl;
if(line[i+1]==' ' || line[i+1]=='t'){
instate=WS1;
}
if(line[i+1]>='0' && line[i+1]<='9'){
instate=ID;
}
break;
case ID:
cout<<" ID"<<endl;
stid= stid*10 + (line[i]-'0');
if(line[i]>='0' && line[i]<='9'){
instate=ID;
}
if(line[i+1]==' ' || line[i+1]=='t'){
instate = WS2;
}
if(line[i+1]==','){
instate=COMMA;
}
break;
case WS2:
cout<<"WS2 "<<endl;
if(line[i+1]==' ' || line[i+1]=='t'){
instate = WS2;
}
if(line[i+1]==','){
instate=COMMA;
}
case COMMA:
cout<<"COMMA "<<endl;
if(line[i+1]>='0' && line[i+1]<='9'){
instate=GRADE;
}
if(line[i+1]==' ' || line[i+1]=='t'){
instate = WS3;
}
break;
case WS3:
cout<<"WS3 "<<endl;
if(line[i+1]==' ' || line[i+1]=='t'){
instate = WS3;
}
if(line[i]>='0' && line[i]<='9'){
instate=GRADE;
}
break;
case GRADE:
cout<<" GRADE"<<endl;
fgrade = fgrade*10 + (line[i]-'0');
if(line[i+1]>='0' && line[i+1]<='9'){
instate=GRADE;
}
if(line[i+1]==' ' || line[i+1]=='t'){
instate=WS4;
}
if(line[i+1]==''){
instate=EOL;
}
break;
case WS4:
cout<<" WS4"<<endl;
if(line[i+1]==' ' || line[i+1]=='t'){
instate=WS4;
}
if(line[i+1]==''){
instate=EOL;
}
break;
case EOL:
cout<<" EOL"<<endl;
i=maxlength;
break;
default:
break;
} 
}

if(stid>maxAcceptableID || stid<minAcceptableID || fgrade>100){
numerror++;
delete[] rejects;
rejects = new int[numerror];
for(int i=0;i<numerror;i++){
rejects[i]=numlines;
}
}
if(fgrade>=0 || fgrade<=9){
int a=0;
histogram[0]=a++;
}
if(fgrade>=10 || fgrade<=19){
int a=0;
histogram[1]=a++;
}    
if(fgrade>=20 || fgrade<=29){
int a=0;
histogram[2]=a++;
}    
if(fgrade>=30 || fgrade<=39){
int a=0;
histogram[3]=a++;
}    
if(fgrade>=40 || fgrade<=49){
int a=0;
histogram[4]=a++;
}    
if(fgrade>=50 || fgrade<=59){
int a=0;
histogram[5]=a++;
}    
if(fgrade>=60 || fgrade<=69){
int a=0;
histogram[6]=a++;
}    
if(fgrade>=70 || fgrade<=79){
int a=0;
histogram[7]=a++;
}         
if(fgrade>=80 || fgrade<=89){
int a=0;
histogram[8]=a++;
}    
if(fgrade>=90 || fgrade<=100){
int a=0;
histogram[9]=a++;
}

}
return numerror;
if(numerror>maxnumerror){
return -1;
}
}

我是不是在处理拒绝数组?它在main()函数中定义为

int* rejectedRecords;

我将在csv文件中发现错误的行号存储在拒绝中。

由于您将rejects从我们看不到的代码传递到函数中,因此很难说100%确定,但我们可以假设,在您第一次尝试删除数组时,您尚未为其分配任何内存。

如果您不想使用C++标准容器类(这将是推荐的方法),那么在尝试删除它之前,您需要确保rejects指针设置为nullptr或已分配内存。

相关内容

最新更新