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