【问题标题】:getting free(): invalid size when executing code获得免费():执行代码时大小无效
【发布时间】:2021-08-31 17:56:14
【问题描述】:

使用 gcc hw2.c -o x -lpthread 编译

free(): invalid size
free(): invalid size
free(): invalid size
nano infile.txt

我假设它可能与文件指针有关?信号量已被注释掉以首先解决此问题。所有答案都指向指针,但切换起来并没有多大帮助。我尝试过不同的编译方式,但也无济于事。

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <semaphore.h>
#include <fcntl.h>
#include <sys/stat.h>

//sem_t X;

void process(){
    //sem_open("X", O_CREAT,0777,0);
    int ret;
    int N = 1;
    pid_t pid;
    FILE* infile = fopen ("infile.txt", "r");

    for(int i = 0; i< 50; i++){
        fscanf (infile, "%d", &N);
        fclose (infile);
        printf("N: %d Process ID: %d",N,pid);
        infile = fopen("infile.txt", "w");
        N++;
        //sem_post(&X);
        fprintf(infile,"%d",N);
        fflush(infile);
        fclose(infile);
    }
    printf("\n");
    int c;
    //sem_getvalue(&X,&c);
    printf(" \n \n \n%d",c);
}
int main(){
    int pid, pid1, pid2;
    pid = fork();
    if(pid == 0){
        //child1, Last
        printf("Starting Process C: ");
        process();
    }
    else{
        pid1 = fork();
        if(pid1 == 0){
            //child2, Middle
            printf("Starting Process B: ");
            process();
        }
        else{
            pid2 = fork();
            if(pid2 == 0){
                //child 3, First
                printf("Starting Process A: ");
                process();
            }
            else{
            }
        }
        
    }
    //sem_close(&X);
    //sem_unlink(&X);
}

【问题讨论】:

  • sem_post(&amp;X)sem_close(&amp;X); 等会导致未定义行为,因为您从未设置过X。您应该将 X 声明为 sem_t *X 并将其分配给 X = sem_open()
  • printf("N: %d Process ID: %d",N,pid); 读取未初始化的 pid。 - 您还应该在您的子进程上waitpid 在退出父进程之前完成。我还建议在process() 函数的末尾添加一个exit()
  • 这是运行时错误,不是编译器错误。
  • 顺便说一句,您不需要-lpthread,但我建议-Wall -Wextra -pedantic -pedantic-errors-g -fsanitize=address 更容易发现错误。

标签: c compiler-errors process pthreads


【解决方案1】:

你的循环有问题

FILE* infile = fopen ("infile.txt", "r"); //1

for(int i = 0; i< 50; i++){
    fscanf (infile, "%d", &N);
    fclose (infile); //2
    printf("N: %d Process ID: %d",N,pid);
    infile = fopen("infile.txt", "w"); //3
    N++;
    //sem_post(&X);
    fprintf(infile,"%d",N);
    fflush(infile);
    fclose(infile); //4
}

您在1 打开文件,然后进入循环,在2 关闭它,在3 重新打开它并在4 重新关闭它。在下一次迭代中,当您尝试在2 关闭时,您会遇到双重释放,因为它已经4 关闭。

【讨论】:

    【解决方案2】:

    最明显的问题在于您的循环。除了fopenfclose 调用之外,我已经删除了所有内容:

    FILE* infile = fopen ("infile.txt", "r");
    
    for(int i = 0; i< 50; i++){
        fclose (infile);
        infile = fopen("infile.txt", "w");
        fclose(infile);
    }
    

    正如您现在可能看到的,当 i1 时,您尝试使用 fclose(infile) - 但它没有打开,因此您会得到错误。

    您需要将第一个 fopen 移动到循环中 - 并检查打开文件并从中读取是否也成功:

    #include <fcntl.h>
    #include <stdbool.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    void process() {
        int N = 1;
        for(int i = 0; i < 50; i++) {
            FILE* infile = fopen("infile.txt", "r");
            if(infile) {
                bool readok = fscanf(infile, "%d", &N) == 1;
                fclose(infile);
    
                if(readok) { /* only do this if a value was read from the file ok */
                    printf("N: %d Process ID: %d", N, getpid());
                    infile = fopen("infile.txt", "w");
                    if(infile) {
                        N++;
                        fprintf(infile, "%d", N);
                        fflush(infile);
                        fclose(infile);
                    }
                }
            }
        }
        printf("\n");
    
        exit(0); /* terminate this sub process */
    }
    
    int main() {
        const size_t kPids = 3;
    
        pid_t pids[kPids]; /* simplify keeping a number of background processes */
    
        for(size_t i = 0; i < kPids; ++i) {
            pids[i] = fork();
            if(pids[i] == 0) {
                printf("Starting Process %c:\n", (char)('A' + i));
                process();
            }
        }
    
        /* wait for children to finish */
        pid_t pid;
        int wstatus;
        while((pid = wait(&wstatus)) != -1) {
            printf("pid %d is done with status %d\n", pid, wstatus);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      • 2018-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多