High quality C code. release memory

Source: Internet
Author: User

Reprinted without permission
Copyright to Mr. Gao lei
Contact info:
Email: gaolei57521@gamil.com
Qq: 38929568

As I have just compiled C Programs, improper articles will inevitably occur. I hope you will correct them and I will modify them as soon as possible!

Recently, MTK game development is based on the C language. Therefore, I want to study the development of the C language and find some articles from forums or experts, which gives me great inspiration! Based on the previous Java experience, I would like to summarize how experts write programs...

My article will not talk about a C language learning process, nor cover all aspects of the C language. I have only learned the C language, or have a certain programming Foundation, when writing code, you are always unconfident about how others write it!

I hope that by reading this article, junior programmers can quickly grow into senior programmers!

1. How to release memory

Very simple free (); OK!

In C language, malloc, realloc, calloc, strdup and so on are all dynamically allocated memory from the heap and will not be automatically released. Here, you need to record the allocated address for future release. If not released, memory leakage may occur. The memory applied for in the heap memory needs to be managed by the programmer. The system does not take the initiative to reclaim the memory. It can be recycled through the free () function. Generally, Commercial Code has a lot of pointers, or pointer pointers. The struct contains pointer-type members. How can we manage these pointers to avoid errors?

For example, Role * pRole = (Role *) malloc (sizeof (Role ));

Release pointer

Free (pRole );

PRole = NULL;

After the pointer is released, the pointer is directed back to 0. If you think it is too troublesome to write two lines of code each time, you can define a macro.
 

/** Release pointer data and directly use the memory pointer variable */# define FREE (X) \ do {\ if (X )! = NULL) \{\ free (void *) (X); \ (X) = NULL ;\}\} while (0)

 

How can I write a function to release a row and achieve the same purpose?

 

View plaincopy to clipboardprint?
  1. // Release pointer data
  2. VoidFreePointer (Void* P)
  3. {
  4. If(P! = NULL)
  5. {
  6. Free (p );
  7. P = NULL;
  8. }
  9. }

// Release the pointer type data void freePointer (void * p) {if (p! = NULL) {free (p); p = NULL ;}}

 

This write won't work, because the pointer p copies the value of the real parameter through the copy method,

Free (p); no problem. You can release the memory referred to by pointer p, but the pointer cannot be pointed to NULL because the address of the form parameter and the address of the real parameter are different!

 

View plaincopy to clipboardprint?
  1. // Release pointer data
  2. VoidFreePointer (Void** P)
  3. {
  4. If(* P! = NULL)
  5. {
  6. Free (* p );
  7. * P = NULL;
  8. }
  9. }

// Release the pointer type data void freePointer (void ** p) {if (* p! = NULL) {free (* p); * p = NULL ;}}

 

You can modify it in this way, but the actual parameter must be a pointer address.

Use this method when using it.

FreePointer (void *) (& pRole ));

In this way, it is still uncomfortable to use it. Forced type conversion is required by some compilers, such as what I use,

Basically not. The pointer address is required! In short, it gives people an inintuitive and refreshing feeling,

If you change

FREE (pRole );

That's better!

1.1 If you release a struct pointer with a pointer

 

View plaincopy to clipboardprint?
  1. // Role Data Type
  2.  
  3. Typedef Struct
  4. {
  5.   Char* Name; // action file name
  6. } ACT; // Animation
  7.  
  8.  
  9. Typedef Struct
  10. {
  11. ACT * body; // body action data
  12. } Role;

// Role data type typedef struct {char * name; // action file name} ACT; // animated typedef struct {ACT * body; // body action data} Role;

 

Create a role object using a method

 

View plaincopy to clipboardprint?
  1. // Create a role Data Object
  2. Role * creatRole (Const Char* RoleName, uint8 weaponId );
  3. // Reclaim the role Data Object
  4. VoidDestroyRole (Role * role );

// Create the Role data object Role * creatRole (const char * roleName, uint8 weaponId); // reclaim the role Data Object void destroyRole (Role * Role );

 

Then a role pointer can be obtained during creation.

Role * pRole = creatRole ("hero", 0 );

If you want to call

DestroyRole (pRole );

Then, pRole points to NULL. What should I do?

We can learn from the freePointer () method,

 

View plaincopy to clipboardprint?
  1. /* Reclaim the role data object */
  2. VoidDestroyRole (Role * role)
  3. {
  4.   If(Role = NULL)
  5.   Return;
  6.  
  7. DestroyAct (role-> body); // whether to structure the pointer in the body. Release several
  8. Free (role );
  9.  
  10. Role = NULL;
  11. }

/* Reclaim the Role data object */void destroyRole (role * role) {if (role = NULL) return; destroyAct (Role-> body ); // whether the pointer in the structure body should be released several free (role); role = NULL ;}

 

Here, role = NULL; the real parameter pRole cannot be pointed to NULL!

If we mentioned above that using macros or getting pointer addresses can be solved, but it does not need to be so troublesome. The common practice is to manually point to NULL

DestroyRole (pRole );

PRole = NULL;

Why do we have pRole = NULL? Because the upper-layer struct objects usually require the program to know whether or not

It is null or not empty. In addition, it refers to the back-to-origin, which is also good for debugging. The independent free () object cannot be seen during debugging!

We also need to mention that

 

View plaincopy to clipboardprint?
  1. Role * creatRole (Const Char* RoleName, uint8 weaponId)
  2.  
  3. {
  4.  
  5. Role role;
  6.  
  7. Role. body = (ACT *) malloc (Sizeof(ACT ));
  8.  
  9. Return& Role;
  10.  
  11. }

Role* creatRole( const char* roleName, uint8 weaponId ){Role role;role.body= (ACT*)malloc( sizeof(ACT) );return &role;}

 

This method is absolutely not desirable, because role is a temporary variable in the function and will be recycled by the system at the end of the function, data in the random & role location will be reused by the system.

In C language programming, not all pointers need to return NULL. It is better if you want to return NULL, but at least the source of a group of pointers should be pointed back to NULL, this prevents program errors. In addition, before returning the source to NULL, make sure that all the pointers under the pointer have been recycled! Otherwise, the memory will leak!

PS. You cannot reprint it without permission!

This article is from the "key code window" blog and will not be reprinted!

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.