Please spot out the cause of this bug

Discussion in 'C Programming' started by Sathyaish, Nov 19, 2004.

  1. Sathyaish

    Sathyaish Guest

    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?
    Sathyaish, Nov 19, 2004
    #1
    1. Advertising

  2. Sathyaish

    Nick Austin Guest

    On 19 Nov 2004 15:25:30 -0800,
    (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.
    Nick Austin, Nov 20, 2004
    #2
    1. Advertising

  3. "Sathyaish" <> wrote in message
    news:...
    > 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.
    Charlie Gordon, Nov 24, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. A great onestop spot

    , Feb 7, 2006, in forum: Perl
    Replies:
    0
    Views:
    2,050
  2. urocrane

    spot the bug

    urocrane, Jul 12, 2003, in forum: C++
    Replies:
    3
    Views:
    407
    John Carson
    Jul 12, 2003
  3. Andreas Vinther

    Please help me spot the difference

    Andreas Vinther, Oct 23, 2006, in forum: C Programming
    Replies:
    9
    Views:
    407
    Andreas Vinther
    Nov 6, 2006
  4. Replies:
    5
    Views:
    298
    David Thompson
    May 21, 2007
  5. shapper
    Replies:
    2
    Views:
    234
    shapper
    Nov 7, 2008
Loading...

Share This Page