C变量覆盖值

时间:2017-03-30 00:03:55

标签: c pointers

您好我实现了一个链表,我无法更新我创建的结构进程的变量。以下是示例代码:

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 个答案:

答案 0 :(得分:5)

AFAIK,pid = pid++;是未定义的行为。

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

<强>更新

您在打印后设置了proc->pid ,因此它将打印为零。实际上,它打印的内容是malloc返回的内容,所以它可能是任何东西,而且大部分时间恰好都是零。

但是,你还有另一个问题。您可以在循环的底部附加一个新节点,以期在 next 循环迭代中读取节点的内容。

所以,因为你附加了“一个提前”,结果链表将有一个太多的节点,最后一个节点将有垃圾数据。你没有在循环中看到这一点,但后续列表遍历[和打印]期间

我创建了两个版本的程序。一个带有注释的错误。还有一个清理过的版本。

这是带注释的版本[正确打印]:

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