0

代码的目的:维护一个唯一的元素链接列表...UFID 是唯一的关键字

Structure declaration:

    struct sharedFiles
    {
      char UFID[50];
      int valid;                    //valid 1 if someone have this file in write mode
      int shared;                   //no of user's reading this file
      struct sharedFiles *next;     //pointer to next node
    }*sfstart,*sfend;               //sfstart points to the first node of linked list and efend to the last node of linked list so that it will be easy to just insert at the end without traversing the linked list

错误描述:下面的代码在我第二次调用它时会出现分段错误。我尝试使用 GDB 进行调试,它说无法访问该位置

if(strcmp(sftemp->UFID,ufid)==0)

在上面的行中,它无法访问sftemp->UFID

/*Function code*/


  int addShareList(char *ufid,int mode)      //mode=0 (read)  and mode=1 (Write request)
{
  struct sharedFiles *sftemp,*newnode;

  sftemp=sfstart;
   if(sfstart==NULL)   //if list is empty add first node
   {
       sfstart=(struct sharedFiles *) malloc(sizeof(struct sharedFiles));
       strcpy(sfstart->UFID,ufid);
       sfstart->valid=mode;
       sfstart->shared=1;
       sfstart->next=NULL;
       sfend=sfstart;             //this node will also be last node of Linked list
       return 0;
   }
   else                 //if list is not empty
   {

        while(sftemp->next != NULL)    //traverse till last node
        {
          if(strcmp(sftemp->UFID,ufid)==0)
          {
             //here if same node found some manupulation to the struct variables

          }
         sftemp=sftemp->next;
        } //while


  if(sftemp->next==NULL)  //procvess last node
       {

         if(strcmp(sftemp->UFID,ufid)!=0) //if last node not same add node at the end of   Linked list
         {
           newnode=(struct sharedFiles *) malloc(sizeof(struct sharedFiles));
           strcpy(newnode->UFID,ufid);
           newnode->valid=mode;
           newnode->shared=1;
            newnode->next=NULL;
           sftemp->next=newnode;
           sfend=newnode;
           return 0;
          }
          else //if last node is same
          {

                //some manipulations to struct variables 
          }
       } //if


   }

return -1;
}//addShareList

上面的代码可以很好地插入第一个元素。当我调用相同的函数在链表中插入第二个节点时,它无法访问第一个节点,而在 if(strcmp(sftemp->UFID,ufid)==0 行中的comarision )。希望现在代码的目的很明确。

提前致谢..

4

2 回答 2

1

In the while you check if sftemp!=NULL so we can be sure that in the second iteration, after line sftemp=sftemp->next; the pointer contain allocated memory.

But, since I don't know how the list it structured, I can't be sure that the content contains another node of sharedFiles type, it could contain an end-list node which not contains UFID attribute.

So, check in your list how to control if the list is finished.

Another solution can be to change your check this way:

while(sftemp->next!=NULL)

...

if(sftemp->next==NULL) {
    //add the node in the right way, consider the end-list node
}

EDIT:

Furthermore, change in the first if the line sftemp->next = NULL; to sfnext->next = NULL;.

And be sure to initialize stnext = NULL.

EDIT 2:

Now that you post struct declaration I still cannot see when you initialize sfstart. Try to do this:

Structure declaration:

struct sharedFiles
{
  char UFID[50];
  int valid;                    //valid 1 if someone have this file in write mode
  int shared;                   //no of user's reading this file
  struct sharedFiles *next;     //pointer to next node
}*sfstart = NULL,*sfend;
于 2013-03-06T13:53:29.247 回答
0

Normally the answer for this is that the pointer is NULL, which in your case means malloc has failed because you're out of memory - you really should be checking the return value.

The most likely cause for your problem is that you have corrupted the heap, so that sftemp no longer points to a valid memory address. Check what it's value is that might give you some more clues.

于 2013-03-06T13:49:59.173 回答