简体   繁体   中英

This code is generating a segment core dump

This code is generating a segment core dump.

typedef struct linked{
        int val;
        struct linked *index;
}linked;


    struct linked *temp1;

     int count=1;
     while(count<10){

           temp1->val=count;
           temp1=temp1->index;

           count++;

     } //end of while

     while(temp1!=NULL){
           printf(" %d\n",temp1->val);
           temp1=temp1->index;
     }
struct linked *temp1 = malloc(sizeof(struct linked));

将内存分配给指针

In

struct linked *temp1;

int count=1;
while(count<10){

      temp1->val=count;

temp1 is never made to point to anything.

struct linked *temp1, *top;
int count;

temp1 = top = calloc(1, sizeof(*top));
for(count=1;count<10;count++){
    temp1->val = count;
    if(count < 10 -1)//not last
        temp1 = temp1->index = calloc(1, sizeof(*temp1));
}
temp1 = top;
 while(temp1!=NULL){
    printf(" %d\n",temp1->val);
    temp1=temp1->index;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM