Reference link:
https://www.cnblogs.com/zhanjxcom/p/5766844.html
http://www.runoob.com/cprogramming/c-function-realloc.html
Application and release of dynamic memory must be paired. The number of malloc and free in the program must be the same, otherwise there must be errors (New/Delete)
The effect of
Malloc and New is similar, but if you want to add memory, you must use the Realloc function.
#include <iOSTREAM>
#include <cstdlib>
#include <cstring>
using namespace std;
int Main ()
{{
char *p;
p = (char *) malloc (10 *sizeof (char));
COUT << "p:" << SIZEOF (P) << Endl;
// After using the Malloc function, generally use Memset to talk about memory initialization
memSet (P, 0,10*sizeof (char));
strcpy (p, "COME on");
COUT << "p:" << P << Endl;
p = (char *) Realloc (p, 20 *sizeof (char));
COUT << "p:" << SIZEOF (P) << Endl;
strcat (p, ", baby!");
COUT << "p:" << P << Endl;
free (p);
p = new char [10];
strcpy (p, "COME on");
COUT << "p:" << P << Endl;
p = (char *) Realloc (p, 20 *sizeof (char));
COUT << "p:" << SIZEOF (P) << Endl;
strcat (p, ", baby!");
COUT << "p:" << P << Endl;
delete [] p;
Return 0;
}