c - 在链接列表中查找循环(​​获取段错误)

标签 c loops pointers linked-list

此解决方案基于对计算机的一项非常重要的观察。 我系统中的指针大小是 8 个字节。 这个结构的大小

 struct ll1
 {  
  int data;
  struct ll1 *next;
 };

16 字节。 作为具有多个成员的结构指针,总是会有一些尾随零。这是由于内存对齐。 所以我试图用剩下的 4 个字节来存储访问过的标志。 当我将 0 分配给那四个字节时,它不会给出错误但是 当我分配零以外的值时,它会在下面的代码中给出一个段错误。 请问有人解释为什么会这样吗?

#include<stdio.h>
#include<stdlib.h>
struct ll1
{
    int data;
    struct ll1 *next;
};
typedef struct ll1 ll;

void create(ll **root)
{

int t=1;
printf("\nEnter node value 0 if end:");
    scanf("%d",&t);
    if(t)
    {
        (*root)=(ll*)malloc(sizeof(ll))    ;
        (*root)->data=t;
        create(&(*root)->next); 
    }
    else
    (*root)=NULL;
}
int main()
{

    ll *node;
    create(&node);
    ll *temp=node,*temp2=node;
int j,size=0;

/*printing 4-4 bytes of the node */
while(temp->next)
{
    size++;
    int *p=(int *)temp;
    printf("\n%d %d %d %d",*(p),*(p+1),*(p+2),*(p+3));
    *(p+3)=0; // setting the value of last four byte zero
    temp=temp->next;

} 
printf("\n");

j=size/3;

/* making loop in the linklist */

while(j--)temp2=temp2->next;
temp=node;

while(temp->next!=NULL)temp=temp->next;
temp->next=temp2;   

/*loop created */



/* code for finding loop in the linklist */
printf("\nfinding loop in the linklist\n");
ll *root=node;  
int *pp=(int *)root;
printf("%d %d %d %d\n",*(pp),*(pp+1),*(pp+2),*(pp+3));
printf("%d \n",root->data);
while(*(pp+3)==0) 
{

    *(pp+3)=1; //when changed that line by *(pp+3)=0 it doesn't give any error 
    root=root->next; //move pointer to next
    pp=(int *)root;  // assign adress to integer pointer
    printf("%d %d %d %d\n",*(pp),*(pp+1),*(pp+2),*(pp+3));  // segmentation fault is here 
    printf("%d \n",root->data);
}
printf("%d \n",root->data);
return 0;

}

在GDB中输出

(gdb) r 
Starting program: /home/kushagra/place/linklist/a.out <ll_in

Enter node value 0 if end:1
Enter node value 0 if end:2
Enter node value 0 if end:3
Enter node value 0 if end:4
Enter node value 0 if end:5
Enter node value 0 if end:6
Enter node value 0 if end:7
Enter node value 0 if end:8
Enter node value 0 if end:8
Enter node value 0 if end:0
1 0 6299696 0
2 0 6299728 0
3 0 6299760 0
4 0 6299792 0
5 0 6299824 0
6 0 6299856 0
7 0 6299888 0
8 0 6299920 0
8 0 6299952 0

finding loop in the linklist
1 0 6299696 0
1 

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400a2e in main () at P_node.c:72
72          printf("%d %d %d %d\n",*(pp),*(pp+1),*(pp+2),*(pp+3));  // segmentation fault is here 

最佳答案

它没有“尾随零”。 int 和指针之间可能有 4 个未使用的字节。

这个 *(pp+3)=1 可能修改了指针中的四个字节。

使用:

struct ll1
 {  
  int data;
  int visited;
  struct ll1 *next;
 };

关于c - 在链接列表中查找循环(​​获取段错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17209388/

相关文章:

c - 用于编译后缀表达式的数据结构

c - 哪个 boost 宏允许我在程序中插入可变数量的语句

javascript - Canvas 地球在 12 次迭代后停止在 "spinning"。

无法增加取消引用的指针的值

c - 如何使用 sizeof 将数组的大小作为另一个参数传递

c - 将文件写入内存缓冲区(fopen something,但写入缓冲区,而不是磁盘)

c - 在没有arduino的情况下在atmega32上编程电子墨水显示屏

loops - 网格计算并导出为 CSV

java - 在迭代 hashmap 时执行循环展开

c - 为什么会出现这些错误?