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;
}