473,412 Members | 3,471 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Please spot out the cause of this bug

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.p...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?
Nov 14 '05 #1
2 1568
On 19 Nov 2004 15:25:30 -0800, Vi****************@yahoo.com
(Sathyaish) wrote:
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.

Nov 14 '05 #2
"Sathyaish" <Vi****************@yahoo.com> wrote in message
news:7b**************************@posting.google.c om...
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.
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: manoj1978 | last post by:
Hi All, I wrote the following to print an integer in its string representation for base -36 to 36. Please comment on this code. #include <iostream> #include <string> using std::abs; using...
0
by: Calvin KD | last post by:
Hi everyone, I need help urgently. I have a C#.Net app which uses cookies for state management (since we've gone away from Session for fear of webfarm and we haven't found the need for SQL Server...
0
by: Calvin KD | last post by:
Hi everyone, I need help urgently. I have a C#.Net app which uses cookies for state management. Everything has been going fine until recently we've expanded the app and a few more screens were...
5
by: weidongtom | last post by:
Hi, I tried to implement the Universal Machine as described in http://www.boundvariable.org/task.shtml, and I managed to get one implemented (After looking at what other's have done.) But when I...
61
by: warint | last post by:
My lecturer gave us an assignment. He has a very "mature" way of teaching in that he doesn't care whether people show up, whether they do the assignments, or whether they copy other people's work....
19
by: Chris Mullins [MVP - C#] | last post by:
I hit this bug last night in some test code I had written. It took a few minutes to figure out what the root cause was, and it left me thinking, "Wow. That was an interesting one that doesn't come...
3
by: victorporton | last post by:
D.K. is traveling from City A to City B. He can stop at some designated spots only. I am trying to use Dijkstra’s algorithm to determine the “spot-to-spot” path that will get D.K. from City A to...
3
by: nigelesquire | last post by:
Please help! I'm trying to clone and delete multiple rows with JavaScript. I need two delete buttons that work...! I only have one for now, but it's not working properly, the output count is...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.