Prototype: externvoid * malloc (unsigned int num _ bytes); Header file: malloc.h or alloc.h can be used in TC2.0 (note: the contents of alloc.h and malloc.h are exactly the same), while malloc.h or stdlib.h can be used in Visual C++6.0. Function: Allocate a memory block with the length of num_bytes. Return value: if the allocation is successful, return a pointer to the allocated memory; Otherwise, a null pointer is returned. When the memory is no longer used, the free () function should be used to release the memory block. Note: regarding the prototype of this function, malloc returned a char pointer in the old version, while the new ANSIC standard stipulates that this function returns a void pointer, so type conversion is needed. Name explanation: malloc's full name is memory allocation, and Chinese is dynamic memory allocation.
void * malloc(int size); Description: malloc applies to the system to allocate memory space with a specified size of bytes. The return type is void*. Void* indicates a pointer of undetermined type. C and C++ stipulate that void* type can be cast to any other type of pointer. As can be seen from the function declaration. There are at least two differences between malloc and new: new returns a pointer of a specified type and can automatically calculate the required size. For example: int * p;; P = new int// The return type is int* type (integer pointer) and the allocation size is sizeof (int); Or: int * parr parr = new int [100]; //The return type is int* type (integer pointer) and the allocation size is sizeof (int) *100; Malloc, on the other hand, must be calculated by us, and then forcibly converted into a pointer of actual type after returning. int * p; p =(int *)malloc(sizeof(int)); First, the malloc function returns void * type. For C++, if you write: p = malloc (sizeof (int)); Then the program failed to compile with the error: "void* cannot be assigned to an int * type variable". Therefore, it must be cast by (int *). For C, there is no such requirement, but in order to transplant C programs to C++ more conveniently, it is suggested to form the habit of mandatory conversion. Second, the parameter of the function is sizeof(int), which is used to represent the required size of an integer data. If you write: int * p = (int *) malloc (1); The code can also be compiled, but actually only 1 byte of memory space is allocated. If you put an integer in it, three bytes will make you homeless and live in a neighbor's house directly! The result is that the original data content in the background memory is rewritten. Malloc can also achieve the effect of new [], and apply for a continuous memory by specifying the memory size you need. For example, you want to allocate 100 space of type int: int * p = (int *) malloc (sizeof (int) *100); //Allocate memory space that can hold an integer of 100. Another difference that can't be seen directly is that malloc only allocates memory, but can't initialize the acquired memory, so the value of a new memory will be random. Except for the different methods of allocation and final release, the pointer is obtained through malloc or new, and other operations are the same. Make a special case to supplement char * ptrif ((ptr = (char *) malloc (0)) = null) puts ("get a null pointer"); Else puts ("got a valid pointer"); What you get at this point is a valid pointer. Assigning 0 to malloc can get a legal pointer.
Working mechanism of editing this paragraph function.
The essence of malloc function is that it has a so-called free linked list, which connects the available memory blocks into a long list. When the malloc function is called, it will find a large enough memory block along the connection table to meet the user's request. Then, divide the memory block into two parts (the size of one block is equal to the size requested by the user and the size of the other block is the remaining bytes). Next, pass the memory allocated to the user to the user and return the remaining memory (if any) to the connection table. When the free function is called, it connects the memory block released by the user to the free chain. Eventually, the idle chain will be cut into many small memory fragments. If the user applies for large memory fragments at this time, there may be no fragments on the free chain that meet the user's requirements. Therefore, the malloc function delayed the request, and began to rummage through the memory fragments on the idle chain, sort them, and merge adjacent small idle blocks into larger memory blocks. If the memory block that meets the requirements cannot be obtained, the malloc function will return an empty pointer, so it is necessary to judge the return value when calling malloc to dynamically apply for the memory block.
For example: 1: (running in Visual C++ 6.0)
# include & ltstdio.h & gt
# include & ltstdlib.h & gt
int main()
{ int i,j; int * array = NULLscanf("%d ",& ampI);
array =(int *)malloc(I * sizeof(int));
for(j = 0; J< me; j++)
scanf("%d ",& amparray[j]);
for(j = 0; J< me; j++)
printf("%d\n ",array[j]);
Free (array);
array = NULL
Returns 0;
}