C语言 关于内存分配的初学者错误[重复]

5us2dqdw  于 2023-05-16  发布在  其他
关注(0)|答案(1)|浏览(111)

此问题已在此处有答案

Dynamic memory access only works inside function(1个答案)
3天前关闭。
作为一个初学者程序员,我最近开始学习c语言的基础知识,我试着做了一个程序,从一个文本文档中读取姓氏和姓氏,并将它们存储在结构体中,并打印出结构体的内容,但是当执行时,我不能得到任何输出,只有随机的无意义字符。我做错了什么?,我假设我的malloc()用法有问题,但我不确定。
主文件

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define N 100

struct person {
    char surname[N];
    char lastname[N];
};

_Bool read_ord(char*surname, char*lastname, FILE* f){ 
    char *temp =(char*) malloc(1000);
    fgets(temp,N,f);

    surname = strtok(temp," ");
    lastname = strtok(NULL, " ");

    if(surname != NULL && lastname != NULL){
        return 1;
    }
    else
        return 0;

}


int main (){
    FILE *infil = fopen("personer.txt", "r");

    struct person p[10];
    char *surn;
    char *lastn;
    int a = 0;

    while(read_ord(surn, lastn, infil) == 1){
        strcpy(p[a].surname, surn);
        strcpy(p[a].lastname, lastn);
        printf("%s %s\n", p[a].surname, p[a].lastname);
        a++;
    }

}

personer.txt

Svensson Lars
Bolinder Annika
Kristersson Uffe
Andersson Lina
Stensdotter Erik
amrnrhlw

amrnrhlw1#

我们不需要在上面提供的代码中进行任何内存分配。
你遇到的问题是,你在read_order中获取了一个char*,它是指针值的副本,而不是你应该获取一个char**,因为它是指针地址的副本。
你还忘了为temp变量调用free。造成内存泄漏。虽然项目中的内存泄漏不是程序无法工作的原因。

/* original */

_Bool read_ord(char*surname, char*lastname, FILE* f){ 
    char *temp =(char*) malloc(1000);
    fgets(temp,N,f);

    surname = strtok(temp," ");
    lastname = strtok(NULL, " ");

    if(surname != NULL && lastname != NULL){
        return 1;
    }
    else
        return 0;
}

/* kinda fixed still memory leak */

_Bool read_ord(char** surname, char** lastname, FILE* f){ 
    char *temp =(char*) malloc(1000);
    fgets(temp,N,f);

    *surname = strtok(temp," ");
    *lastname = strtok(NULL, " ");

    if(*surname != NULL && *lastname != NULL){
        return 1;
    }
    else
        return 0;
}

我把你的整个程序重写了一遍,加上一些注解和解释,并修复了一些问题。
这是我的版本

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

/* include stdbool to get access to bool, true and false */
#include <stdbool.h>

#define N 100

struct person {
    char surname[N];
    char lastname[N];
};

bool read_ord(char* surname, char* lastname, FILE* fd)
{
    /* we know we that the one line can only be max 2N + 1 long plus null terminator */
    char tmp[2*N + 2];

    /* get line */
    fgets(tmp, 2*N + 1, fd);

    /* get the pointers to surname and lastname */
    char* tsrun = strtok(tmp, " ");
    char* tlastn = strtok(NULL, " ");

    /* check if both surname and lastname were found */
    if(tsrun != NULL && tlastn != NULL) 
    {
        /* copy from tmp to surname */
        strcpy(surname, tsrun);

        /* copy from tmp to lastname */
        strcpy(lastname, tlastn);
        
        return true;
    }

    return false;
}

int main()
{
    FILE* infile = fopen("personer.txt", "r");

    struct person p[10];
    int a = 0;

    /* 
    add check to see if a < 10 for buffer overflow
    also no need for temporary addresses for surname and lastname
    */
    while(read_ord(p[a].surname, p[a].lastname, infile) && a < 10)
    {
        printf("%s %s\n", p[a].surname, p[a].lastname);
        a++;
    }

    /* close file */
    fclose(infile);
    
    return 0;
}

相关问题