簡體   English   中英

這段代碼正在生成一個段核心轉儲

[英]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));

將內存分配給指針

struct linked *temp1;

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

      temp1->val=count;

temp1永遠不會指向任何東西。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM