2016-04-07 13 views
0

Bağlantılı listelerin kafalarını saklamak için kullandığım tek boyutlu bir dizi işaretçim var. Bunu beyan veBağlantılı listenin kopyalama kafasındaki bölümleme hatası

struct node { long int val; struct node *next; }; //Our node structure 
struct node **pointArray; 
pointArray = malloc(size_Of_array * sizeof(struct node *)); 
for (z =0; z<sizeof(pointArray); z++) 
    pointArray[z] = NULL; 
/* 
do something else 
... 
... 
*/ 
while(numRead <= bytesPerThread) //loop1 
{ 
    m = read(fd, buff, sizeof(buff)); 
    numRead += m; 
    if (m < 0) 
     handle_error("Read error\n"); 
    else 
    { 
     for (i=0; i< m; i++) //loop2 
     { 

      if (buff[i] == '#') 
       while(buff[i] != '\n') 
       { 
        i++; 
        continue; 
       } 
      else //else 2 
      { 
       if(buff[i] !=' ' && buff[i]!='\n' && buff[i] !='\t') 
       { temp[a] = buff[i]; 
        a++; 
       } 
       else //else 1 
       { Node = atoi(temp); 
        a = 0; 
        for (x=0; x< 15; x++) 
         temp[x] = '\0'; 
        if ((buff[i] == ' ' || buff[i] == '\t') && curruntNode == -1)//just for first time 
        { 
         head = (struct node*) malloc(sizeof(struct node)); 
         head->val = Node; //create first node, i.e. head 
         head->next = NULL; 
         current = head; //Set current node to head 
       pthread_mutex_lock(&myLock); 
         pointArray[Node] = head; 
       pthread_mutex_unlock(&myLock); 
         curruntNode = Node; 
        } else 
        if (buff[i] == '\n') 
        { 
         temp2 = (struct node*) malloc(sizeof(struct node)); 
         temp2->val = Node; 
         temp2->next = NULL; 
         current->next = temp2; 
         current = temp2; 
        } else 
        if ((buff[i] == '\t' || buff[i] ==' ') && curruntNode != Node) 
{head = (struct node*) malloc(sizeof(struct node)); 
head->val = Node; //create first node, i.e. head 
head->next = NULL; 
current = head; //Set current node to head 
pthread_mutex_lock(&myLock); 
if (pointArray[Node] == NULL) 
{ 
pointArray[Node] = head;} 
else 
{ 
current = pointArray[Node]; 
while (current->next != NULL) 
{ 
    current = current->next; 
} 
current->next = head; 
current = head; 
} 
pthread_mutex_unlock(&myLock); 
curruntNode = Node;} 

Sonra her bağlantılı listesinde arama yapmak için işaretçiler beyan bu ifadelere doldurmak

for (j = NodesToSeek; j < NodesPerThread; j++) 
{printf("hee"); 
    if (pointArray[j] == NULL) 
     {printf("Null");continue;} 
    else 
    { 
     firstNode = pointArray[j]; 
printf("1node %ld",firstNode->val); 
     firstNode = firstNode ->next; 
     while (firstNode != NULL) 
     { 
      move = firstNode; 
      while (move != NULL) 
      { 
       comparedNode = pointArray[firstNode->val]; 
       comparedNode = comparedNode->next; 
       while (comparedNode != NULL) 
       { 
        if (comparedNode->val == move->val) 
        { 
       pthread_mutex_lock(&myLock); 
         tringles++; 
       pthread_mutex_unlock(&myLock); 
        } 
        comparedNode = comparedNode->next; 
       }//end while 
       move = move->next; 
      }//end while 
      firstNode = firstNode->next; 
     }//end while 
    }//end else 
}//en 
Bu işaretçi boş sorun bu ifadeye

comparedNode = comparedNode->next; 

oluşur kalır

Bana bunun neden olduğunu söyleyen var mı?

+0

Bu mantıklı değil? –

+0

int. Ben her biri bitişik listeye sahip düğüm sayısı var, bu yüzden ben düğüm numarası –

cevap

0
pointArray = malloc(size_Of_array * sizeof(struct node *)); 
for (z =0; z<sizeof(pointArray); z++) 
    pointArray[z] = NULL; 

sizeof (pointArray) dizideki elementlerin değil numarası, örneğin bayt ibrenin boyutunu verir. Eğer size_Of_array < sizeof (pointArray) ise, dizinin bir kısmı başlatılmamış, aşağıdaki kod oldukça tehlikeli hale getirilmiştir.

// It's possible pointArray[Node] is not initialized here 
if (pointArray[Node] == NULL) 
{ 
    pointArray[Node] = head; 
} 
else 
{ 
... 

Zaten dizinin boyutunu biliyorsunuz, sadece döngü için bunu kullanın. `` PointArray [Düğüm] == NULL`,/`Node` tanımı nedir vs;` öndere> val = Düğüm:

pointArray = malloc(size_Of_array * sizeof(struct node *)); 
for (z =0; z<size_Of_array; z++) 
    pointArray[z] = NULL; 
+0

ben 'size_Of_array' kullanılan bir boyutlu dizinin indeksi olarak başvurmak gerekiyor ama sorun hala –

+0

0 ve size_Of_array – Pemdas

+0

arasında Düğüm Evet, sınırlı. Kafam karıştı, Tek boyutlu işaretçi dizisinin beyanı doğru mu? –