free
| معرفة في ملف <stdlib.h>
|
||
void free( void* ptr ); |
||
تقوم بتحرير مساحة سبق حجزها بـ malloc() أو calloc() أو realloc().
في حالة أن ptr يكافئ NULL تكون الدالة بدون تأثير.
يكون السلوك غير معرف عندما يكون ptr غير مكافيء لمؤشر لم ينتج عن استدعاء لـ malloc() أو calloc() أو realloc().
السلوك أيضا غير معرف إذا كانت مساحة الذاكرة المشار إليها بـ ptr قد تم تحريرها من قبل (أي أنه تم استدعاء free() or realloc() بـ ptr كمعطى ولم تقم أي استدعاءات لاحقة لـ malloc() أو calloc() أو realloc() بإرجاع مؤشر مكافيء لـ ptr).
المعطيات
|
A call to free that deallocates a region of memory synchronizes-with a call to any subsequent allocation function that allocates the same or a part of the same region of memory. This synchronization occurs after any access to the memory by the deallocating function and before any access to the memory by the allocation function. |
(منذ C11) |
| ptr | - | مؤشر على مساحة من الذاكرة ينبغى تحريرها (توفيرها لباقي النظام) |
القيمة المُرجعة
(لا شيء)
مثال
| This section is incomplete Reason: simplify or make more practical |
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
/* Allocate an array with 100 integers. */
int *pa = malloc(100*sizeof(int));
if (pa == NULL) { /* check that memory allocation occurred */
printf("malloc() failed in file %s at line # %d", __FILE__,__LINE__);
printf("*** PROGRAM TERMINATED ***");
exit(1);
}
printf("starting address of pa: %p\n", (void*)pa);
/* Deallocate array pa. */
free(pa), pa = NULL;
/* Allocate an array with 100 doubles. */
double *pb = malloc(100*sizeof(double));
if (pb == NULL) { /* check that memory allocation occurred */
printf("malloc() failed in file %s at line # %d", __FILE__,__LINE__);
printf("*** PROGRAM TERMINATED ***");
exit(1);
}
/* Array pb may have the same starting address that array pa had. */
printf("starting address of pb: %p\n", (void*)pb);
free(pb);
return 0;
}
خرج ممكن:
starting address of pa: 0x2113010
starting address of pb: 0x2113010
أنظر أيضا
مقالة مرجع C++ عن free
|