The storage category of the C language variable and the corresponding memory allocation?

Source: Internet
Author: User

From the angle of the existence of variable value, it can be divided into static storage mode and dynamic storage mode. The so-called static storage means that the system allocates a fixed amount of storage space while the program is running (the <strong> program is allocated at the start of execution, released at the end of the program, and in the process they occupy the state of the storage unit, rather than dynamically allocating and releasing </strong>). Dynamic storage is dynamically stored as needed during runtime (<strong> some of the space </strong> that is applied and released during the program). The storage space that the user uses in memory is divided into: program area, static storage area and dynamic storage area. The program is placed in the program area and the data is placed in the static and dynamic storage areas. field protection and return addresses are stored in the dynamic store for function form parameters, automatic variables (local variables that are not static declared), and function calls.
In the C language, each variable and function has two properties: the data type and the storage category of the data. Storage is divided into two main categories: static storage and dynamic storage. Specific protection: Automatic (Auto), static (passive), register (register), external (extern). The above keywords are described separately below.
(1) Auto: This keyword is the default, this keyword belongs to the dynamic store, declared without default initialization.
(2) Static: If you want to keep this value after the local variable call ends (that is, with the Modifier keyword allocating space storage in the static area), there is a default value. A static local variable assigns an initial value at compile time (that is, assigns an initial value once), and the last result is retained for each subsequent run. If the global variable is static, it means that the scope of the variable is limited to this module. (Global variables are stored by default in the static store.) Similarly, in multiple files to prevent name collisions, you can also declare a function as static so that only references within the file
(3) Register: In general, when the variable is in memory, the program uses the variable to send instructions to the memory of the variable sent to the operator, after the operation, and then stored in the seed. In this way, if a variable is used frequently, it is defined as the register type (free local automatic variable and function form parameter), which can be directly placed in the CPU to improve the efficiency. (General compiler for the user to consider this problem, generally do not need to consider)
(4) extern: Used to declare external variables and external functions (as opposed to static one usage), extern is just a declaration rather than a definition. In both cases, extern is declared in the same file (indicating that the variable definition is behind the current reference), and an external variable is declared in multiple files (indicating that the variable is defined outside the file). The same is true for function declarations (general case function declarations are not extern, omitted).

<strong> Note: Classes are encapsulated, so members in a class cannot qualify their storage type with the keyword extern, auto, or register. </strong>

&nbsp;
<H4><STRONG>2.C language memory allocation mechanism </strong>(1) <strong> stack (stack) </strong>: Local variables (including function arguments) within a function, which are assigned by the compiler to be freed, the function ends, and the stack variable is invalidated.

(2) The <strong> heap (heap) </strong>: Allocated by the programmer with Malloc/calloc/realloc, free release. If the programmer forgets free, it will cause a memory leak, and the memory will be recycled by the OS at the end of the program, but the program will not end, it may cause a memory leak. (Programmer is responsible for assigning and releasing)

(3) <strong> Global/static zone </strong>: global variable and static variable storage area, once the program is compiled, the region exists. and the global variables and static variables initialized in the C language and uninitialized in the adjacent two regions (in C + +, because the global variables and the static variable compiler will give these variables automatically initialized assignment, so there is no distinction). Because global variables occupy memory space and are difficult to maintain, it is recommended to use less. Released at the end of the program.

(4) <strong>c style string constant store </strong>: The place where the string constants are specifically stored, released at the end of the program.

(5) <strong> program code area &LT;/STRONG&GT;: The area where the program binary code is stored.
<pre lang= "C" line= "1" escaped= "true" > Instance 1:
int a = 0; Global initialization Zone
Char *p1; Global uninitialized zone (is initialized to NULL in C + +)
int main ()
{
int b; b allocated on the stack, integral type
Char s[] = "ABC"; S is allocated on the stack, char * type; "Abc\0" is allocated on the stack, run-time assignment, function end destroy
Char *p2; P2 allocated on the stack, not initialized
Char *p3 = "123456"; P3 points to "123456" assigned to the address of the string constant store, at compile time to determine P3 stored on the stack
static int c = 0; C in the global (static) initialization zone, you can keep the original value across function calls multiple times
P1 = (char *) malloc (10); P1 in the global uninitialized zone, pointing to the heap address that was allocated 10 bytes
P2 = (char *) malloc (20); P2 points to the heap address where 20 bytes are allocated
strcpy (P1, "123456"); "123456" is placed in the string constant store, and the compiler may optimize it with the "123456" that P3 points to as a block
return 0;
}</pre>
In the C + + language, similar to C, but also different, memory is mainly divided into the following 5 storage areas:
(1) Stack: a local variable within a function (including a function argument), the compiler is responsible for allocating the release, the function ends, and the stack variable is invalidated.
(2) Heap: Here, unlike C, the heap is the memory requested by new, and is released by delete or delete[].
(3) Free Storage: Distributed by programmer with Malloc/calloc/realloc, free release. If the programmer forgets free, it will cause a memory leak and the memory will be recycled by the OS at the end of the program.
(4) Global static area: global variable and static variable storage area, once the program is compiled, the region exists. In C + +, initialization variables and uninitialized variables are not distinguished because global variables and static variable compilers automatically initialize assignments for these variables. It is important to note that both global and local static variables are stored in the same static zone (the global zone), but with different scopes.
(5) Constant storage: This is a special storage area that stores constants that cannot be modified (typically const-modified variables, or some constant strings).
&nbsp;

(1) Stack
In particular, modern computers (von Neumann serial execution mechanisms) are directly supporting the data structure of the stack at the lower level of the code. This is manifested in the fact that there is a dedicated register pointing to the address of the stack (SS, stack segment register, storing the stack segment address), and there is a dedicated machine instruction to complete the data into the stack (the assembly has push and pop instructions).
This mechanism is characterized by high efficiency, but with limited data support, it is generally a data type directly supported by the system, such as integers, pointers, floating-point numbers, etc., and does not directly support other data structures (you can customize the stack structure to support a variety of types). Because of this feature of the stack, the use of stacks is very frequent in the program. The call to the child program is done directly using the stack. The call instruction of the machine implies the operation of putting the return address into the stack and then jumping to the subroutine address, while the subroutine's RET instruction implicitly pops the return address from the stack and jumps to the operation.
The function automatic variable in c/s + + is an example of using the stack directly, which is why when the function returns, the automatic variable of the function is automatically invalidated, so we should avoid returning the stack memory and the stack reference, lest the memory leak.
(2) heap
Unlike stacks, the data structures are not supported by the system (whether it is a machine hardware system or an operating system), but are provided by a library of functions. The basic Malloc/calloc/realloc/free function maintains a set of internal heap data structures (New/delete maintenance is added in C + +).
When the program uses these functions to obtain new memory space, the function first attempts to find available memory space from the internal heap (common memory allocation algorithms are: first-time adaptive algorithm, first-time adaptive algorithm, best adaptive algorithm and worst-fit algorithm, etc.). If there is no memory space available, an attempt is made to dynamically increase the memory size of the program data segment with a system call, and the newly allocated space is first organized into the internal heap and then returned to the caller in the appropriate form. When the program frees allocated memory space, this memory space is returned to the internal heap structure and may be appropriately processed (such as free space merging into larger free space) to better fit the next memory allocation request.
This complex allocation mechanism is actually equivalent to a memory allocation buffer pool (cache), using this mechanism for the following reasons: System calls may not support arbitrary size of memory allocation. Some system calls only support a fixed size and its multiple memory requests (per page allocation), which can be wasteful for large amounts of small memory allocations, which can be costly to request memory from system calls. System calls may involve the conversion of the user state and the kernel mentality, and the memory allocations that are not managed can easily cause memory fragmentation under the allocation release operation of a large amount of complex memory.
(3) stack and heap pair
From the above introduction, they have the following differences:
A, the stack is the system provides the function, the characteristic is the fast high efficiency, the disadvantage is by the restriction, the data is not flexible, but the heap is the function library provides the function, the characteristic is the flexible convenient, the data adapts the surface wide, but the efficiency has certainly reduced.
b, the stack is the system data structure, for the process/thread is unique, the heap is the function library internal data structure, not necessarily unique. Memory allocated by different heaps cannot operate with each other.
c, stack space static allocation and dynamic allocation, generally by the compiler to complete the static allocation, automatic release, the dynamic allocation of the stack is not encouraged; heap allocation is always dynamic, although all the data space will be released to the system at the end of the program, but accurate application memory/release memory matching is the basic element of good program.
D, fragmentation problem: For the heap, frequent new/delete and other operations will inevitably cause memory space discontinuity, resulting in a large number of fragments, so that the efficiency of the program is reduced; for the stack, there is no problem because the stack is a last-in, first-out (LIFO) queue.
E, growth direction: The growth direction of the heap is upward, that is, to the memory address of the direction of increase, for the stack, the growth direction is downward, is to reduce the memory address in the direction of growth.
F, Distribution: The heap is dynamically allocated, there is no statically allocated heap, there are two ways to allocate the stack: static allocation and dynamic allocation. Static allocations are done by the compiler, such as the allocation of local variables. The dynamic allocation is allocated by the Alloca function, but the dynamic allocation of the stack is different from the heap, and its dynamic allocation is released by the compiler without our manual implementation.
G, allocation efficiency: The stack is a machine system provides data structure, the computer provides support at the bottom, the allocation of special stack segment registers, into the stack out of the stack has a special machine instruction, which determines the efficient execution of the stack. And the heap is provided by the C + + function library, the mechanism is more complex, there are different allocation algorithms, easy to produce memory fragmentation, the need for memory management, the efficiency of the stack is much lower.

&nbsp;

Example 2:

Look at the following small piece of code, the difference between heap and stack:

&nbsp;
<pre lang= "C" line= "1" escaped= "true" >
int foo ()
{
The rest of the code
int *p = new Int[5];
The rest of the code
return 0;

}
</pre>
where the statement int *p = new Int[5], contains the heap and stack. Where the new keyword allocates a heap of memory, and the pointer p itself occupies memory as stack memory (typically 4 bytes for the address). This means that a pointer to a heap of memory is stored in the stack memory p. In the program, first determine the size of the memory allocated in the heap, and then call the New keyword to allocate memory, and finally return the memory first address, put into the stack. The assembly code for this code under VC6 is:
<blockquote>00401028 Push 14h

0040102A call operator new (00401060)

0040102F Add esp,4

00401032 mov dword ptr [Ebp-8],eax

00401035 mov eax,dword ptr [ebp-8]

00401038 mov dword ptr [ebp-4],eax</blockquote>
If we need to free up memory, here we need to use delete[] p to tell the compiler that I'm going to delete an array.

&nbsp;

Example 3:

Look at the following snippet of code and try to find the error:
<pre lang= "C" line= "1" escaped= "true" >
#include &lt;iostream&gt;
using namespace Std;

int main ()
{
chara[] = "Hello";
a[0]= ' X ';
cout&lt;&lt; a&lt;&lt; Endl
Char*p = "World";
p[0]= ' X ';
cout&lt;&lt; p&lt;&lt; Endl
Return0;
}</pre>
Did you find the problem? Yes, the capacity of character array A is 6 characters, and its content is "hello\0". The content of a can be changed, such as a[0]= ' X ', because it is allocated on the stack, which is what is determined at run time. But pointer p points to the string "world" allocated in the string constant store, content "world\0", the contents of the constant string can not be modified. Syntactically, the compiler does not think that the statement p[0]= ' X ' has any problems, but at run time there is an issue with "accessviolation" illegal memory access.

The storage category of the C language variable and the corresponding memory allocation?

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.