请问这里应该怎么做?要求是说它需要使用 malloc 和 free。并且要求一旦程序运行起来,要确保正确使用了“free”函数来释放内存,然后将指针设置为等于 NULL。求大lao帮我编程一下。蟹蟹
#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
英文能翻译一下不,看不懂啊
// TODO BELOW
// part 1: Complete the "createInteger" function that uses
// malloc to store a new integer on the heap
int * createInteger(int value) {
int *heap=(int *)malloc(sizeof(int)*1);
*heap=value;
return heap; // 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) {
double *heap=(double *)malloc(sizeof(double)*1);
*heap=value;
return heap; // You don't want to return null
}
// TODO ABOVE
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
free(heapInteger);
heapInteger=NULL;
free(heapDouble);
heapDouble=NULL;
// TODO ABOVE
return 0;
}