473,761 Members | 4,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 WasFirstNonSpac eChar; /*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!=NUL L)
{
if(strcat(NewBl ock, 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 1586
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(s izeof(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.goo gle.com...
while(head!=NUL L)
{
if(strcat(NewBl ock, 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
3440
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 std::cout;
0
1065
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 to be used for this purpose yet). Recently we've expanded the app and a few more screens were added and quite a few more cookies were required to hold data across pages. Now i just found that at one specific spot, when clicking the button, using...
0
1029
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 added and quite a few more cookies were required to hold data across pages. Now i just found that at one specific spot, when clicking the button, using Response.Redirect("abc.aspx", false) to go to the abc.aspx page. As soon as the the click event...
5
2562
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 use to run a UM program, I kept on getting error messages. I have used someone else's implementation and it runs fine. I have compared my code with other's and I still can't figure it out what's wrong with mine. So please help me out, after 3...
61
3560
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. Furthermore, he doesn't even mark the assignments, but rather gives tips and so forth when going over students' work. To test students' capabilities for the purpose of state exams and qualifications though, he actually sits down with us at a...
19
1312
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 up very often!". So, for fun, who sees the bug and can explain why it's happening? List<Threadthreads = new List<Thread>(); for (int i = 0; i < 2; i++) { Thread t = new Thread(delegate()
3
7845
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 the City B in the minimum amount of time. The input in my program is an integer n and the 2D coordinates of n spots. Some assumptions have been made about the physical layout of the problem: 1) All the spots are considered to be in a square of...
3
3927
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 messing up. Problems:
0
9538
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10123
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9909
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9788
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7342
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6623
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5241
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3481
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.