First, Memory application
1. It is recommended to use CALLOC for memory and not to use malloc as much as possible.
Calloc automatically initializes the memory space after the memory is dynamically allocated to zero, and malloc is not initialized, and the data inside is random garbage data.
2. The requested memory size must be greater than 0.
(1) The behavior of requesting memory using 0-byte length is undefined, and an unpredictable error is raised when referencing the memory request function return address, and it is necessary to judge the possibility of requesting 0-length memory to avoid this situation.
(2) Use a negative length to apply for memory, negative numbers will be treated as a large unsigned integer, resulting in the application memory is too large to fail.
3. After applying for memory check whether the request succeeds, check whether the return pointer is null, that is, 0.
Second, Memory release
1. The requested memory must be released, and can only be released once
2. Disable the release or return non-dynamic memory in the function (memory in the stack, temporary variables in the function, etc.)
3. When the pointer is released, you must point the pointer to a null pointer, or the wild pointer will appear.
Third, add a C implementation of the storage of binary tree elements of the dynamic stack
#include <stdio.h>#include<string.h>#include<malloc.h>#defineLh_malloc (Pmemory,type,size)if(Size >0) {pmemory= (type*)calloc(size/sizeof(Type),sizeof(Type)); } Else{pmemory=NULL; }#defineLh_free (P) if (NULL! = p) {free (p);p =null;}#defineLh_memory_mov (Dst,dstsize,src,srcsize,type) \Lh_malloc (dst,type,dstsize) memcpy (dst,src,srcsize); Lh_free (SRC) typedefstructtagtreenode{intv; structtagtreenode*Pleft; structtagtreenode*Pright;} TreeNode;
typedef struct TAGLINEARSTACK
{
treenode* ptrees;
int* ptags;
int maxsize;
int index;
}linearstack;
/*get a stack pointer*/Linearstack*Getalinearstack () {Linearstack*Pstack; Lh_malloc (Pstack,linearstack,sizeof(Linearstack)); returnPstack;}/*release stack, paired with Getalinearstack*/voidFreelinearstack (linearstack*pstack) {Lh_free (Pstack-ptags); Lh_free (Pstack-ptrees); Lh_free (Pstack);}/*into the stack*/voidPush (linearstack*pstack,treenode node) { if(Pstack->ptrees = =0&& Pstack->ptags = =0) {Lh_malloc (Pstack->ptrees,treenode,sizeof(TreeNode) *5); Lh_malloc (Pstack->ptags,int,sizeof(int)*5); Pstack->maxsize=5; } if(Pstack->index < pstack->maxsize) {Pstack->ptrees[pstack->index++]=node; } Else{TreeNode*tmptrees; int*Tmptags; Lh_memory_mov (Tmptrees,sizeof(TreeNode) *pstack->maxsize*2, Pstack-Ptrees,sizeof(TreeNode) *pstack->maxsize, TreeNode); Lh_memory_mov (Tmptags,sizeof(int) *pstack->maxsize*2, Pstack-Ptags,sizeof(int) *pstack->MaxSize,int); Pstack->ptrees=tmptrees; Pstack->ptags=Tmptags; Pstack->maxsize=pstack->maxsize*2; Pstack->ptrees[pstack->index++]=node; }}/*pop-up stack*/TreeNode Pop (linearstack*pstack) { if(Pstack->index >0) { returnPstack->ptrees[--pstack->index]; }}voidMain () {Linearstack* pstack=Getalinearstack (); if(NULL = =pstack) Retrun; for(intI=0;i<60000; i++) {A.V=i; Push (PSTACK,A); } for(intj=0;j<60000; j + +) {TreeNode node=pop (pstack); printf ("%d:%d \ n", J,NODE.V); } freelinearstack (Pstack);}
Memory management specification in C language