|
Calloc (Configure memory space) |
Related functions |
Malloc,free,realloc,brk
|
Table header File |
#include <stdlib.h>
|
Defining functions |
void *calloc (size_t nmemb,size_t size);
|
Function description |
Calloc () is used to configure NMEMB contiguous units of memory, each of which is size, and returns a pointer to the first element. This works the same way as with the following: malloc (nmemb*size); However, when you configure memory with Calloc (), the memory content is initialized to 0.
|
return value |
Returns a pointer if the configuration succeeds, or null if failed.
|
Example |
/* Dynamic configuration of 10 struct test spaces */ #include <stdlib.h> struct test { int a[10]; Char b[20]; } Main () { struct Test *ptr=calloc (sizeof (struct test), 10); }
|
|
|
Free (releases the previously configured memory) |
Related functions |
Malloc,calloc,realloc,brk
|
Table header File |
#include <stdlib.h>
|
Defining functions |
void free (void *ptr);
|
Function description |
The parameter ptr is a pointer to a memory that was previously returned by malloc (), calloc (), or realloc (). When you call Free (), the memory space that is referred to by PTR is retracted. If the memory space referred to by the parameter PTR has been retracted or an unknown memory address, then calling free () may not be expected to happen. If the parameter PTR is null, then free () does not have any effect.
|
|
|
GetPageSize (get memory paging size) |
Related functions |
Sbrk
|
Table header File |
#include <unistd.h>
|
Defining functions |
size_t getpagesize (void);
|
Function description |
Returns the size of a page in bytes (byte). This is the paging size of the system and is not necessarily the same as the hardware paging size.
|
return value |
The paging size of the memory. Additional instructions on Intel x86 the return value should be 4096bytes.
|
Example |
#include <unistd.h> Main () { printf ("Page size =%d\n", getpagesize ()); }
|
|
|
malloc (Configure memory space) |
Related functions |
Calloc,free,realloc,brk
|
Table header File |
#include <stdlib.h>
|
Defining functions |
void * malloc (size_t size);
|
Function description |
malloc () is used to configure the memory space, which is determined by the specified size.
|
return value |
Returns a pointer if the configuration succeeds, or null if failed.
|
Example |
void P = malloc (1024); /* Configure 1k of memory */
|
|
|
Mmap (build memory map) |
Related functions |
Munmap,open
|
Table header File |
#include <unistd.h> #include <sys/mman.h>
|
Defining functions |
void *mmap (void *start,size_t length,int prot,int flags,int fd,off_t offsize);
|
Function description |
Mmap () is used to map a file's contents into memory, and access to that area of memory is directly read and written to the contents of the file. The parameter start points to the corresponding memory start address, which is usually set to NULL, which means that the system automatically selects the address and the address is returned after the successful match. The length of the parameter indicates how much of the file corresponds to memory.
|
Parameters |
Prot represents the protected mode of the map area with the following combinations Prot_exec map area can be executed Prot_read map area can be read Prot_write mapped area can be written Prot_none map area cannot be accessed
|
Parameters |
Flags affect the various features of the map area Map_fixed if the address that the parameter start refers to cannot successfully establish the mapping, discard the mapping and do not fix the address. This flag is generally discouraged. Map_shared writes data to a mapped region is copied back into the file, and other processes that map the file are allowed to be shared. Map_private writes to a mapped region will result in a copy of the mapping file, which means that any changes made to this area by the private copy on write will not be written back to the original file contents. map_anonymous establish an anonymous mapping. The parameter FD is ignored, the file is not involved, and the mapped area cannot be shared with other processes. Map_denywrite only allows write operations to the mapped region, and other operations that write directly to the file will be rejected. map_locked locks the mapped area, which means that the region will not be displaced (swap). You must specify map_shared or map_private when calling Mmap (). The parameter fd is the file descriptor returned by open (), representing the file to be mapped to memory. The offset of the parameter, which is usually set to 0, corresponds to the file map, and the offset must be an integer multiple of the paging size.
|
return value |
If the mapping succeeds, it returns the memory start address of the mapping area, otherwise map_failed (-1) is returned, and the reason for the error is stored in errno.
|
error code |
< Span style= "font-family: Song body; font-size:12px ">EBADF parameter fd is not a valid file descriptor Eacces access rights are incorrect. If the file must be readable in the case of map_private, use map_shared to have prot_write and the file to be writable. The EINVAL parameter start, length, or offset has an illegal one. The Eagain file is locked, or too much memory is locked. Enomem Insufficient memory. |
Example |
/* Use MMAP () to read the contents of the/etc/passwd file */ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <sys/mman.h> Main () { int FD; void *start; struct STAT sb; Fd=open ("/etc/passwd", o_rdonly); /* Open/etc/passwd*/ Fstat (FD,&SB); /* Get File Size */ Start=mmap (null,sb.st_size,prot_read,map_private,fd,0); if (start= = map_failed)/* Determine if the mapping succeeded */ Return printf ("%s", start); Munma (start,sb.st_size); /* de-map */ Closed (FD); }
|
Perform |
root:x: 0:root:/root:/bin/bash bin:x: 1:1: Bin:/bin: daemon:x: 2:2:d Aemon:/sbin adm:x: 3:4: adm:/var/adm: Lp:x: 4:7: LP:/var/spoo L/LPD: sync:x: 5:0: Sync:/sbin:bin/sync: shutdown:x: 6:0: Shutdown:/sbin:/sbin/shutdown Hal T:X: 7:0: Halt:/sbin:/sbin/halt mail:x: 8:12:mail:/var/spool/mail: news:x: 9:13:news:/var /spool/news: uucp:x: 10:14:UUCP:/VAR/SPOOL/UUCP: operator:x: 11:0: operator:/root: Games:x: 12 : 100:games:/usr/games: gopher:x: 13:30:gopher:/usr/lib/gopher-data: ftp:x: 14:50:ftp User:/hom E/FTP: nobody:x: 99:99:nobody:/: Xfs:x: 100:101:x Font Server:/etc/xll/fs:/bin/false Gdm:x: 42: :/HOME/GDM:/bin/bash kids:x: 500:500:/home/kids:/bin/bash |
|
|
Munmap (de-memory mapped) |
Related functions |
Mmap
|
Table header File |
#include <unistd.h> #include <sys/mman.h>
|
Defining functions |
int Munmap (void *start,size_t length);
|
Function description |
Munmap () is used to cancel the starting address of the mapped memory referred to by the parameter start, and the length of the parameter is the memory size to be canceled. The mapped memory is automatically dismissed when the process finishes or when other programs are executed with the exec correlation function, but the corresponding file descriptor is not unmapped when it is closed.
|
return value |
Returns 0 if the mapping succeeds, otherwise returns 1, the error reason is stored in errno error code EINVAL
|
Parameters |
Start or length is illegal.
|
Example |
Reference mmap ()
|