int *a = malloc(sizeof(*a)); if (a) { /* a is now valid; use it: */ *a = 1 + 2 + 3; printf("The value calculated is %d\n", *a); } /* Variable A is done being used; free the memory. */ free(a); /* If a failed to be allocated, it is NULL, and this call is safe. */
int *a = (int*)malloc(sizeof(int));
我应该在代码完成时释放一个吗?
问题应该是
代码完成后我必须释放一个吗?
答案是肯定的。一个 malloc 必须附有a free 声明。
malloc
free
free(a);