Memory Dynamic Allocation Function and Storage Class in C

Source: Internet
Author: User

In C, there are a total of four dynamic memory allocation functions, which are defined in "stdlib. H" or "alloc. H" as follows:

(1) void * calloc (unsigned num, unsigned size). The returned value is a void pointer. In C, a void pointer can be assigned to any type of pointer, in this way, we can get any type of space. The num parameter indicates the number of blocks and the size indicates the size of each block. The unit is byte. For example, void * calloc (10, sizeof (INT) can get a space of 10 int numbers. The most common is stru * strulp = calloc (2, sizeof (stru) to get the space size of two stru struct.

(2) void * malloc (unsigned size). The function of this function is equivalent to * calloc (1, unsigned size), that is, a size of memory space is obtained.

(3) void * realloc (void * buffer, unsigned newsize). This function changes the buffer pointer space to newsize, if newsize is larger than oldsize (used to indicate the size of the original buffer space), the content in the storage area will definitely remain unchanged. If newsize is smaller than oldsize, some original content will not be saved, after calling this function, the buffer's first address may be different from the original buffer's first address.

(4) void free (void * buffer). This function releases the storage zone to which the buffer points.

CThere are three storage zones, namely static zone (Data Segment), dynamic zone (stack segment), and general register zone of the CPU. Both static zone and dynamic zone are in the memory, general registers use space on the CPU. c supports four storage classes:

(1) Auto: the automatic type is stored in the dynamic storage area of the computer. Therefore, it must be a local variable. Its scope is limited to the function. After the function is executed, the space will be automatically released. Generally, we omit auto when defining the function, and directly write auto int I;, which is the most common storage type.

(2) registers: register type, which is stored in the General registers of the CPU. Because it is on the CPU, there cannot be many variables of this type, generally no more than two, generally, the General registers are 16-bit. Therefore, for variable types with more than 16 bits, the storage methods of register types, such as double and float, cannot be used. The use of register-type variables can directly communicate with the CPU, thus saving the memory access time. In this way, the program running speed can be improved.

(3) static: static type, which is located in the static storage area together with extern. Static variables do not release space because of the function, but end until the end of the program, static variables are initialized only when they are defined. If Initialization is not performed, the system will automatically initialize them. Generally, the initialization is 0, and the initialization statement is after initialization, will not be executed again.

(4) extern: External type, which is also a variable stored in the static zone. This variable is allocated when it is defined and defined outside the function. In the function, when an extern is encountered, the system does not allocate space for the variable, but searches outside for the variable.

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.