2017-03-30 157 views
-2

您好我實現了一個鏈表,我有麻煩更新我創建的結構過程的變量。下面是示例代碼:C變量覆蓋值

typedef struct Process { 
    int pid; 
    char name[256]; 
    int prior;  
    int state; 
    int start_time; 
} Process; 

typedef struct Node { 
    Process *value; 
    struct Node *next; 
} Node; 


Node *create_node(){ 
    Node *temp = malloc(sizeof(Node)); 
    temp->value = NULL; 
    temp->next = NULL; 
    return temp; 
} 

void append(Node *head, Node *nodo){ 
    Node *current = head; 
    while (current->next != NULL){ 
     current = current->next; 
    }  
    current->next = nodo; 
} 



void add_attr(char *string, Process *procc){  
    char *pch; 
    pch = strtok(string, " "); 
    for (int i = 0; i < 3; i++){ 
     if (i == 0){ 
      strcpy(procc->name,pch);    
     } 
     else if(i == 1){   
      int aux = atoi(pch);    
      procc->prior = aux;         
     } 
     else{    
      int aux1 = atoi(pch); 
      procc->start_time = aux1;     
     } 
     pch = strtok(NULL, " "); 
    } 


int main(int argc, char * argv []) {  
    FILE *fp; 
    int pid = 0; 
    char *line = NULL; 
    size_t len = 0; 
    ssize_t read; 
    fp = fopen(argv[1],"r"); 
    Node *process_list = create_node();  
    Process *proc = malloc(sizeof(Process)); 
    proc->pid = pid; 
    proc->state = 0; 
    process_list->value = proc; 
    pid += 1 ; 
    while ((read = getline(&line, &len, fp)) != -1) { 
     printf("%s\n",line);   
     add_attr(line, proc); 
     printf("---------------------------------------------------------\n"); 
     printf("pointer proc memory dir = %p\n", proc); 
     printf("pid = %d\n",proc->pid); 
     printf("name = %s\n",proc->name); 
     printf("pior = %d\n",proc->prior); 
     printf("state = %d\n",proc->state); 
     printf("start_time = %d\n",proc->start_time); 
     printf("----------------------------------------------------------\n"); 
     Node *nodo = create_node(); 
     Process *proc = malloc(sizeof(Process)); 
     proc->pid = pid;  
     proc->state = 0; 
     nodo->value = proc; 
     append(process_list, nodo); 
     pid = pid +1; 
    }  
    fclose(fp); 
    return 0; 
} 

有主() 你可以看到我打印的結構體變量的狀態,看看他們的價值觀和一切順利的話,除了未改變PID 。 while循環完成後,我打印了鏈接列表中的所有進程及其屬性,並且它們全部都已更改。在這裏你可以看到一個帶輸出的SS。 enter image description here

我真的不知道發生了什麼事我的計劃有任何幫助將是巨大的,我知道它的一個非常特殊的情況下,但我不知道如何做一個工作的例子,顯示了同樣的問題。 (*我現在更新了pid的輸出結果,但主要問題並沒有解決,我仍然無法弄清楚爲什麼Process attr會改變)。

Input sample: 
p1 2 3 10 1 2 3 4 5 6 7 8 8 9 
p2 1 4 8 6 2 6 4 3 2 2 1 
p3 3 5 5 1 2 6 7 8 
+1

'PID = PID ++'是無效的。它應該是'pid ++'或'pid = pid + 1' – Barmar

+2

將代碼發佈爲文本,而不是圖像。 – Barmar

+0

'Process * proc = malloc(sizeof(Process)); PROC-> PID ++;'。考慮到'proc-> pid'是未分配的,你試圖增加一個隨機值(它可能是0,但可以是任何東西,因爲這是一個不明確的行爲)。你可能想'proc-> pid = pid ++;' – Evert

回答

5

AFAIK,pid = pid++;是未定義的行爲。

使用pid = pid + 1pid++,或pid += 1代替


UPDATE:

你被設置proc->pid印刷,所以將打印爲零。實際上,它打印的是malloc返回的內容,所以它可能是任何東西,大部分時間恰好爲零。

但是,你也有另一個問題。您可以在循環底部附加一個新節點,以便在讀取下一個循環迭代中的節點內容。

所以,因爲你在前面添加了「one」,所得的鏈表將會有一個節點太多,而最後一個節點會有垃圾數據。你沒有看到這個循環本身,但期間後續列表遍歷[和打印]。

我已經創建了兩個版本的程序。一個與bug註釋。和一個清理版本。

這裏的註釋版本[其正確打印]:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

typedef struct Process { 
    int pid; 
    char name[256]; 
    int prior; 
    int state; 
    int start_time; 
} Process; 

typedef struct Node { 
    Process *value; 
    struct Node *next; 
} Node; 

Node * 
create_node() 
{ 
    Node *temp = malloc(sizeof(Node)); 

    temp->value = NULL; 
    temp->next = NULL; 
    return temp; 
} 

void 
append(Node * head, Node * nodo) 
{ 
    Node *current = head; 

    while (current->next != NULL) { 
     current = current->next; 
    } 
    current->next = nodo; 
} 

void 
add_attr(char *string, Process * procc) 
{ 
    char *pch; 

    pch = strtok(string, " "); 
    for (int i = 0; i < 3; i++) { 
     if (i == 0) { 
      strcpy(procc->name, pch); 
     } 
     else if (i == 1) { 
      int aux = atoi(pch); 

      procc->prior = aux; 
     } 
     else { 
      int aux1 = atoi(pch); 

      procc->start_time = aux1; 
     } 
     pch = strtok(NULL, " "); 
    } 
} 

int 
main(int argc, char *argv[]) 
{ 
    FILE *fp; 
    int pid = 0; 
    char *line = NULL; 
    size_t len = 0; 
    ssize_t read; 

    fp = fopen(argv[1], "r"); 
    Node *process_list = create_node(); 
    Process *proc = malloc(sizeof(Process)); 

    proc->pid = pid; 
    proc->state = 0; 
    process_list->value = proc; 
    pid += 1; 
    while ((read = getline(&line, &len, fp)) != -1) { 
     printf("%s\n", line); 
     add_attr(line, proc); 

// NOTE/FIX: this is the correct place to set the pid -- _before_ printing 
#if 0 
     proc->pid = pid; 
#endif 

     printf("---------------------------------------------------------\n"); 
     printf("pointer proc memory dir = %p\n", proc); 
     printf("pid = %d\n", proc->pid); 
     printf("name = %s\n", proc->name); 
     printf("pior = %d\n", proc->prior); 
     printf("state = %d\n", proc->state); 
     printf("start_time = %d\n", proc->start_time); 
     printf("----------------------------------------------------------\n"); 

// NOTE/BUG: this is setting up the _next_ node before it is known if it will 
// be filled 
     Node *nodo = create_node(); 
     Process *proc = malloc(sizeof(Process)); 

// NOTE/BUG: this is set _after_ the printing is done 
#if 1 
     proc->pid = pid; 
#endif 

// NOTE/BUG: this is appending the node before it is filled in (i.e. the last 
// node in the list will have garbage) 
     proc->state = 0; 
     nodo->value = proc; 
     append(process_list, nodo); 

     pid = pid + 1; 
    } 

    fclose(fp); 
    return 0; 
} 

這裏的清理和工作版本:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

typedef struct Process { 
    int pid; 
    char name[256]; 
    int prior; 
    int state; 
    int start_time; 
} Process; 

typedef struct Node { 
    Process *value; 
    struct Node *next; 
} Node; 

Node * 
create_node() 
{ 
    Node *temp = malloc(sizeof(Node)); 

    temp->value = NULL; 
    temp->next = NULL; 
    return temp; 
} 

void 
append(Node * head, Node * nodo) 
{ 
    Node *current = head; 

    while (current->next != NULL) { 
     current = current->next; 
    } 
    current->next = nodo; 
} 

void 
add_attr(char *string, Process * procc) 
{ 
    char *pch; 

    pch = strtok(string, " "); 
    for (int i = 0; i < 3; i++) { 
     if (i == 0) { 
      strcpy(procc->name, pch); 
     } 
     else if (i == 1) { 
      int aux = atoi(pch); 

      procc->prior = aux; 
     } 
     else { 
      int aux1 = atoi(pch); 

      procc->start_time = aux1; 
     } 
     pch = strtok(NULL, " "); 
    } 
} 

int 
main(int argc, char *argv[]) 
{ 
    FILE *fp; 
    int pid = 0; 
    char *line = NULL; 
    size_t len = 0; 
    ssize_t read; 

    fp = fopen(argv[1], "r"); 
    Node *process_list = create_node(); 
    Process *proc = malloc(sizeof(Process)); 

    proc->pid = pid; 
    proc->state = 0; 
    process_list->value = proc; 

    while ((read = getline(&line, &len, fp)) != -1) { 
     printf("%s\n", line); 

     Process *proc = malloc(sizeof(Process)); 
     add_attr(line, proc); 
     proc->state = 0; 

     pid += 1; 
     proc->pid = pid; 

     Node *nodo = create_node(); 
     nodo->value = proc; 
     append(process_list, nodo); 

     printf("---------------------------------------------------------\n"); 
     printf("pointer proc memory dir = %p\n", proc); 
     printf("pid = %d\n", proc->pid); 
     printf("name = %s\n", proc->name); 
     printf("pior = %d\n", proc->prior); 
     printf("state = %d\n", proc->state); 
     printf("start_time = %d\n", proc->start_time); 
     printf("----------------------------------------------------------\n"); 
    } 

    fclose(fp); 

    return 0; 
} 
+0

抱歉,我的壞它是一箇舊版本,但我修好了,它也沒有工作。 –

+0

我想知道爲什麼這個答案得到了DV'ed,特別是在得到+2 –

+0

因爲它沒有幫助,這是我的壞上傳和舊版本,但如果你想我可以刪除這些帖子,並再次上傳正確的版本,我不明白爲什麼一個答覆根本沒有幫助應該得到投票 –