The use _c language of free () function and getpagesize () function in C language

Source: Internet
Author: User

C language Free () function: frees dynamically allocated memory space
header file:

#include <stdlib.h>

The free () function is used to release the dynamically allocated memory space, and its prototype is:

 void free (void* ptr);

Free () frees up the memory space allocated by malloc (), Calloc (), realloc () so that other programs can use it again.

The "parameter description" PTR is the address of the memory space that will be freed.

Free () Only frees the dynamically allocated memory space and does not release any memory. The following is the wrong wording:

int a[10];
// ...
Free (a);

If the memory space that PTR points to is not allocated by the above three functions or has been freed, then the call to free () can occur unpredictably.

If PTR is NULL, then free () has no effect.

Note: free () does not change the value of the PTR variable itself, and it still points to the same memory space after the call to free (), but the memory is now invalid and cannot be used. Therefore, it is recommended that the value of PTR be set to NULL, for example:

Free (PTR);
ptr = NULL;

code example:

#include <stdlib.h>
int main ()
 int * buffer1, * buffer2, * BUFFER3;
 Buffer1 = (int*) malloc (100*sizeof (int));
 Buffer2 = (int*) calloc (100,sizeof (int));
 Buffer3 = (int*) realloc (buffer2,500*sizeof (int));
 Free (buffer1);
 Free (BUFFER3);
 System ("pause");
 return 0;

C language GetPageSize () function: Get the memory paging size
header file:

#include <unistd.h>

To define a function:

size_t getpagesize (void);

Function Description: Returns the size of a paging in bytes (byte). This is the paging size of the system and is not necessarily the same as the hardware paging size.

Return value: Memory paging size.

Additional note: The return value on the Intel x86 should be 4096bytes.


#include <unistd.h>
Main () {
 printf ("Page size =%d\n", getpagesize ());

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.