Simulation implementation of memory management _c language in C language

Source: Internet
Author: User

This simulates memory management in C, when we want to create or use an object, then the object will call the Retain method, Count +1, when we want to release the object, we will call free, note here to judge the counting memory, if it is 0, then destroy.

#import <Foundation/Foundation.h> 
int cnt = 0; 
 
void Fun (Charchar * p) 
{ 
  printf ("%c\n", P[0]); 
 
Charchar * RETAIN1 (Charchar * p) 
{ 
  //retain (p); 
  CNT + +; 
  return p; 
} 
 
void Dealloc1 (Charchar *p) 
{free 
  (p); 
} 
 
void Release (Charchar * p) 
{ 
  cnt--; 
  if (cnt = = 0) { 
    //free (p); 
    Dealloc1 (P); 
  } 
 
int main (int argc, const CHARCHAR * argv[]) 
{ 
 
  @autoreleasepool { 
    Charchar * p = (Charchar *) malloc (1000); 
  p[0] = ' a '; 
    P[1] = ' B '; 
    Charchar * q = p; 
    Early release of 
    //free (p); 
    printf ("%c\n", P[0]); 
    Free (q); 
    Free (q); 
     
  } 
  return 0; 
}

The above is the simulation C language memory management of the implementation code, I hope to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.