Re: [Dev-C++] Please spot out the cause of this bug
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2004-11-20 11:36:37
|
First of all - i don't like a construct: if (strcat(x,y()) == NULL) ... since that implies that x is NULL - strcat doesn't perform any reallocations, it just returns the address of it's first parameter! You should have checked up on x long before, i.e. when you allocated x. Unless you are using a nice and current C++ compiler, in which case the new operator don't return a NULL. Second: Why bother with a list/queue/vector/... of lines. All you need is the current and previous lines in memory. Why? If you previous line ends with a // comment and your current line starts with white space followed by //, that is enough to activate conversion mode. Emit previous line but with the comment using "/*". Then just pricess lines until you find a line that doesn't start with white space followed by "//". Emit previous line but with an ending "*/". This state-machine concept allows you to handle a 10-million-line comment without requiring any extra memory. All you need is memory enough for two arbitrary long lines. /Per W On 19 Nov 2004, Curious Student wrote: > 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: > > > //Jingle bells, jingle bells, jingles all the way > //O what fun it is to ride in a one-horse open sledge > //yeah, jingle bells.... > > > into > > > /*Jingle bells, jingle bells, jingles all the way > O what fun it is to ride in a one-horse open sledge > yeah, jingle bells....*/ > > > or the lines, > > > i=0; //Isn't it nice > //today? hehehehe.... > > > into > > > i=0; /*Isn't it nice > today? hehehehe...*/ > > > The code can be downloaded from here. > > http://www.vbforums.com/attachment.php?s=&postid=1841801 > > I've finished coding it but I'm still not through debugging it. > > I've taken a linked list to represent a single comment block that needs 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 like this: > > > /*Represent a line of code that has a // comment in it > The purpose of this structure is to store those lines of > source code that: > > (1) Contain a // in them > (2) Appear in a series > (3) Are a part of one multi-line comment that is to be formatted > > For more information, please read the "Comments to be targetted" section > in the specification file "Read Me.txt" in this project > */ > struct LOC > { > char *Text; //Text of the line containing the comment > > struct LOC *NextLine; //Pointer to the next comment > > int Len; //length of the line containing the comment > > int Position; //Starting position of the comment in the line of text > > int WasFirstNonSpaceChar; /*A boolean value indicating if the comment > was the first non-space character in the line > of text*/ > }; > > > I'm facing a problem in my FormatBlock function. Particularly in this while loop within the FormatBlock function. > > > > while(head!=NULL) > { > if(strcat(NewBlock, Replace(head->Text, head->Position, head->Position+1, " "))==NULL) > return NULL; > > if(head->NextLine==NULL) > strcat(NewBlock, "*/"); > else > strcat(NewBlock, sNewLine); > head=head->NextLine; > } > > > > What happens is that in this while 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 list 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 that it changes the 5th element. > > For a test, I am currently reading the comments from a file that I created called "Code.c" and put in C:. > > You 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. > > > Thoughts as to what might be causing this problem? > > > > PS: I know there's a memory leak here in this line. > > if(strcat(NewBlock, Replace(head->Text, head->Position, head->Position+1, " "))==NULL) > > This is just rough work. I have a day job, and I wrote that code in a hurry. I will remove all leaks. Also the naming convention sucks because I haven'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. |