All programs must set aside enough memory to store the data they use. Some memory allocations are done automatically. More memory needs to be allocated at run time. The primary tool is the function malloc (), which takes a parameter: The number of bytes required in memory. Then malloc finds a suitable block of available memory in the appropriate size. Memory is anonymous, that is, malloc () allocates memory, but does not specify a name for it. However, it can return the address of the first byte of the memory. Therefore, you can assign that address to a pointer variable and use that pointer to access the block of memory.
Char place[] = "Dancing oxen Creek";
Because Char represents a byte, malloc has traditionally been defined as a pointer type that points to char.
A new type is used in ANSI C standard use: A pointer to void. This type is used as a "generic pointer". The function malloc () can be used to return array pointers, struct pointers, and so on, so it is generally necessary to assign the type of the return value to the appropriate type. In ANSI C, it is not a type conflict to assign a null pointer value to other types of pointers in order for the program to clearly address the pointer for type assignment. If malloc () does not find the space needed, he returns a null pointer.
Create an array using malloc. You can request a block of storage using malloc () while the program is running, and you need a pointer to hold the block in memory.
Double * PTD;PTD = (double *) malloc (* sizeof (double));
A space is requested for 30 values of type double, and the package ptd points to the location where the space resides.
The ptd here is a pointer to a double-type value, not a data block that points to 30 type values. The name of the array here is the address of its first element.
If you make ptd point to the first element of a memory, you can use it just as you would use an array name.
That is, you can use the expression ptd[0] to access the first element of a memory block, Ptd[1] access the second element, and so on. You can use the array name in the pointer symbol, or you can use the pointer in the array symbol.
There are 3 ways of creating an array
1. Declare an array, declare the array dimension with a constant expression, and then access the array element with the array name
2. Declare a variable-length array, declare the array dimension with a variable expression, and then use the array name to access the array element
3. Declare a pointer, call malloc (), and use the pointer to access the array elements.
In general, for each malloc call, a free () should be called. The parameter of the function free is the address returned by the previous malloc (), releasing the previously allocated memory. The duration of the allocated NEI-Cun will begin with the call to malloc () to allocate memory, and to call Free () to release memory for reuse. It is envisaged that malloc () free () manages a pool of memory. Each call to malloc () allocates memory to the program for use, and each call to free () returns the memory to the pool so that the memory is used again. The free () parameter should be a pointer to a block of memory allocated by malloc () and cannot be freed () to allocate memory by other means. There is a prototype of malloc () and free () in the header file stdlib.h.
By using malloc (), the program can determine how large an array is needed and create it at run time.
Allocating memory malloc and free ()