Use calloc function and understanding of memory free

  #include<stdlib.h> 

  void *calloc(size_t n, size_t size);

  free();

  Current understanding: n is the number of such size, the use of such similar has fread, fwrite function to memory inside the data cleared, free indeed the original buf cleared, the data after buf is random data, there is. after free issue is not set to NULL pointer

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	// calloc   1920
	char* buf = (char*)calloc(1920,sizeof(char));
	if (buf == NULL)
	{
		printf ( "calloc allocate memory failed \ n");
		return -1;
	}
	printf("buf is %d\n", buf[0]);
	printf("buf is %p\n", buf);
	buf[0] = 1;
	printf("buf is %d\n", buf[0]);
	free(buf);
	printf("buf is %p\n",buf);
	printf("buf is %d\n",buf[0]);
	buf = NULL;
	printf("buf is %p\n",buf);
	while (1)
	{
		//printf("buf is %p\n", buf);
	}
	return 0;
}

  

Guess you like

Origin www.cnblogs.com/nowroot/p/12155565.html