function crashing on Unix but not Windoze

This is a discussion on function crashing on Unix but not Windoze within the C Programming forums, part of the General Programming Boards category; Hi the function below is crashing when the first or second if statements are true and copynode() is called. I've ...

  1. #1
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72

    Question function crashing on Unix but not Windoze

    Hi
    the function below is crashing when the first or second if statements are true and copynode() is called.
    I've commented the calls where the crash takes place.

    I don't have copynode() code. All I have is the .o file for that. I can see, however, in gdb that the crash is happening when strcpy() is called from within copynode(). it "Seems" that I am passing in a bad pointer. Looking in my debugger at the node (curr) that I am passing into copynode() , all it's members contain seemingly valid data.

    My question is:
    1) am I allocating enough memory for (curr) that I am passing to copynode() I am allocating malloc(sizeof(KEY)) which should be the correct amount. I even tried malloc(sizeof(10000)) and that didn't stop the crash.

    2) Is there any other obscure thing that could be causing this crash to take place?

    Thanks in advance,
    Mike

    typedef struct
    {
    int month,day,year;
    }DATE;

    typedef struct Key KEY;
    typedef struct Key* KEYS;

    struct Key
    {
    char *sender;
    char *addressee;
    char *regarding;
    DATE date;
    int id;
    char *fname;
    KEYS next;
    };




    /****************************************
    * search.c
    * KEYS search(KEYS k,KEY ki) (1)
    *
    * This routine takes the information in a node and
    * finds all the matches in the keyk.
    * NOTE: The node passed in by -will contain
    * strings that are "chnull" valued if the user has
    * entered a fillename or id. The "empty" strings
    * will contain "0" (the users input) if no id or
    * filename was entered.
    *
    * inputs: pointer to keyk, information node
    * outputs: pointer to k of nodes that match info node
    ******************************************/
    #include<stdio.h>
    #include"keytype.h"

    KEYS search(KEYS k, KEY ki )
    {
    /***************************
    * general declarations
    ****************************/
    KEY *curr; //a dummy pointer for the incoming k
    KEY *head, *hits; //to build our k with

    /************************
    * prototype declarations
    **************************/
    KEYS copynode(KEYS node);

    /***************************************
    * Begin Function body
    ****************************************/
    //set up our new k to build
    hits=(KEYS)malloc(sizeof(KEY));
    head=hits; //make a copy of head.
    hits->next=(KEYS)malloc(sizeof(KEY));
    curr=k; //move k ptr to dummy node

    //if we more than just a dummy node.
    if(curr->next)
    {
    //loop through the k and compare
    do
    {
    curr=curr->next; //bump the pointer
    /************************************
    * test for unique identifiers first.
    *************************************/
    if(ki.id==curr->id)
    {
    hits->next=copynode(curr); /**crashes here**/
    //hits=hits->next; //bump pointer
    //hits->next=(KEYS)malloc(sizeof(KEY));
    break;
    }
    if(!strcmp(ki.fname,curr->fname))
    {
    hits->next=copynode(curr); /**crashes here**/
    //hits=hits->next; //bump pointer
    //hits->next=(KEYS)malloc(sizeof(KEY));
    break;
    }
    /***************************************
    * test for NOT unique identifiers
    * in this section we have to consider
    * that there may be more than one search
    * criteria inside ki.
    *****************************************/
    if((strcmp(ki.sender,"0")==0 || strcmp(ki.sender,curr->sender)==0) &&
    (strcmp(ki.addressee,"0")==0 || strcmp(ki.addressee,curr->addressee)==0) &&
    (strcmp(ki.regarding,"0")==0 || strcmp(ki.regarding,curr->regarding)==0) &&
    (ki.date.day == 0 || ki.date.day == curr->date.day) &&
    (ki.date.month == 0 || ki.date.month == curr->date.month) &&
    (ki.date.year == 0 || ki.date.year == curr->date.year) &&
    (ki.id == 0 || ki.id == curr->id) &&
    (strcmp (ki.fname, "0") == 0 || strcmp (ki.fname, curr->fname) == 0))
    {
    hits->next=copynode(curr);
    hits=hits->next; //bump pointer
    hits->next=(KEYS)malloc(sizeof(KEY));//link!
    }
    }
    while(curr->next);
    }
    //terminate the k and return
    hits->next=NULL;
    return head;
    }
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    First of all, you shouldn't be doing this

    hits=(KEYS)malloc(sizeof(KEY));
    head=hits; //make a copy of head.
    hits->next=(KEYS)malloc(sizeof(KEY));
    curr=k; //move k ptr to dummy node

    You should start with an empty list like so
    head = NULL;
    hits = NULL;

    Otherwise, you will return a list of at least 2 entries (containing uninitialised data/pointers to the caller).

    The next point is that the search loop should be a while loop, not a do loop. This is so it will do the right thing if the input list is also an empty list.

    I'm guessing here that copynode also calls malloc, and copies the data into that new node.

    > curr=curr->next; //bump the pointer
    See, this is also suspicious - you start the loop by following the next pointer, which leads me to suspect that you have a dummy node at the front of your main list as well.

    You might want to think about fixing this, because it will complicate all of your code. If your 'n' element list has 'n' elements (and no dummies), then you end up with simple while loops to traverse such lists. The example also shows you how to set the first node in a list, and then append to it.

    Code:
    KEYS search(KEYS k, KEY ki ) {
        KEY *head, *hits;           //to build our k with
        KEYS copynode(KEYS node);
        hits = NULL;
        head = NULL;
    
        /* search the list */
        while ( k != NULL ) {
            /* compare_nodes returns true if there is a match */
            if ( compare_nodes(k,ki) ) {
                if ( head == NULL ) {
                    /* result list is empty */
                    hits = copynode( k );
                    head = hits;
                } else {
                    /* append to existing result list */
                    /* hits always points to the last node in the result list */
                    hits->next = copynode( k );
                    hits = hits->next;
                }
            }
            k = k->next;
        }
        return head;
    }

  3. #3
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    Hi Salem,
    Firstly, thank you for taking the time to reply.
    Secondly, I want to get that little applet that you have that
    makes my code readable from HTML.

    thirdly,
    I have no control of any functions outside of this one that I am writing. Unfortunately!

    Your assumption is correct about malloc being called inside of copynode().

    So every list that is passed in any fashion is expected to have a dummy node. With the exception of copynode() since we are passing the pointer (pointing to the node we wish to make a copy of) into copynode(). the return value of copynode is returned to hits->next. Which incidently, that list has a dummy node too since it's returned at the end of the function. The caller expects the return of search() to have a dummy node.

    So having said all that, I must use a do while. If I use a while loop, the last node that has data in it is missed because that last node's curr->next is null.

    also, knowing this...
    is my code
    hits=(KEYS)malloc(sizeof(KEY));
    head=hits; //make a copy of head.
    hits->next=(KEYS)malloc(sizeof(KEY));
    curr=k; //move k ptr to dummy node

    still incorrect? Sorry if I seem a little confused still.

    Thank you
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    > Secondly, I want to get that little applet that you have that
    makes my code readable from HTML.
    Read this
    http://www.cprogramming.com/cboard/m...bbcode#buttons

    If everything has a dummy node, then I think you do this

    hits=(KEYS)malloc(sizeof(KEY));
    head=hits; //make a copy of head.
    // omit this step hits->next=(KEYS)malloc(sizeof(KEY));
    curr=k; //move k ptr to dummy node

    Then everywhere where you call copynode, the code should look like this
    hits->next=copynode(curr); /**crashes here**/
    hits=hits->next; //bump pointer
    // dont do this - hits->next=(KEYS)malloc(sizeof(KEY));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21