malloc() function usage

The malloc function is a function for allocating memory space. The
header file stdlib.h must be included. The
function prototype void *malloc(unsigned int size)
is an unsigned integer number, which is the size of the allocated bytes.
If the space is allocated successfully, the return value Pointer to the allocated memory, otherwise it returns NULL. After
allocating the memory, if you don't need to use it anymore, you need to use the free() function to release the memory, otherwise it will cause a memory leak.

#include<cstdlib>
#include<cstdio>

int main()
{
	int *a;
	//申请内存 
	a=(int *)malloc(sizeof(int));
	*a=1;
	printf("%d",*a);
	//使用完之后,释放内存 
	free(a);
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_54621932/article/details/114142491