3

このソリューションは、コンピューターに関する非常に重要な観察に基づいています。私のシステムのポインタ サイズは 8 バイトです。この構造体のサイズ

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

です16 bytes。複数のメンバーを持つ構造体ポインターとして、常にいくつかの末尾のゼロがあります。これはメモリ アライメントによるものです。そのため、残りの 4 バイトを使用して、訪問済みフラグを保存しようとしています。その4バイトに0を割り当ててもエラーは発生しませんが、ゼロ以外の値を割り当てると、以下のコードでセグメンテーション違反が発生します。plz なぜこれが起こるのか説明してくれる人はいますか?

#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 
4

2 に答える 2

6

「末尾のゼロ」はありません。とポインターの間に 4 バイトの未使用バイトがある可能性がintあります。

これにより*(pp+3)=1、おそらくポインター内の 4 バイトが変更されます。

使用する:

struct ll1
 {  
  int data;
  int visited;
  struct ll1 *next;
 };
于 2013-06-20T09:07:46.550 に答える
1

リンクされたリストの最後に到達したかどうかを確認していません。こちらも要チェックです。

while(*(pp+3)==0 && root->next) 
于 2013-06-20T09:12:29.613 に答える