(I have 2 queries)
I have made a html parser but the memory leaks, as it seems from the message below; is killing me. I am getting this message from gdb debugger (cygwin build).
Heap block at 003D2CA0 modified at 003D2CB3 past requested size of b
Also now not all the output from cout and printf are getting printed. I am posted the code of main() function where all the pointer acrobatics takes place. The class Parser does the parsing. It can parse from a file or from a character array. It uses an object (buf) of vector<char> to store the output and dynamically instructs vector to resizes when needed using the code...
if(bufptr>=buf.size()) buf.resize(buf.size()+INITIAL_BUF_SIZE,'\0');
buf[bufptr++]=c;
Query 1: What is the difference between vector's resize and reserve?
Quer 2: Do u see any possible memory leaks in the badly hacked code below?
int main(){
Parser p;
Tag tag;
strcpy(tag.name,"div");
Property *px=new Property[2];
strcpy(px[0].name,"id");
strcpy(px[0].value,"res");
tag.totalProperties=1;
tag.property=px;
bool err;
char *out=p.parse("g-ogre.htm",tag,0,NULL,0,err,true);
//cout<<"div:: "<<out<<endl;
strcpy(tag.name,"p");
tag.totalProperties=0;
char *tout=p.parse(out,tag,1,NULL,0,err,false);
//cout<<tout<<endl;
strcpy(tag.name,"a");
char *rp=new char[300], *trp=NULL;
rp[0]='\0';
int i=1;
do{
if(trp) {delete [] trp; trp=NULL;}
trp=p.parse(tout,tag,i++,NULL,0,err,false);
strcat(rp,"; ");
strcat(rp,trp);
}while(strLen(trp)!=0);
if(trp) {delete [] trp; trp=NULL;}
if(tout) {delete [] tout; tout=NULL;}
//cout<<"Related phrases::-\n"<<rp<<endl;
strcpy(tag.name,"ul");
strcpy(px[0].name,"type");
strcpy(px[0].value,"disc");
tag.totalProperties=1;
Tag itag;//tags to ignore.
strcpy(itag.name,"br");
//strcpy(itag[0].name,"a");
tout=p.parse(out,tag,1,&itag,1,err);
//cout<<"ul :: "<<tout<<endl;
tag.totalProperties=0;
char *lp=new (nothrow) char[10000];
if(lp==NULL){
cout<<"Out of Memory!!!"<<endl;
return 0;
}
lp[0]='\0';
lp[9999]='@';
i=1;
trp=NULL;
strcpy(itag.name,"a");
do{
if(trp) {delete [] trp; trp=NULL;}
strcpy(tag.name,"font");
trp=p.parse(tout,tag,i++,NULL,0,err);
if(strLen(trp)==0) break;
strcat(lp,"\n\n:");
char *qp=NULL;
strcpy(tag.name,"li");
int j=1;
do{
if(qp) {delete [] qp; qp=NULL;}
qp=p.parse(trp,tag,1,&itag,1,err);//The content of li ignoring the tag <a> (this contains the source url, will be extracted in following lines).
cout<<">>>>>>>>>>>>>QP"<<qp<<endl;
strcat(lp,"\n");
strcat(lp,qp);
}while(strLen(qp)!=0);
if(qp) {delete [] qp; qp=NULL;}
/*
strcpy(tag.name,"a");
char *krp=p.parse(trp,tag,1,NULL,0,err);//Extracting content of <a> for getting source url, but contains font tag too that encloses the data required.
strcpy(tag.name,"font");
char *ttrp=p.parse(krp,tag,1,NULL,0,err);//From the content extracted in the line above getting the content of <Font>. This effectively removes the font tag.
strcat(lp,"\nSource Url: ");
strcat(lp,ttrp);
delete [] krp; krp=NULL;
delete [] ttrp; ttrp=NULL;
delete [] trp; trp=NULL;*/
//strcat(lp,trp);
}while(1);
if(trp) {delete [] trp; trp=NULL;}
if(lp[4999]!='@')
printf("lp overflowed!!!\n");
else
printf("Definitions:-\n%s\n",lp);
printf("End of program.");
//cout<<err;
return 0;
}