[Dev-C++] Please spot out the cause of this bug
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Curious S. <est...@re...> - 2004-11-19 23:56:31
|
I am writing a program that will take a string that has a C source file and= it would format all the multi-line comments like these:=0A=0A=0A//Jingle b= ells, jingle bells, jingles all the way=0A//O what fun it is to ride in a o= ne-horse open sledge=0A//yeah, jingle bells....=0A=0A=0Ainto=0A=0A=0A/*Jing= le bells, jingle bells, jingles all the way=0AO what fun it is to ride in a= one-horse open sledge=0Ayeah, jingle bells....*/=0A=0A=0Aor the lines,=0A= =0A=0Ai=3D0; //Isn't it nice=0A//today? hehehehe....=0A=0A=0Ainto=0A=0A=0Ai= =3D0; /*Isn't it nice=0Atoday? hehehehe...*/=0A=0A=0AThe code can be downlo= aded from here. =0A=0Ahttp://www.vbforums.com/attachment.php?s=3D&postid=3D= 1841801=0A=0AI've finished coding it but I'm still not through debugging it= .=0A=0AI've taken a linked list to represent a single comment block that ne= eds to be formatted. Each node would represent one line of source code that= contains a comment. The list is represented by a struct LOC that looks li= ke this:=0A=0A=0A/*Represent a line of code that has a // comment in it=0AT= he purpose of this structure is to store those lines of=0Asource code that:= =0A=0A (1) Contain a // in them=0A (2) Appear in a series=0A (3) Are a p= art of one multi-line comment that is to be formatted=0A=0A For more infor= mation, please read the "Comments to be targetted" section=0A in the speci= fication file "Read Me.txt" in this project=0A*/=0Astruct LOC=0A{=0A cha= r *Text; //Text of the line containing the comment=0A=0A struct LOC *Nex= tLine; //Pointer to the next comment=0A=0A int Len; //length of the line= containing the comment=0A=0A int Position; //Starting position of the c= omment in the line of text=0A=0A int WasFirstNonSpaceChar; /*A boolean v= alue indicating if the comment =0A was the fir= st non-space character in the line =0A of text= */=0A};=0A=0A=0AI'm facing a problem in my FormatBlock function. Particular= ly in this while loop within the FormatBlock function.=0A=0A=0A=0Awhile(hea= d!=3DNULL)=0A {=0A if(strcat(NewBlock, Replace(head->Text, head->= Position, head->Position+1, " "))=3D=3DNULL)=0A return NULL;=0A= =0A if(head->NextLine=3D=3DNULL)=0A strcat(NewBlo= ck, "*/");=0A else=0A strcat(NewBlock, sNewLine);=0A = head=3Dhead->NextLine;=0A }=0A=0A=0A=0AWhat happens is that in this w= hile loop, somehow, while i'm only traversing the loop and replacing, say, = node 3 out of 5 nodes in my linked list, the text of the 5th node in the li= st gets changed somehow. Just before this loop, all the elements are intact= . I put printf statements at many steps and discovered that the problem is = only when it comes to the 2nd element of the linked list within this loop t= hat it changes the 5th element.=0A=0AFor a test, I am currently reading the= comments from a file that I created called "Code.c" and put in C:.=0A=0AYo= u will find the file Code.c also within the attachment linked above. Please= download it and put the Code.c file in C: and then walk through the code.= =0A=0A=0AThoughts as to what might be causing this problem? =0A=0A=0A=0APS:= I know there's a memory leak here in this line.=0A=0Aif(strcat(NewBlock, R= eplace(head->Text, head->Position, head->Position+1, " "))=3D=3DNULL)=0A= =0AThis is just rough work. I have a day job, and I wrote that code in a hu= rry. I will remove all leaks. Also the naming convention sucks because I ha= ven't found a convention to stick to, that is to my taste. I still have to = land on one. No typedef used instead of struct LOC* all the time.=20 |