C Language memory allocation-popular understanding __c language

Source: Internet
Author: User
Tags arithmetic

Author: huaqing visionary lecturer

Recently many students want to understand the C language memory allocation, although can find a lot of explanations on the Internet, but you will find either not easy to understand, or not too comprehensive. And these for the novice, and will definitely make you dizzy, so on the Internet and books on all these explanations, the popular translation and summary.

Before talking about memory allocation, let's take a bit of a digression, because when you talk to your classmates about memory allocations, they don't quite understand why they should be assigned. So first of all, the composition of the computer and the basic principles.

I. Composition of the computer

The five major components of the computer: the operator, the controller, the memory, the input device and the output device.

We all know that the computer's processing center is the CPU, which is mainly composed of operators and controllers.

1) Arithmetic device

The part that realizes arithmetic operation and logic operation, mainly carries on processing to the data.

2) Controller

The command center of the computer, it accesses the memory through the address, takes out the instruction (program) from the memory, and points out the position of the next instruction in the memory, will take out the instruction to the instruction decoder by the instruction register, after the analysis of the instruction produces the corresponding operation, controls the orderly work of other

The execution instruction has four steps: takes the instruction, the instruction decoding, executes according to the instruction operation code, forms the next instruction address.

3) Memory

The computer holds the memory part of all data and program, it is divided into two kinds: one is internal memory (memory) and the other is external memory. The memory is composed of several memory units, each of which has an address, and the computer reads and writes the memory unit by address.

4) Input Equipment

A device that enters information (Programs, data, sounds, text, graphics, images, etc.) into a computer (keyboard, mouse, graphics scanner, touch screen, barcode input, stylus, etc.).

5) Output device

Mainly display, printer and plotter.

Second, memory allocation

Memory management is important in any programming environment and language. In the current computer system or embedded systems, memory resources are still limited. Therefore, in the program design, the effective management of memory resources is the first consideration of the programmer

1 C Program Structure: When executable code is stored

The following are the basics of the C language executable program:


The above are: Code area, global initialization data area/static data area, uninitialized data area, decimal sum, hexadecimal sum, file name.

We can see that when the program is not running, it is divided into three parts: the Code area (text), the data area (information), the Uninitialized data area (BSS) before it is transferred into memory.

(1) Code area (text)

Store CPU executable machine instructions, because the program is often used to prevent accidental modification of the code area is usually read-only.

(2) Global initialization data area/static data area

Holds the initialized global variables, static variables (global static and local static variables), constant data (such as String constants).

(3) Uninitialized data area (BSS)

Holds uninitialized global variables, the term BSS is based on an earlier assembly operator, which marks the beginning of a block. The data in the BSS is initialized by the kernel to 0 or null (NULL) before the program begins executing.

2 C Program Structure: when the program executes

A running C program that occupies 5 areas of memory: Code area, initialization data area/static data area, uninitialized data area, heap area, stack area.

(1) Code area (text)

Code area instructions are executed sequentially according to the program design process, and for sequential instructions, they will only be executed once, if repeated, you need to use the jump instruction, if recursion, you need to implement with the help of the stack.

The code area includes the opcode and the object to be manipulated (or an object's address reference). If the number is immediate (that is, a specific value, such as 2), it is included directly in the code, and if it is local data, the space is allocated in the stack and the address of the data is referenced, and the address of the data is referenced in the code, if it is the BSS and data area

(2) Global initialization data area/static data area

Initialize only once. As mentioned above, when the program is compiled, the area is already allocated, which exists throughout the running of the program, and is released when the program ends.

(3) Uninitialized data area (BSS)

Changes its value at run time.

(4) Stack area (stacks)

The parameter values and local variables of the function are stored and released automatically by the compiler, which is similar to the stack of the data structure. The feature is that the programmer does not need to consider the problem of memory management, very convenient, while the capacity of the stack is limited, in the Linux system, the stack capacity of only 8M, and when the corresponding range at the end (such as functions), local variables can no longer be used.

(5) Heap area (heap)

Some action objects can be determined only when the program is running, so that the compiler cannot allocate space for them at compile time, only when the program is run, which is the dynamic memory allocation. Heap area is used for dynamic memory allocation (such as malloc dynamic memory allocation), the heap is in memory between the BSS area and the stack area, generally by the programmer to request and release.

The reason for allocating so many areas is mainly because:

When a process is running, the code executes sequentially according to the process, and the code only needs to be accessed once, of course, the code is executed multiple times when the jump or recursion is performed, and the data is typically accessed multiple times, so separate space is created to access and save space.

The following is a detailed code to fully analyze the memory allocation:

Main.c

int a = 0; A in the global initialization data area

Char *p1; P1 in the BSS area (global variable not initialized)

static int c = 0; C in the global initialization data area (c is a global static variable)

struct employee

{

Char name[20];

int age;

Float score;

}e1; E1 in the global initialization data area

int main ()

{

int b; b in the stack area (local variables)

Char s[] = "ABC"; s in the stack area, "ABC" in the Constant area (global initialization data area)

Char *p2; P2 in the Stack area

Char *p3 = "123456"; P3 in the Stack area, "123456" in the constant area (global initialization data area)

static int d = 0; D in the global initialization data area (static local variables)

struct student

{

Char *name; Name is in the stack area and the name pointer is in the heap area

int age;

Float score;

}S1; S1 in the Stack area

P1 = (char*) malloc (10); Allocated 10 bytes of area in the heap area

P2 = (char*) malloc (20); Allocated 20 bytes of area in the heap area

Name = (Cahr *) malloc (20); Allocated 20 bytes of area in the heap area

/* Copy from the "Hello World" string in the constant area to the newly allocated heap area * *

strcpy (P1, "Hello World");

Free (p1); Free memory

Free (p2); Free memory

}

Article selected from the Huaqing Vision embedded Training

>>> more excellent Technical blog daily updates

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.