General Protection Exception
I encountered this message (General Protection Exception) after I attempted to write into a text file. I cannot find anything wrong with it, and it works on another application (my friend's - it's almost the same).
This is the error I got:
General Protection Exception
0x5677:0x48A2
BOOKSHOP[1]0x48A2 Processor Fault
If I execute this program again, the memory address will be different. I know GPE is about the pointer pointing into a memory that doesn't belong to me but I don't know what to do.
This is how my program looks like. It's not very complicated, just a few classes, inheritance and I made an array of objects to store the data temporarily. Upon loading of the program I load the contents of the text file into the array and during runtime all manipulation will be made to the array in the memory. Upon exit, the program copies all data in the array into the text file.
Code:
//--------------------------------------------------------------------------------------------------------------------
class MainApp{
OFormStu ofArray[];
RecForm rfArray[];
Book bkArray[];
UserAccount useracc[];
public:
MainApp();
void loading();
void get_choice();
void enquiry();
void bookorderform();
void delbookorderform();
void recform();
void delrecform();
void newbook();
void destroybook();
void useraccount();
void adduseraccount();
void moduseraccount();
void remuseraccount();
void login();
void writefile();
};
MainApp::MainApp()
{
int c;
OFormStu of;
RecForm rf;
Book bk;
UserAccount ua;
for(c=0; c<=MAX-1; c++)
{
ofArray[c]=of;
rfArray[c]=rf;
bkArray[c]=bk;
useracc[c]=ua;
}
}
void MainApp::loading()
{
FILE* fptr;
int bisbn;
char btitle[11];
float bprice;
char bdesc[31];
int sid;
char sname[11];
int lid;
char lname[11];
int id;
char name[11];
char upass[11];
int c;
fptr=fopen("a:OrderFormStudent.txt","a+");
c=0;
if (!(fptr==NULL))
{
while (!feof(fptr))
{
fscanf(fptr,"%d",sid);
/*if(feof(fptr))
break;*/
fscanf(fptr,"%s",sname);
fscanf(fptr,"%d",bisbn);
fscanf(fptr,"%s",btitle);
fscanf(fptr,"%f",bprice);
fscanf(fptr,"%s",bdesc);
ofArray[c].setId(sid);
ofArray[c].setName(sname);
ofArray[c].setIsbn(bisbn);
ofArray[c].setTitle(btitle);
ofArray[c].setPrice(bprice);
ofArray[c].setDesc(bdesc);
c++;
};
}
fclose(fptr);
fptr=fopen("a:book.txt","a+");
c=0;
if (!(fptr==NULL))
{
while (!feof(fptr))
{
fscanf(fptr,"%d",bisbn);
/*if(feof(fptr))
break; */
fscanf(fptr,"%s",btitle);
fscanf(fptr,"%f",bprice);
fscanf(fptr,"%s",bdesc);
bkArray[c].setIsbn(bisbn);
bkArray[c].setTitle(btitle);
bkArray[c].setPrice(bprice);
bkArray[c].setDesc(bdesc);
c++;
};
}
fclose(fptr);
fptr=fopen("a:recform.txt","a+");
c=0;
if (!(fptr==NULL))
{
while (!feof(fptr))
{
fscanf(fptr,"%d",lid);
/*if(feof(fptr))
break;*/
fscanf(fptr,"%s",lname);
fscanf(fptr,"%d",bisbn);
fscanf(fptr,"%s",btitle);
fscanf(fptr,"%f",bprice);
fscanf(fptr,"%s",bdesc);
rfArray[c].setId(lid);
rfArray[c].setName(lname);
rfArray[c].setTitle(btitle);
rfArray[c].setPrice(bprice);
rfArray[c].setDesc(bdesc);
c++;
};
}
fclose(fptr);
fptr=fopen("a:useraccount.txt","a+");
c=0;
if (!(fptr==NULL))
{
while (!feof(fptr))
{
fscanf(fptr,"%d",id);
/*if(feof(fptr))
break;*/
fscanf(fptr,"%s",name);
fscanf(fptr,"%s",upass);
useracc[c].setId(id);
useracc[c].setName(name);
useracc[c].setPassword(upass);
c++;
};
}
fclose(fptr);
}
//--------------------------------------------------------------------------------------------------------------------
void MainApp::writefile(){
int c;
FILE* fptr;
fptr=fopen("a:book.txt","w+t");
c=0;
do
{
if (strcmp(bkArray[c].getTitle(),"A")==0)
{c=c+1;}
else
{
fprintf(fptr,"%d\n",bkArray[c].getIsbn());
fprintf(fptr,"%s\n",bkArray[c].getTitle());
fprintf(fptr,"%f\n",bkArray[c].getPrice());
fprintf(fptr,"%s\n",bkArray[c].getDesc());
c++;
}
}while(c <= MAX-1 && !feof(fptr));
fclose(fptr);
fptr=fopen("a:OrderFormStudent.txt","w+t");
c=0;
do
{
if (strcmp(ofArray[c].getName(),"U")==0)
{c=c+1;}
else
{
fprintf(fptr,"%d\n",ofArray[c].getId());
fprintf(fptr,"%s\n",ofArray[c].getName());
fprintf(fptr,"%d\n",ofArray[c].getIsbn());
fprintf(fptr,"%s\n",ofArray[c].getTitle());
fprintf(fptr,"%f\n",ofArray[c].getPrice());
fprintf(fptr,"%s\n",ofArray[c].getDesc());
c++;
}
}while(c <= MAX-1 && !feof(fptr));
fclose(fptr);
fptr=fopen("a:recform.txt","w+t");
c=0;
do
{
if (strcmp(rfArray[c].getName(),"U")==0)
{c=c+1;}
else
{
fprintf(fptr,"%d\n",rfArray[c].getId());
fprintf(fptr,"%s\n",rfArray[c].getName());
fprintf(fptr,"%d\n",rfArray[c].getIsbn());
fprintf(fptr,"%s\n",rfArray[c].getTitle());
fprintf(fptr,"%f\n",rfArray[c].getPrice());
fprintf(fptr,"%s\n",rfArray[c].getDesc());
c++;
}
}while(c <= MAX-1 && !feof(fptr));
fclose(fptr);
fptr=fopen("a:useraccount.txt","w+t");
c=0;
do
{
if (strcmp(useracc[c].getName(),"U")==0)
{c=c+1;}
else
{
fprintf(fptr,"%d\n",useracc[c].getId());
fprintf(fptr,"%s\n",useracc[c].getName());
fprintf(fptr,"%s\n",useracc[c].getPassword());
c++;
}
}while(c <= MAX-1 && !feof(fptr));
fclose(fptr);
}
//--------------------------------------------------------------------------------------------------------------------
These are just snippets of my program, and probably the ones causing the problem, as they're the only methods in which I used pointers. Both are in MainApp class, among other methods that manipulates the array, as aforementioned.
Please, can anyone help me? Thanks in advance. :confused: :confused: