First, prototype: extern void *malloc (unsigned int num_bytes);
Header file: #include <malloc.h> or #include <alloc.h> (note: alloc.h is exactly the same as Malloc.h's content. )
Function: Allocate a block of memory with a length of num_bytes bytes
Note: Returns a pointer to the allocated memory if the assignment succeeds, or null if the null pointer is returned.
When memory is no longer in use, you should use the free () function to release the memory block.
Example:
#include <stdio.h>
#include <malloc.h>
int main ()
{
char *p;
p= (char *) malloc (MB);
if (p)
printf ("Memory allocated at:%x/n", p);
else
printf ("Not Enough memory!/n");
Free (p);
return 0;
}
second, function declaration (function prototype):
void *malloc (int size);
Description: malloc allocates the memory space for the specified size byte to the system request. The return type is the void* type. Void* represents a pointer to an indeterminate type. C,c++ stipulates that the void* type can be cast to any other type of pointer. This can be found on MSDN, with the following details:
malloc returns a void pointer to the allocated spaces, or NULL if there is insufficient memory. To return a pointer to a type other than void and use a type cast on the return value. The storage space pointed "to" by the "return value is guaranteed to" suitably aligned for storage of any type of object. If size is 0, malloc allocates a zero-length item in the heap and returns a valid pointer to that item. Always Check the return from malloc, even if the amount of memory requested is small.
Iii. the difference between malloc and new
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 required 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 calculated by us for the number of bytes, and will be forcibly converted to the actual type of pointer after the return.
int* p;
p = (int *) malloc (sizeof (int));
1th, the malloc function returns the void * type if you write: p = malloc (sizeof (int)); The program cannot compile and error: "Cannot assign void* to int * type variable". So you must pass the cast by (int *).
2nd, the function's argument is sizeof (int), which indicates the size required for an integer data. If you write:
int* p = (int *) malloc (1);
Code can be compiled, but in fact allocating only 1 bytes of memory space, when you deposit an integer, there will be 3 bytes homeless, and directly "live in the Neighborhood"! The result is that the contents of the original data in the back of the memory are all emptied.
Malloc can also achieve the effect of new [], the application of a continuous memory, the way is to specify the size of the memory you need.
For example, to allocate 100 int types of space:
int* p = (int *) malloc (sizeof (int) * 100); Allocate the memory space that can be put down 100 integers.
Another point that cannot be seen directly is that malloc allocates memory and cannot initialize the resulting memory, so the value of the resulting new memory will be random.
In addition to allocating and final release methods, the pointer is obtained through malloc or new, consistent in other operations.
four, dynamic application array
Request a one-dimensional array
the array name of a one-dimensional array can be considered as the first address of an array's starting element, so I define a pointer to an int *arr, allocating n size int spaces, and writing as follows:
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int n, *arr;
while (scanf ("%d", &n)!= EOF) {
arr = (int *) malloc (sizeof (int) * n);
}
return 0;
}
request two-dimensional arrays
the array name of a two-dimensional array is the first address of all its one-dimensional arrays, because the array name of the two-dimensional array is the pointer to the pointer because I define a two-dimensional array of row column columns, as follows:
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int i, row, column, **arr;
while (scanf ("%d%d", &row, &column)!= EOF) {
arr = (int * * *) malloc (sizeof (int *) * row);//Assign the first address of all rows for
(i = 0; i < row; i + +) {///assign each column by row
arr[i] = (int *) malloc (sizeof (int) * column);
}
Free (arr);
}
return 0;
}
Summarize:
The malloc () function is actually looking for a space of the specified size in memory, the first address of the space is then given to a pointer variable, where the pointer variable can be a separate pointer or the first address of an array, depending on the exact contents of the parameter size in the malloc () function. The memory space we allocate here is logically contiguous, and can be malloc or discontinuous in physics. For our programmers, we are focused on logical continuity, because the operating system will help us arrange the memory allocation, so we can use it as a continuous.