C language Dynamic Storage Allocation Function

Source: Internet
Author: User

The system automatically performs type conversion. Link: http://www.hackbase.com/tech/2009-02-02/44968.html

 

 

To achieve dynamic storage allocation, in addition to using struct containing pointer members, you also need to use several standard library functions provided by C language. (The header file "alloc. H", "malloc. H", or "stdlib. H" should be included when used ")

1. malloc function: Open up a specified storage space and return the starting address of the storage area. Function prototype:
Void * malloc (unsigned int size)

The size is the number of bytes to be opened. The function returns a pointer that does not point to a specific type. When the pointer is assigned to a specific pointer variable, a forced type conversion is required. If the size exceeds the available space, null pointer is returned.
For example

Float * P1;
Int * P2;
P1 = (float *) malloc (8 );
* P1 = 3.14;
* (P1 + 1) =-3.14;
P2 = (int *) malloc (20 * sizeof (INT ));
For (I = 0; I <20; I ++)
Scanf ("% d", P2 ++ );

The storage space of 8 bytes and 20 × 2 bytes is opened, and data is stored in the storage space.

2. calloc function: Open up storage space based on the number of data given and the number of bytes occupied by each data. Function prototype:
Void * calloc (unsigned int num, unsigned int size)

Num indicates the number of data, and size indicates the number of bytes for each data. Therefore, the total number of bytes is num * size. The function returns the starting address of the bucket.
For example, p2 can be rewritten
P2 = (int *) calloc (20, sizeof (int ));
For example
Calloc (10, 20)
You can open up 10 buckets with a size of 20 bytes.

3. realloc function: redefine the size of the opened memory. Function prototype:
Void * realloc (void * ptr, unsigned int size)

The ptr refers to the memory space that has been opened using the aforementioned functions. The size is the new space size, and its value is larger or smaller than the original one. The function returns the starting address of the new storage area (this address may be different from the previous address ). For example
P1 = (float *) realloc (p1, 16 );
Adjust the original 8 bytes to 16 bytes.

4. free function: release a previously opened memory space. Function prototype:
Void free (void * ptr)

Ptr is the pointer variable that stores the start address of the space to be released. The function has no return value. Note: The space pointed to by ptr must be opened by the aforementioned function. For example
Free (void *) p1 );
Release the 16 bytes opened in the previous example. Can be abbreviated
Free (p1 );

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.