Memory Management (usage of malloc and free in C)

Source: Internet
Author: User

In C, memory management is implemented through special functions. In addition, to be compatible with various programming languages, the interface provided by the operating system is usually a function declaration written in C Language (Windows itself is also written in C and assembly language ).

The header file is <stdlib. h> and <malloc. h>

1. Memory Allocation malloc Function

Header files must be included:

# Include

Or

# Include

Function declaration (function prototype ):

Void * malloc (int size );

Description: malloc applies to the system for allocating a specified size of bytes of memory. The return type is void. Void * Indicates a pointer of unknown type. C, C ++ stipulates that the void * type can be forcibly converted to any other type of pointer.

We can see from the function declaration. There are at least two differences between malloc and new: new returns a pointer of the specified type and can automatically calculate the required size. For example:

Int * p;

P = new int; // The return type is int * (integer pointer) and the allocated size is sizeof (int );

Or:

Int * parr;

Parr = new int [100]; // The returned type is int * (integer pointer) and the allocated size is sizeof (int) * 100;

While malloc must be calculated by the number of bytes and forcibly converted to the actual type of pointer after the return.

Int * P;

P =(Int *)Malloc (Sizeof (INT));

First, the malloc function returns the void * type. If you write it as P = malloc (sizeof (INT), the program cannot be compiled and an error is returned: "You cannot assign void * to int * type variables ". Therefore, you must pass(Int *)To force the conversion.

Second, the real parameter of the function is sizeof (INT), used to specify the size required for an integer data. If you write:

Int * P = (int *) malloc (1 );

The code can also be compiled, but in fact only one byte memory space is allocated. When you store an integer in it, three bytes will be left homeless, and you can directly "Live in the neighbor's house "! The result is that all the original data in the memory is cleared.

Malloc can also achieve the effect of new [], and apply for a continuous memory, the method is nothing more than specify the memory size you need.

For example, you want to allocate 100 int-type space:

Int * P = (int *) malloc (sizeof (INT) * 100); // allocate memory space that can be placed in the next 100 integers.

Another difference that cannot be seen directly is that malloc only allocates memory and Cannot initialize the obtained memory. Therefore, the value of a new memory will be random.

In addition to the allocation and final release methods, you can use malloc or new to obtain pointers, Which are consistent in other operations.

 

2. Release the memory free function

The header file must be included (the same as malloc ):

# Include

Or

# Include

Function declaration:

Void free (void * block );

That is, void free (pointer variable );

The pointer in the form parameter is declared as void * because free must be able to release any type of pointer, and any type of pointer can be converted to void *.

Example:

Int * p = (int *) malloc (4 );

* P = 100;

Free (p); // release the memory space specified by p.

Or:

Int * p = (int *) malloc (sizeof (int) * 100); // allocate memory space that can be placed in the next 100 integers.

......

Free (p );

Free can be correctly released no matter how much space Your Pointer Points to. This release is more convenient than delete/Delete. However, you must note that if you use new or new [] When allocating pointers, sorry, when you release the memory, you cannot use free to release images easily. In turn, you cannot use delete/Delete [] to release the memory allocated by malloc. In a word, the new/delete, new []/Delete [], and malloc/free pairs must be used together and cannot be used in combination!

Int * P = new int [100];

......

Free (p); // error! P is obtained from new.

 

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.