malloc () and calloc ()
Process a request for dynamic memory is not considered urgent. For example, when a process executable file is loaded, the process is not necessarily immediate access to all of the code. Similarly, when the process calls malloc () request dynamic memory, does not mean that the process will soon have access to all the memory available. Therefore, in general, always try to postpone the kernel to user mode processes dynamically allocated memory.
The kernel succeeds in deferring the allocation of dynamic memory to processes by using a new kind of resource When a User Mode process asks for dynamic memory, it does not get additional page frames;. Instead, it gets the right to use a new range of linear addresses, which become part of its address space. This interval is called a "memory region."
kernel uses a resource for the successful implementation of dynamic memory allocation to postpone the process. When a user mode process request dynamic memory, and did not get the requested page frame, but only to obtain the right to use a new linear address area, and this linear address range to become part of the process address space. This range is called the linear region (Memory Region).
At The Page Fault Exception Handler in deferring at The Allocation of page frames to Processes. Missing page exception handler eventually get the current page frames needed for the process. Excerpt from "in-depth understanding of linux kernel (second Version) "P269 - process address space of a chapter
brk () system call is the most common, by which a user process to the kernel space applications (memory region and perhaps page frames). By malloc () is a Class C language library functions used indirectly brk (). If malloc () thought of as retail, brk () is wholesale. Library function malloc () is a user process to maintain a small warehouse, when the process requires more memory space is to be a small warehouse, on the adoption of brk () to the kernel when small wholesale warehouse inventory shortage. alloc () is used Indirect how brk () is? "Scenario analysis (on)" P160
malloc ()
--------------------------------------- ----
the malloc () function is used to allocate memory: the total of the number of bytes passed as a parameter to the function, the return value is a pointer to memory newly assigned, and if the memory allocation is not good, then the value returned is NULL .
malloc () the use of technology:
some_type * pointer;
pointer = malloc (sizeof * COUNT (* pointer));
Note:
(1) This method ensures malloc () will allocate the correct amount of memory, regardless of life pointer. If the type of pointer later changed, sizeof operator automatically ensure that the number of bytes to be allocated is still correct.
(2) malloc () returns the memory is "no" initialization. This memory may contain any random junk, you can use right away, or at least valid data to initialize this memory with zeroes. 0 Initialization use, may be
void * memset (void * s, int c, size_t n);
(3) malloc () eventually acquired by missing page abnormal physical memory of the original data, in most cases is 0 (but is not guaranteed 0)
calloc ()
------------ -------------
calloc () malloc function is a simple wrapper. Its main advantage is that the dynamically allocated memory is cleared.
void * calloc (size_t nmemb, size_t size);
with experienced programmers prefer to use calloc (), because the content of newly allocated memory so there would be no problem, call the calloc () will certainly be cleared, and to avoid calling memset ().
malloc () practice
----------------------------------------- ------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *a;
a = (char *)malloc(10 * sizeof(*a)); //(1)
memset(a, 'a', 10); //(2)
printf("a = %s ", a);
free(a); //(3)
a = NULL; //(5)
B * char;
B = (char *) the malloc (10 * the sizeof (* B)); // (. 4)
Memset (B, 'B', 10);
the printf ( "% S = A", A);
the printf ( "% S = B", B);
Free (B);
}
Note:
------------------------------- ----------
(1)
user address space (virtual memory)
| |
| -------- |
| |
| | 10bytes
| |
A -------> | | 0x804a008
| -------- |
| |
a = malloc (); after performing
(2)
true physical memory on this page by the lack of an exception to get
| ------------ | - ----------- | -------------- |
| 0000100000 | 0001001010 | 000000001000 |
|------------|------------|--------------|
| | |
| - |---------| | |---------| | |---------|
| - | | | | | | | |
| - | | | | | | |---------|
| - | | | | | | | a |
| - | | | | | | | ... |10bytes
| - | | | | | | | ... |
+->| |--+ +-->| |--+ | | a |
| | - | | | - | + ---> | - ------- |
| | - | | | - | | |
| --------- | - + ----> | --------- | - + -----> | --------- |
page directory table page table data stored valid physical page
of physical memory
memset (a, 'a', 10); post
(3)
performs Free (a) after a period of user space remains directed at 0x804a008, but the space is no longer belongs to a pointer management, the corresponding physical memory is also cleared up and recovered. If the pointer is released immediately after a visit to which it refers the use of a space again, missing page exception process space allocation for the new physical memory again (before the piece of physical space may still be released, but has been cleared up).
If a new dynamic memory requests b = malloc (), a pointer to the released process space may be reassigned. Pointer a then still can access (read or write) the process space corresponding to the physical space (the physical space is access to the new pointer exception for the new pointer allocated by the missing pages, probably as a pointer p allocated physical memory) (no error), but such operations are illegal because the space could have been the new owner (the new guidelines) under management.
(4)
User address space
| |
| -------- |
| |
| | 10bytes
| |
B -------> | | 0x804a008
(A) | -------- |
| |
B = malloc (); after performing
this case both a and b point to the same process space (space corresponding to the process is the same physical space), the operation of a or b will be the same in both operating space (physical space), which is very dangerous because this is really the owner of the space is b, but also has the ability to modify a space b referred to in (Wow! the former owner had to sneak into the house, the new owner of the property may be dangerous)
( 5)
so that upon release process space pointer, once the pointer should be set to a null pointer. The pointer will be no chance to visit before the process space!
malloc()和calloc()都是用于分配内存的函数。
函数malloc()和calloc()都可以用来动态分配内存空间,但两者稍有区别。
malloc()函数有一个参数,即要分配的内存空间的大小:
void*malloc(size_tsize);
calloc()函数有两个参数,分别为元素的数目和每个元素的大小,这两个参数的乘积就是要分配的内存空间的大小。
void*calloc(size_tnumElements,size_tsizeOfElement);
如果调用成功,函数malloc()和函数calloc()都将返回所分配的内存空间的首地址。
函数malloc()和函数calloc() 的主要区别是前者不能初始化所分配的内存空间,而后者能。如果由malloc()函数分配的内存空间原来没有被使用过,则其中的每一位可能都是0;反之, 如果这部分内存曾经被分配过,则其中可能遗留有各种各样的数据。也就是说,使用malloc()函数的程序开始时(内存空间还没有被重新分配)能正常进行,但经过一段时间(内存空间还已经被重新分配)可能会出现问题。
函数calloc() 会将所分配的内存空间中的每一位都初始化为零,也就是说,如果你是为字符类型或整数类型的元素分配内存,那麽这些元素将保证会被初始化为0;如果你是为指 针类型的元素分配内存,那麽这些元素通常会被初始化为空指针;如果你为实型数据分配内存,则这些元素会被初始化为浮点型的零。
memset
memset 函数详细说明
#include <stdio.h>
#include <string.h>
int main()
{
char s[]="Golden Global View";
memset(s,'G',strlen(s));//貌似这里有点问题//
printf("%s",s);
return 0;
}
Description: Returns a pointer to the buffer pointer.
Reproduced in: https: //my.oschina.net/dake/blog/196646