C language free function and wild pointer introduction

  • 2020-04-02 01:08:05
  • OfStack

【 FROM MSDN && encyclopedia 】
Prototype: void free(void * PTR);
# include < Stdlib. H > Or # include < The malloc. H >
Deallocate space in the memory
Free the storage space pointed to by PTR. The freed space is usually fed into the pool of available storage and can be redistributed later in calls to malloc, realloc, and realloc functions.
Note: using the free function twice in a row is bound to cause errors. The degree of malloc has to be equal to the degree of free.
A block of memory previously allocated using A call to malloc, calloc or realloc is deallocated, making it available again for further allocations.
If PTR does not point to a block of memory allocated with the above functions provides, the behaviors is undefined.
If PTR is a null pointer, the function does nothing
Notice that this function does not change the value of PTR itself, hence it still points to the same (now invalid) location
The DEMO:


//#define  FIRST_DEMO
#define  SECOND_DEMO
#ifdef FIRST_DEMO
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(void)
{
 int *buffer1,*buffer2,*buffer3;
 buffer1=(int *)malloc(100*sizeof(int));
 buffer2=(int *)calloc(100,sizeof(int));
 buffer3=(int *)realloc(buffer2,500*sizeof(int));
 free(buffer1);
 free(buffer3);
 getch();
 return 0;
}
#elif defined SECOND_DEMO
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(void)
{
 char *str;
 
 str=(char *)malloc(10);
 if (str==NULL)
 {
  perror("malloc"); 
  abort();
 }
 else
 {
  
  strcpy(str,"hello");
  
  printf("String is %sn",str);
  
  free(str);
 }
 getch();
 return 0;
}
#endif

DEMO: perror
Perror () is used to output the cause of the error in the previous function to the standard device (stderr). The string referred to by parameter s is printed first, followed by the error cause string. The reason for this error depends on the value of the global variable errno to determine the string to output.

#include <stdio.h>
#include <stdlib.h>   //Perror is included in this file
#include <conio.h>
int main(void)
{
    FILE *fp;
 fp=fopen("abc","r+");
 if (NULL == fp)
 {
  perror("abc");
 }
 getch();
 return 0;
}

The output:
ABC: No such file or directory
The DEMO:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
 char *ptr;
 ptr=(char *)malloc(100);
 strcpy(ptr,"Hello");
 free(ptr);    //<Span style="font-family: arial, sans-serif; The font - size: 13.63636302947998 px; The line - height: 24 px; The text text-indent: 30 px;"> & have spent The memory referred to by PTR is freed, but the address referred to by PTR remains the same, and the original memory becomes "garbage" memory. / span>
#if 1
 if (ptr!=NULL)    /*<span style="font-family: arial,  Song typeface , sans-serif; font-size: 13.63636302947998px; line-height: 24px; text-indent: 30px;">  It's not error-proof </span>*/
 {
  strcpy(ptr," world");
  printf("%sn",ptr);
 }
#endif
 getch();
 return 0;
}

Free (STR) after the pointer still points to the original heap address, that is, you can still continue to use this memory, but it is dangerous, because the operating system already thinks this memory is usable, it will not consider it to allocate it to other programs, so the next time you use it may have been changed by other programs, this case is called "Wild pointer", so it's best to set it free () later
STR = NULL;
That is, the program has abandoned the use of him.
What is" Wild pointer ", here to add.
Wild Pointers are Pointers that programmers or operators cannot control. Wild Pointers are not NULL Pointers, but Pointers to garbage.

The main reasons for the "wild pointer" are as follows
1. Pointer variable is not initialized,
Any pointer variable does not automatically become a NULL pointer when it is first created, its default value is random, and it will point randomly. At the time of initialization, it either points to a valid pointer or to NULL.

2. Pointer variable is not set to NULL after free or delete. They just free up the memory that the pointer points to, but they don't kill the pointer itself. If (p! = NULL) for error-proof handling. Unfortunately, the if statement is not error-proof at this point, because even if p is not a NULL pointer, it does not point to a valid block of memory. This is the case in the above DEMO.

3. Pointer operation goes beyond the scope of variables. Notice the lifecycle.

The following is an excerpt from the forum in the image of metaphor, deepen understanding.
The memory management module of the CRT is a steward.    
Your program (" you "for short) is a guest.    
The housekeeper has a pair of buckets to hold water in.    
Malloc means "housekeeper, I want XX buckets".    
The housekeeper first checks to see if there are enough buckets for you, and if not, tells you no. If it is, register that the buckets have been used and tell you to "go ahead".    
Free means: "housekeeper, I'm done with it. I'll pay you back!" .    
As to whether or not you should empty the water before giving it to the housekeeper, that is your own business. -- is it zero?    
Nor will the butler empty out the buckets you return (he has so many that it would kill him to return them all). I'll take care of the rest when I use it anyway.    
Free then zeros the pointer just to remind myself that these buckets are not mine anymore, don't put water in them anymore, ^_^    
If free still USES that pointer after that, it's possible that the housekeeper has given the buckets to someone else to fill with drinks, and you've urinated in them. A good housekeeper may feel strongly about your behavior and kill you -- for the best, you know you're wrong. Some bad stewards may be too busy, sometimes they catch you doing bad things and punish you, sometimes they don't know where to go -- it's your nightmare, you don't know when and how to deal with it and die. Either way, there's a good chance that someone will drink urine in this situation -- whether it's your boss or your client.^_^.    
So, of course, a good citizen is to return to the steward of things do not occupy,.^_^.


Related articles: