简体   繁体   English

为什么我的C程序在每次执行时都更改输出而没有任何输入或代码更改?

[英]Why does my C program changes the output in each execution without any input or code change?

I'm working on my first program on C and it is about two process communication. 我正在开发我的第一个C语言程序,它涉及两个进程的通信。 The messages send from one process to another can vary in length and I need to store them until the receiver process need it. 从一个进程发送到另一个进程的消息的长度可能会有所不同,我需要存储它们,直到接收者进程需要它为止。 I thought that would be good to use FIFO structure which length can vary so I decided to program it. 我认为使用长度可以变化的FIFO结构会很好,因此我决定对其进行编程。 This is the code: 这是代码:

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

typedef struct Queue{
        int length;
        struct Member *first;
        struct Member *last;
}Queue;

typedef struct Member{
        char ch;
        struct Member *next;
}Member;

Queue create(){
        struct Queue cola;
        cola.length = 0;
        return cola;
} 
void add(struct Queue *q, char c){
        if(q->length == 0){
                struct Member m;
                m.ch = c;
                q->first = &m;
                q->last = &m;

        }else{
                struct Member m;
                m.ch = c;
        q->last->next = &m;
        q->last = &m;
        }   
        q->length++;
}
void print(struct Queue n){
    struct Member *p;
    p = n.first;
    for(int i = n.length; i > 0; i--){
        printf("%c,", p->ch);
        p = p->next;
    }
    printf("\n");
}

int main(){
        struct Queue cola = create();
        add(&cola, 'a');
        printf("%c\n",cola.first->ch);
        printf("%c\n",cola.last->ch);
        print(cola);
}

The problem is that, first of all, when I try to print the queue it do not works as expected (I don't know if it is problem of the print() function or the structure itself), but this is something that I can find out by myself. 问题是,首先,当我尝试打印队列时,它无法按预期工作(我不知道这是print()函数还是结构本身的问题),但这是我要解决的问题可以自己找出来。 The biggest problem, and the one that do not allow me to find the first problem, is that the answer of the program changes. 最大的问题(也是让我找不到第一个问题的问题)是程序的答案有所更改。 This is a copy of my terminal: 这是我的终端机的副本:

    $./list
a
a
p,
    $./list
a
a
�,
    $./list
a
a
 ,
    $./list
a
a
0,
    $./list
a

,
    $./list
a
a
�,

The desired output would be: 所需的输出将是:

    $./list
a
a
a,

I don't know why this happens and I would like to know it. 我不知道为什么会这样,我想知道。 Thanks! 谢谢!

   q->first = &m;
   q->last = &m;

These are poiting to a memory with automatic storage duration which will not be there once the scope on which it is declared ends - accessing it outside the scope like you did is undefined behavior. 这些将自动添加到具有自动存储持续时间的内存中,一旦声明内存的作用域终止,该内存将不再存在-像您那样在范围之外访问它是未定义的行为。

Solution would be to dynamically allocate the memory using *alloc ( malloc / calloc etc) and then you can pass them around. 解决方案是使用*allocmalloc / calloc等)动态分配内存,然后可以传递它们。 The only thing is you need to free them once you are done working with it. 唯一的事情是,一旦使用完它们,您就需要释放它们。 Something like this:- (Yes you can/should modify it as you need) 这样的事情:-(是的,您可以/应该根据需要对其进行修改)

struct Member* m = malloc(sizeof *m);
if(!m){ perror("malloc");exit(EXIT_FAILURE);}
m->ch = c;
q->first = m;
q->last = m;
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的C程序会输出这个? - Why does my C program output this? 为什么气泡排序程序的输出每次运行都会改变? - Why does the output of my bubble sort program change each time I run it? 为什么我的C代码不打印任何输出? - Why is my C code not printing any output? 为什么交换变量名会更改C程序的输出? - Why does exchanging variable name changes output of C program? 为什么这个 c 代码在我运行此代码时不接受输入。 该程序无需任何输入即可存在 - Why this c code is not accepting input from when i am running this code. this program is getting exist without taking any input 为什么我的程序给我多余的输出? C程序 - Why does my program give me the excess output? C program C语言中的基本更改程序符合但未显示任何输出 - Base change program in C complies but does not show any output 为什么每次我输入 10 进行评级输入时,我的 C 代码 output 垃圾值? - Why my C code output garbage value each time i enter 10 for rating input? 在不干扰c程序执行的情况下进行输入 - Taking input without disturbing the program execution in c Linux pthread c 中的生产者和消费者问题 - 为什么我的代码不打印任何 output? - Linux pthread Producer and consumer problem in c - why does my code not print any output?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM