#Linux中的GCC编程# 动态内存申请

C

201707XX

1、关于动态内存申请的几个函数

我们在linux终端可以通过man命令查询相关函数的解释
man 3 malloc

  1. 首先需要包含的头文件为:#include <stdlib.h>
  2. 相关的函数声明如下
       void *malloc(size_t size);
       void free(void *ptr);
       void *calloc(size_t nmemb, size_t size);
       void *realloc(void *ptr, size_t size);
  • void *malloc(size_t size);官方解释
The malloc() function allocates size bytes and returns a pointer to the allocated memory.  
The memory is not initialized.  
If size is 0, then malloc() returns either NULL, or a unique pointer value  that can later be successfully passed to free().
翻译:
malloc()函数分配大小字节,并返回指向所分配内存的指针。
 内存没有初始化。
 如果大小为0,则malloc()返回NULL或唯一指针值,该值稍后可以成功传递给free()。
  • void *calloc(size_t nmemb, size_t size);官方解释
The  calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory.  
The memory is set to zero.  
If nmemb or size is 0, then  calloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().
翻译:
calloc()函数为每个大小字节的nmemb元素数组分配内存,并返回指向所分配内存的指针。
内存设置为零。
如果nmemb或size为0,则calloc()返回NULL或唯一指针值,该值稍后可以成功传递给free()。
  • void *realloc(void *ptr, size_t size); 官方解释
The  realloc()  function  changes  the size of the memory block pointed to by ptr to size bytes.  
The contents will be unchanged in the range from the start of the region up to the minimum of the old and new  sizes.   
If  the new size is larger than the old size, the added memory will not be initialized.
If ptr is NULL, then the call is equivalent to malloc(size), for all values of size; 
if size is equal to  zero, and ptr is not NULL, then the call is equivalent to free(ptr).  
Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc().
If the area pointed to was moved, a free(ptr) is done.
翻译:
realloc()函数将ptr所指向的内存块的大小更改为新的大小字节。
从区域开始到新旧尺寸的最小值,内容将保持不变。
如果新大小大于旧大小,则不会初始化添加的内存。
如果ptr是NULL,那么对于(第二参数size)所有大小值,调用等价于malloc(size);
如果size等于零,并且ptr不是NULL,则调用等价于free(ptr)。
除非ptr是NULL,否则必须有已经返回了的指针ptr(先通过对malloc()、calloc()或realloc()的调用得到ptr)。
如果指向的区域被移动,则执行free(ptr)。
  • void free(void *ptr);官方解释
The free() function frees the memory space pointed to by ptr, which must have been returned by a pre‐vious call to malloc(), calloc(), or realloc().  
Otherwise, or if free(ptr) has already  been  called before, undefined behavior occurs.  
If ptr is NULL, no operation is performed.
翻译:
free()函数释放ptr所指向的内存空间,该内存空间必须是已经通过对malloc()、calloc()或realloc()的预先调用返回指针的。
否则,或者如果以前已经调用free(ptr),则会发生未定义的行为。
如果ptr为空,则不执行任何操作。

2、具体事例

1)malloc 申请空间,赋值,再释放

#include<stdio.h>
#include<malloc.h>
void out(int *p,int n)
{
 	int i;
 	for(i=0;i<n;i++)
 	{
  		printf("%d  ",*(p+i));
 	}
 	printf("\n---------------------\n");
}
int main()
{
 	printf("请输入需要申请的字节数\n");
 	int n=0;
 	scanf("%d",&n);
 	int *p=(int*)malloc(n*sizeof(int));//向系统申请 n*4个字节的内存块
 	if(p!=NULL)
	 {
  		out(p,n); //打印未赋值的动态数组的成员
  		int i=0;
  		for(i=0;i<n;i++)
  		{
   			*(p+i)=i*i;
 		}
  		out(p,n);  //打印已赋值的动态数组的成员
  		free(p); //释放资源
 	}else{
  		printf("malloc error\n");
 	}
 	return 0;
}

运行结果:可以看到申请了6字节的空间,该空间初始时,每个成员都是0(malloc不初始化,只能说申请的空间本身就是0),赋值之后,变为数组下标的平方数。

  • 注:malloc之后正常都需要初始化一下(memset)
kshine@kshine-virtual-machine:~/桌面/GCC$ gcc test_malloc.c  -o main -Wall
kshine@kshine-virtual-machine:~/桌面/GCC$ ./main
请输入需要申请的字节数
6
0  0  0  0  0  0  
---------------------
0  1  4  9  16  25  
---------------------

2)calloc

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>   //calloc  参数 n 和 size  ,自带初始化,需要释放
void out(int *p,int n)
{
 	int i;
 	for(i=0;i<n;i++)
 	{
  		printf("%d  ",*(p+i));
	 }
 	printf("\n---------------------\n");
}
int main()
{
 	printf("请输入需要申请的字节数\n");
 	int n=0;
 	scanf("%d",&n);
 	/*向系统申请 n*4个字节的内存块  */
	// int *p=(int*)malloc(n*sizeof(int));
 	int *p=(int*)calloc(n,sizeof(int));
 	if(p!=NULL)
 	{
  		out(p,n);
  		int i=0;
  		for(i=0;i<n;i++)
  		{
   			*(p+i)=i*i;
  		}
  		out(p,n);
  		free(p);/*释放资源*/
 }else{
  	printf("calloc error\n");
 }
 	return 0;
}

运行结果

kshine@kshine-virtual-machine:~/桌面/GCC$ gcc test_calloc.c  -o main -Wall
kshine@kshine-virtual-machine:~/桌面/GCC$ ./main
请输入需要申请的字节数
7
0  0  0  0  0  0  0  
---------------------
0  1  4  9  16  25  36  
---------------------

3)realloc

#include<stdio.h>
//#include<malloc.h>
#include<stdlib.h>   //calloc  参数 n 和 size  ,自带初始化,需要释放
void out(int *p,int n)
{
 	int i;
 	for(i=0;i<n;i++)
 	{
  		printf("%d  ",*(p+i));
 	}
 	printf("\n---------------------\n");
}
int main()
{
 	int n=5;
	// int *p=(int*)malloc(n*sizeof(int));
	 int *p=(int*)calloc(n,sizeof(int));
 	if(p!=NULL)
 	{
  		out(p,n);
  		int i=0;
  		for(i=0;i<n;i++)
  		{
   			*(p+i)=i*i;
  		}
  		out(p,n);
 		n=7;
		 p=(int*)realloc(p,n*sizeof(int));
 		if(p==NULL)exit(1);
 		*(p+5)=5;
 		*(p+6)=6;
 		out(p,n);
 		n=3;
 		p=(int*)realloc(p,n*sizeof(int));
 		if(p==NULL)exit(1);
 		out(p,n);
 		free(p);/*释放资源*/
 	}else{
  	printf("calloc error\n");
 }
 return 0;
}

运行结果:

kshine@kshine-virtual-machine:~/桌面/GCC$ gcc test_realloc.c  -o main -Wall
kshine@kshine-virtual-machine:~/桌面/GCC$ ./main
0  0  0  0  0  
---------------------
0  1  4  9  16  
---------------------
0  1  4  9  16  5  6  
---------------------
0  1  4  
---------------------

猜你喜欢

转载自blog.csdn.net/Kshine2017/article/details/85322700