简体   繁体   English

从C函数返回的字符串是垃圾

[英]String returned from C function is garbage

i've been working on a RPG in C language lately, and i've come accross some difficulties for saving / loading some datas from a text file. 我最近一直在从事C语言的RPG的开发,但是在从文本文件中保存/加载某些数据时遇到了一些困难。

Here is the (simplified to make it short) code : 这是(简化起见)代码:

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

struct player{
    int id;
    char* name;
};

void save(struct player* player)
{
    FILE* fp = 0;
    char* buffer = 0;

    fp = fopen("./gamesave.txt", "w");
    fclose(fp);
    buffer = malloc(80);
    memset(buffer, 0, 80);

    snprintf( buffer, 80, "%d|%s|\n", player->id, player->name);
    fp = fopen("./gamesave.txt", "a");
    fputs(buffer, fp);
    free(buffer);
    fclose(fp);
}

struct player* load()
{
    FILE* fp = 0;
    char* buffer = 0;
    struct player* player = malloc(sizeof(struct player));
    char tokens[] = "|\n";

    buffer = malloc (80);
    memset(buffer, 0, 80);

    fp = fopen("./gamesave.txt", "r");

    if (fgets(buffer, 80, fp)) {
        char temp[20];
        player->id = atoi(strtok(buffer, tokens));
        strcpy(temp, strtok(NULL, tokens));
        player->name = temp;
        printf("during load : name is %s\n", player->name);
    }
    fclose(fp);
    free(buffer);
    return player;
}

void main()
{


    struct player* player = malloc(sizeof(struct player));
    player->id = 5;
    player->name = "Bobby";
    printf("before save : my name is %s and id is %d\n", player->name, player->id);

    save(player);
    struct player* playerN = load();
    printf("after load : name is %s and id is %d", playerN->name, playerN->id);
}

This is my latest try in doing this but i've tried many differents ways of doing it, but it almost always ends in the same problem : i can't seem to get the right values for strings although it works for integers.I can print the name of the player before save, during the load function, but after the load it just prints garbage (the id works just fine). 这是我的最新尝试,但是我尝试了许多不同的方法来完成它,但是它几乎总是以相同的问题结尾:尽管它适用于整数,但我似乎无法获得正确的字符串值。在加载之前,在加载功能期间打印播放器的名称,但是在加载之后,它只是打印垃圾(ID可以正常工作)。

My guess is that it has to do with some kind of memory problem but that's it, couldn't solve it after hours and hours. 我的猜测是它与某种内存问题有关,仅此而已,数小时后仍无法解决。

Thanks in advance. 提前致谢。

You need to allocate space for player->name . 您需要为player->name分配空间。

You are using automatic variable temp for this, and its lifetime ends when the function returns. 您正在为此使用自动变量temp ,并且其寿命在函数返回时结束。 Auto variables are on the stack. 自动变量在堆栈中。

So you are taking an address on the stack and keeping it as your object. 因此,您要在堆栈上保留一个地址,并将其作为对象。 This leaks a pointer to your current stack level because you return it. 因为您返回它,所以泄漏了指向当前堆栈级别的指针。 It's OK to use pointers to automatic variables, but only while that function instance is active, ie, in it's life cycle and in functions it calls. 可以使用指向自动变量的指针,但只能在该函数实例处于活动状态时使用,即在其生命周期和调用的函数中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM