Function and dynamic allocation of memory

1.malloc () function dynamically allocated section of memory space ------

  • Function prototype: void * malloc (unsigned int size);
  • Function: the dynamic memory application memory size a length of contiguous memory bytes, the malloc () function returns a pointer to a memory space allocated to the start address (not enough storage space can be allocated
    the function return value null pointer type is NULL) value of the function pointer type, since the void type group, this is assigned to other types of pointer value of the pointer variable, it should be cast

Example
int * p = (int *) malloc (sizeof (int))
to apply a storage space of type int length, and converts the address of the memory space allocated to type int address, assigned to the defined pointer variable p

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int *pInt;//定义指针pInt并分配内存空间 
	pInt=(int *)malloc(sizeof(pInt));
	*pInt=100;//使用新空间保存数据 
	printf("%d\n",*pInt);
	free(pInt);
	printf("%d\n",*pInt);//保存数据的空间已被释放,数据不将存在,故输出一个不可预料的值 
	return 0;
 } 

2.calloc () function is continuously dynamically allocated memory space --------

  • Function prototype: void * calloc (unsigned int n, unsigned int size);

  • Function: the application memory size of length n bytes of storage space, and return to the starting address of the memory space
    is mainly used for application as a dynamic memory array, n is the number of elements, the element size is the memory length

Case

int * p = (int *) calloc (10, sizeof (int));
application type int length 10 of storage space and the storage space allocated to the address converted to type int address, which was assigned the first address defined the pointer variable p
thereafter can be p as an integer array of 10 elements using this array numerous group name can only be used to access the pointer variable p,

It may also be function malloc () to achieve
int * p = (int *) malloc (sizeof (int) * 10);

#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(char[26])
int main()
{
	int i;
	/*使用calloc动态分配一个长度为26字节的字符数组*/ 
	char *ch1=(char *)calloc(26,sizeof(char));
	/*使用malloc动态分配一个长度为26字节的字符数组*/ 
	char *ch2=(char *)malloc(LEN);
	for(i=0;i<26;i++)
	{
		ch1[i]=65+i;//小写字符数组 
		ch2[i]=97+i;//大写字符数组 
	 } 
	 printf("26个英文字母表:\n");
	 for(i=0;i<26;i++)
	 {
	 	printf("%c%c ",ch1[i],ch2[i]);
	 	if(i==12 || i==25)
	 	printf("\n");
	 } 
	 free(ch1);
	 free(ch2);
	 return 0;
 } 

3.realloc () ------ pointer to change the size of the space

  • Function prototype: void * realloc (void * ptr, size_t size);
  • Function: ptr pointer to change the size of the space, the size of the set size is arbitrary, the return value is a pointer to point to the new address

Case

Changing a float to integer space size
fDouble = (double *) malloc ( sizeof (double)); the floating-point spatial distribution
iInt = realloc (fDouble, sizeof ( int));

realloc () function to change the size of the address space pointing fDouble memory space after its size to an integer, and then change the return pointer assigned to iInt

#include<stdio.h>
#include<stdlib.h>
int main()
{
	short *s;
	double *f=(double *)malloc(sizeof(double));
	printf("指针f指向内存空间的起始地址:\n%d\n",f);//申请double变量所占内存空间 
	printf("指针f指向内存空间的大小:\n%d字节\n",sizeof(*f));//打印首地址
	s=(short *)realloc(f,sizeof(short));//重新分配内存
	printf("指针s指向内存空间的起始地址:\n%d\n",s);//打印首地址 
	printf("指针s指向内存空间的大小:\n%d字节\n",sizeof(*s));//打印空间大小 
	return 0;
}
//新分配的内存空间和原来的空间起始地址一样,但大小不同 

4.free () function to release memory space ------

  • Prototype: void free (void * p)
  • Function: the pointer variable p to point to release storage space, the system returned, Free () function does not return a value,
    p is the address of the program only after the last call malloc () or calloc () function returns
`int *p,*q=(int *)calloc(10,sizeof(int));
p=q;   q++;
free(p);     // 将p指向的,此前调用的calloc()函数申请的存储空间释放若改用free(q)则会提示错误,因为执行了q++,以后,q已改变,若改用free(q)则会提示错误,因为执行了q++,以后,q已改变
#include<stdio.h>
#include<stdlib.h>
int main()
{
	int *pInt;//定义指针pInt并分配内存空间 
	pInt=(int *)malloc(sizeof(pInt));
	*pInt=100;//使用新空间保存数据 
	printf("%d\n",*pInt);
	free(pInt);
	printf("%d\n",*pInt);//保存数据的空间已被释放,数据不将存在,故输出一个不可预料的值 
	return 0;
 } 
Published 26 original articles · won praise 6 · views 1467

Guess you like

Origin blog.csdn.net/qiao_xi/article/details/89388136