Detailed description of the malloc Function

Source: Internet
Author: User

I. prototype: extern void * malloc (unsigned int num_bytes );

Header file: # include <malloc. h> or # include <alloc. h> (Note: The contents of alloc. h and malloc. h are exactly the same .)

Function: allocate memory blocks of num_bytes bytes.

Note: If the allocation is successful, the pointer pointing to the allocated memory is returned. Otherwise, the NULL pointer is returned.

When the memory is no longer used, use the free () function to release the memory block.

 

Example:
# Include <stdio. h> <br/> # include <malloc. h> <br/> int main () <br/>{< br/> char * P; </P> <p> P = (char *) malloc (100); <br/> If (p) <br/> printf ("memory allocated at: % x/N", P ); <br/> else <br/> printf ("not enough memory! /N "); <br/> free (p); <br/> return 0; <br/>}</P> <p>

 

 

Ii. 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. You can find related explanations on msdn. The specific content is as follows:

MallocReturns a void pointer to the allocated space, orNullIf there is insufficient memory available. To return a pointer to a type otherVoid, Use a type cast on the return value. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object. If size is 0,MallocAllocates a zero-length item in the heap and returns a valid pointer to that item. Always check the return fromMalloc, Even if the amount of memory requested is small.

Iii. Differences between malloc and new

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 automatically calculates 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 use (int *) to forcibly convert the data.

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.

 

Summary:

The malloc () function finds a space of the specified size in the memory, and then gives the first address range of the space to a pointer variable. The pointer variable here can be a separate pointer, it can also be the first address of an array, depending on the specific content of the parameter size in the malloc () function. Here, the memory space allocated by malloc is logically continuous, and physically continuous or discontinuous. For our programmers, we focus on logical continuity, because the operating system will help us arrange memory allocation, so we can use it as continuous.

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.