【问题标题】:Segmentation fault in copying head of linked list链表拷贝头分段错误
【发布时间】:2016-07-28 20:29:35
【问题描述】:

我有一个一维指针数组,我用它来存储链表的头部。我声明它并填写这些陈述

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

然后我在每个链表中声明要搜索的指针

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

这个语句出现问题

comparedNode = comparedNode->next;

这个指针保持为空 谁能告诉我为什么会这样?

【问题讨论】:

  • 这些没有意义:head-&gt;val = Node;pointArray[Node] == NULL 等。Node 的定义在哪里/是什么?
  • 它是整数。我有多个节点,每个节点都有邻接列表,所以我需要将节点号称为一维数组的索引

标签: c arrays pointers segmentation-fault


【解决方案1】:
pointArray =  malloc(size_Of_array * sizeof(struct node *));
for (z =0; z<sizeof(pointArray); z++)
    pointArray[z] = NULL;

sizeof(pointArray) 在您的示例中返回指针的大小(以字节为单位),而不是数组中的元素数。如果 size_Of_array

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

【讨论】:

  • 我使用了size_Of_array,但问题仍然存在
  • 是介于 0 和 size_Of_array 之间的节点
  • 是的,它是有界的。我一头雾水,一维指针数组的声明是否正确?
  • 我使用了eclipse调试器。我看到pointArray[0] 有一个我想要的完整链表,但是当我想查看像pointArray[10] 这样的其他值时,我看不到它我的意思是当我用鼠标指向它时,它在这个箭头下有一个箭头,一个值是pointArray[0]
  • 我猜测数组没有正确填充。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-10
  • 1970-01-01
  • 2011-06-08
  • 2014-12-21
  • 1970-01-01
  • 2018-04-15
相关资源
最近更新 更多