【问题标题】:How to set up Dynamic memory and temporary buffer?如何设置动态内存和临时缓冲区?
【发布时间】:2012-11-12 21:45:12
【问题描述】:

我一直在尝试完成这段代码,但我一直在创建一个临时缓冲区。我以前从未学过这个,但不知何故我需要在我的程序中使用它。

来自this website我认为最好的选择是

char * func1() {
     char *buffer = (char *)malloc(1000);
     buffer[0] = '\0'; // Initialize buffer
     // Do processing to fill buffer
     return buffer;
}

以下是我的代码

 #include <stdio.h>
#include <stdlib.h>
#define LUNCHES 5
#define ARRAY 2

int main(void)
{
    int x;
    struct Food
    {
        char *name;                                                            /* “name” attribute of food */
        int weight, calories;                                                  /* “weight” and “calories” attributes of food */
    }lunch[LUNCHES] = { [0] = {"apple", 4, 100}, [1] = {"salad", 2, 80} };

    for(x = ARRAY; x < LUNCHES; ++x)
    {
        char *buff = malloc(sizeof(lunch[x].name));

        printf("Please input \"food\", weight, calories: ");
        scanf("%s", buff);
        scanf("%d %d", &lunch[x].weight, &lunch[x].calories);
        printf("The %s weighs %doz. and contains %d calories.\n", lunch[x].name, lunch[x].weight, lunch[x].calories);


    }

    return 0;
}

好的,改变了这一点。但是现在的输出是

NULL 称重并包含 .为什么为空?

更正

#include <stdio.h>
#include <stdlib.h>
#define LUNCHES 5
#define ARRAY 2

int main(void)
{
    int x;
    struct Food
    {
        char *name;                                                            /* “name” attribute of food */
        int weight, calories;                                                  /* “weight” and “calories” attributes of food */
    }lunch[LUNCHES] = { [0] = {"apple", 4, 100}, [1] = {"salad", 2, 80} };

    for(x = ARRAY; x < LUNCHES; x++)
    {
        lunch[x].name = malloc(25 * sizeof(char));

        printf("Please input \"food\", weight, calories: ");
        scanf("%s", lunch[x].name);
        scanf("%d %d", &lunch[x].weight, &lunch[x].calories);
        printf("The %s weighs %doz. and contains %d calories.\n\n", lunch[x].name, lunch[x].weight, lunch[x].calories);

        free(lunch[x].name);

    }

    return 0;
}

【问题讨论】:

  • 不,这是不对的,你真的需要再看一遍 for 循环。
  • 好的。你需要说lunch[x].name = malloc(952);,然后说scanf("%951s", lunch[x].name)
  • 我必须分配内存大小吗?因为我想让程序弄清楚要使用多少。
  • 程序应该如何做到这一点?
  • 您正在使用缓冲区进行用户输入。你怎么知道用户会输入多少?您应该分配一个合理的大小,然后在您的scanf 格式字符串中使用这个大小,以确保您不会阅读更多内容。

标签: c memory dynamic malloc


【解决方案1】:

首先,它是for(x = ARRAY; x &lt; LUNCHES; ++x) - 注意&lt; 而不是&lt;=,否则你会溢出数组(索引从零开始,它从0 运行到LUNCHES-1)。

至于分配:

  • 您需要为 lunch[] 数组中的每个条目创建缓冲区,因此在 for 循环中您需要类似 lunch[x].name = malloc(SIZE) 的内容,其中 SIZE 是一个合理的值 - 就餐名而言,大约 80 个字符将似乎绰绰有余;
  • 接下来您必须检查分配给lunch[x].name 的指针不是NULL,这将发出内存不足的信号 - 否则您可能会通过取消引用它而导致分段错误;
  • 那么您可以使用指针(指向新分配的缓冲区)作为scanf() 的参数,但请记住指定最大宽度(即SIZE-1),这样您就不会溢出到未分配的内存中。

当您不再需要数据或程序结束时,请记住在指向已分配内存的指针上使用free() - 而在您的简单示例中,技术上没有必要,它很容易开始非常糟糕习惯。

【讨论】:

  • 我知道免费的。我只是还没到那个地步。谢谢
  • 难道不能让程序根据餐名字符串的大小计算出要使用多少内存吗?
  • 当然,如果您知道要使用多少物品。但接下来的问题是,为什么不直接将数组放入struct中,如struct Food { char name[NAMELEN]; ... }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-08
  • 1970-01-01
  • 2013-03-21
  • 2018-05-09
  • 2012-12-30
  • 1970-01-01
相关资源
最近更新 更多