2016-04-07 9 views
0

リンクリストの先頭を格納するために使用するポインタの1次元配列があります。私はその後、私はこのポインタがNULLのままの問題は、この声明の中でリンクされたリストのコピーヘッドのセグメンテーションフォールト

comparedNode = comparedNode->next; 

を発生

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 

各リンクリストで検索するようにポインタを宣言し、それを宣言し、これらの文

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

でいっぱい なぜこのようなことが起こるのか教えてください。

+0

「head-> val = Node; '、' pointArray [Node] == NULL'などここで、/ Nodeは何の定義ですか? –

+0

それはintです。私は各ノードに隣接リストがあるので、ノード番号を1次元配列のインデックスとして参照する必要があります。 –

答えて

0

sizeof(pointArray)は、配列内の要素の数ではなく、例のポインタのサイズをバイト単位で返します。 sizeof(pointArray)の場合、配列の一部が初期化されていないため、次のコードがかなり危険です。

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

あなたは既に配列のサイズを知っているので、それをループのために使用してください。

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

'size_Of_array'を使用しましたが、問題はまだ発生します。 –

+0

ノードが0からsize_Of_arrayの間です – Pemdas

+0

はい、私は混乱している、一次元ポインタ配列の宣言は正しいですか? –

関連する問題