嗨,我实现了一个链表,我遇到了更新我创建的结构进程的变量的麻烦.以下是示例代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
你可以看到main()我在结构中打印变量的状态以查看它们的值,除了没有改变的pid之外一切顺利.在while循环结束后,我打印了我的链表及其属性中的所有进程,并且它们都已更改.在这里,您可以看到带有输出的SS.

我真的不知道我的程序发生了什么,任何帮助都会很棒,我知道这是一个非常具体的案例,但我不知道如何制作一个显示同样问题的工作示例.(*我现在更新了输出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
Run Code Online (Sandbox Code Playgroud)
AFAIK,pid = pid++;是未定义的行为.
使用pid = pid + 1,pid++或pid += 1代替
更新:
您proc->pid 在打印后进行设置,因此打印为零.实际上,它所印刷的是什么malloc,所以它可能是任何东西,而且恰好在大多数情况下恰好为零.
但是,你还有另一个问题.您可以在循环的底部追加一个新节点,以期在下一次循环迭代中读取节点的内容.
因此,因为您附加了"一个提前",结果链表将有一个太多的节点,最后一个节点将有垃圾数据.你不会在循环本身中看到这一点,但会在后续的列表遍历[和打印]中看到.
我已经创建了两个版本的程序.一个带有注释的错误.还有一个清理过的版本.
这是带注释的版本[正确打印]:
#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;
}
Run Code Online (Sandbox Code Playgroud)
这是清理和工作版本:
#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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
73 次 |
| 最近记录: |