Please spot out the cause of this bug

S

Sathyaish

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?
 
N

Nick Austin

Thoughts as to what might be causing this problem?

strSourceCode = (char*)malloc(sizeof(char)*(2*MAX_LEN));

This only allocates 510 bytes which isn't enough to hold the file
you are loading.

Nick.
 
C

Charlie Gordon

Sathyaish said:
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;
}

strcat returns its first argument. Are you sure you understand the semantics of
this function ? it doesn't allocate any memory nor otherwise "create" a new
string. It merely copies characters from the second string to the end of the
array pointed to by its first argument. This array must be large enough
already. I don't know how you allocate NewBlock, but computing the required
size is non trivial, so this is a good place to look for problems.

Chqrlie.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top