啦啦啦拉拉裤 2021-07-24 20:07 采纳率: 69.1%
浏览 19
已结题

C语言Mallocing Memory问题,求解决

请问这里应该怎么做?要求是说它需要使用 malloc 和 free。并且要求一旦程序运行起来,要确保正确使用了“free”函数来释放内存,然后将指针设置为等于 NULL。照片第一张是原题,第二张是机翻。求帮我编程一下。蟹蟹。

img

img

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

int *createInteger(int value);
double *createDouble(double value);

int main(int argc, char* argv[]) {

    int *heapInteger = createInteger(3);
    double *heapDouble = createDouble(4.0);

    printf("heapInteger: %d\n", *heapInteger);
    printf("heapDouble: %lf\n", *heapDouble);

    // TODO BELOW
    //  part 3: don't forget to free your memory and set their values
    //          to NULL
    // TODO ABOVE
    return 0;
}

// TODO BELOW
//  part 1: Complete the "createInteger" function that uses
//          malloc to store a new integer on the heap
int *createInteger(int value) {
    return NULL; // You don't want to return null
}
// TODO ABOVE

// TODO BELOW
//  part 2: Complete the "createDouble" function that uses
//          malloc to store a new integer on the heap
double *createDouble(double value) {
    return NULL; // You don't want to return null
}
// TODO ABOVE

  • 写回答

2条回答 默认 最新

  • 关注

    参考:

    char *Ptr = NULL;
    
            Ptr = (char *)malloc(100 * sizeof(char));
    
            if (NULL == Ptr)
        {
            exit (1);
        }
    
            gets(Ptr);
    
            // code...
    
            free(Ptr);
    
            Ptr = NULL;
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月1日
  • 已采纳回答 7月24日
  • 修改了问题 7月24日
  • 创建了问题 7月24日

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值