malloc function Usage
function declaration (function prototype):
void *malloc (int size);
Description: malloc allocated memory space to the system to allocate a specified size byte. The return type is the void* type. Void* represents a pointer to an indeterminate type. c,c++ Specifies that the void* type can be cast to any other type of pointer.
As you can see from the function declaration. malloc and new are at least two different: new returns a pointer of the specified type, and can automatically calculate the desired size. Like what:
int *p;
p = new int; The return type is the int* type (integer pointer), and the allocation size is sizeof (int);
Or:
Int* Parr;
Parr = new int [100]; The return type is the int* type (integer pointer), and the allocation size is sizeof (int) * 100;
malloc, however, must be computed by us to calculate the number of bytes and forcibly converted to a pointer of the actual type after the return.
int* p;
p = (int *) malloc (sizeof (int));
First, the malloc function returns the void * type if you write: p = malloc (sizeof (int)); The program cannot compile, error: "Cannot assign void* to int * type variable". Therefore, the cast must be passed (int *).
Second, the argument to the function is sizeof (int), which indicates the size required for an integral type of data. If you write:
int* p = (int *) malloc (1);
Code can also be compiled, but in fact only allocates 1 bytes of memory space, when you put an integer inside, there will be 3 bytes homeless, and directly "live in the neighbor's house"! The result is that the contents of the original data in the back memory are all emptied.
Malloc can also achieve the effect of new [], requesting a contiguous amount of memory by specifying the size of the memory you need.
For example, to allocate 100 spaces of type int:
int* p = (int *) malloc (sizeof (int) * 100); Allocates a memory space that can be placed down to 100 integers.
Another thing that cannot be directly seen is that malloc allocates memory and does not initialize the resulting memory, so the value will be random in a new piece of memory.
In addition to the method of allocation and final release, pointers are obtained through malloc or new and are consistent on other operations.
=============================================
Both the function malloc () and calloc () can be used to dynamically allocate memory space, but the two are slightly different.
The malloc () function has a parameter that is the size of the memory space to allocate:
void *malloc (size_t size);
The Calloc () function has two parameters, each of which is the number of elements and the size of each element, and the product of these two parameters is the size of the memory space to allocate.
void *calloc (size_t numelements,size_t sizeofelement);
If the call succeeds, the function malloc () and function calloc () will return the first address of the allocated memory space.
The main difference between the function malloc () and the function calloc () is that the former cannot initialize the allocated memory space, while the latter can. If the memory space allocated by the malloc () function has not been used, then each of the bits may be 0, whereas if this part of the memory has been allocated, there may be a variety of data left. That is, when a program that uses the malloc () function starts (the memory space has not been reassigned), it can work, but after a while (the memory space has been reassigned) there may be a problem.
The function calloc () initializes each bit of the allocated memory space to zero, that is, if you are allocating memory for an element of a character type or integer type, then those elements will be guaranteed to be initialized to 0, and if you are allocating memory for an element of pointer type, then these elements will usually be initialized to null pointers If you allocate memory for real data, these elements are initialized to 0 of the floating-point type.
The syntax for malloc is: pointer name = (data type *) malloc (length), (data type *) represents the pointer.
Example:
Malloc.c
#include
#include
Main ()
{
Char *p;
CLRSCR (); Clear Screen
p= (char *) malloc (100);
if (p)
printf ("Memory allocated at:%x", p);
Else
printf ("Not Enough memory!\n");
Free (p);
GetChar ();
return 0;
}
=============================================
working mechanism of malloc () function
The real body of the malloc function now, it has a so-called idle list that connects the available memory blocks to a long listing. When you call the malloc function, it looks for a block of memory along the join table that is large enough to satisfy the user's request. The memory block is then split in two (the size of the chunk is equal to the size of the user request, and the other is the remaining bytes). Next, pass the memory allocated to the user to the user and return the rest of the block (if any) to the connection table. When the free function is called, it connects the memory blocks freed by the user to the idle chain. In the end, the idle chain will be cut into a lot of small memory fragments, if the user requests a large memory fragment, then the idle chain may not be able to meet the user requirements of the fragment. The malloc function then requests a delay and begins to rummage through the memory fragments on the idle chain, collating them, and merging the adjacent small free blocks into larger chunks of memory
malloc function Usage