Talking about the storage location of variables in the MCU

Local variables, local static variables, global variables, global static variables difference:

Local variables: Stack area

Local static variables: static area

Global Variables: Constant Area Constant Area

Global static variables: static area

In C/C++ programming, programmers need to have a more accurate understanding of memory. The memory that often needs to operate can be divided into the following categories:

1, stack (stack) - automatically allocated by the compiler to release, store the parameter values ​​of the function, the value of local variables. It operates like a stack in a data structure.

2, Heap (heap) - generally allocated by the programmer to release, if the programmer does not release, the program may be recovered by the OS. Note that it is different from the heap in the data structure, and the distribution method is similar to the linked list.

3, the global area (static area) (staTIc) -, the storage of global variables and static variables is put together, initialized global variables and static variables in a region, uninitialized global variables and uninitialized static variables in phase Another area adjacent to. - System released after the end of the program

4, text constant area - constant string is placed here. Released by the system after the program ends

5, program code area - store the binary code of the function body.

Talking about the storage location of variables in the MCU

The following is a practical description of the program code:

This is written by a senior, very detailed

Int a = 0; global initialization area

Char *p1; global uninitialized area

Main()

{

Int b; stack

Char s[] = "abc"; stack

Char *p2; stack

Char *p3 = "123456"; 123456 in the constant area, p3 on the stack.

staTIc int c =0; global (static) initialization area

P1 = (char *)malloc(10);

P2 = (char *)malloc(20);

The allocated area of ​​10 and 20 bytes is in the heap area.

Strcpy(p1, "123456"); 123456 is placed in the constant area and the compiler may optimize it to a place with "123456" pointed to by p3.

}

In the C language program, the following points need to pay attention to the use of variables:

1. Variables defined in the body are usually on the stack and do not need to be managed in the program, and are processed by the editor.

2. The memory allocated by the functions that allocate memory such as malloc, calloc, realloc, etc., is on the heap. The program must ensure that free is used, otherwise a memory leak will occur.

3. All the functions defined in the body are global variables, and the variables after adding staTIc are placed in the global area either inside or outside the function.

4. Use const defined variables will be placed in the program's read-only data area.

Use of program segments

The following uses a simple example to illustrate the correspondence between variables and segments in the C language. The global area (static area) in the C language program actually corresponds to the following sections: RO Data; RW Data; BSS Data.

In general, a directly defined global variable is in the uninitialized data area. If the variable is initialized, it is in the initialized data area (RW Data), and const is placed in the read-only data area.

Example: const char ro[ ] = {"this is read only data"}; / / read-only data area

staTIc char rw_1[ ]={"this is global read write data"}; //The data segment has been initialized

Char BSS_1[ 100]; //uninitialized data segment

Const char *ptrconst = "constant data"; //String puts only read data segment

Int main()

{

Short b; //On the stack, occupying 2 bytes

Char a[100]; //Open up 100 bytes on the stack, the value of the worker is its first address

Char s[ ]=“abcdefg”; //s is on the stack, occupying 4 bytes

// "abcdefg" itself is placed in the read-only data store, accounting for 8 bytes

Char *p1; //p1 is on the stack and occupies 4 bytes

Char *p2="123456"; //p2 On the stack, the content pointed to by p2 cannot be changed.

//"123456" read data area in Chinese

Static char rw_2[ ]={"this is local read write data"};//Locally initialized read and write data segments

Static char BSS_2[100]; //Local uninitialized data segment

Static int c = 0; // global (static) initialization area

P1 = (char *) malloc (10 * sizeof (char)); / / allocate memory area in the heap area

Strcpy(p1, "xxxx"); //"XXXX" is placed in the read-only data area, accounting for 5 bytes

Free(p1); //free to release the memory pointed to by p1

Return 0;

}

0.8mm Male Header

0.8mm Pin Header

Antenk 0.8mm Pitch Male Header series is a fine pitch, low profile, single/dual/three/four row, PCB mounted connector set intended for limited space applications or where total weight is a factor. Our specially tooled insulators and contacts maintain consistent high quality through our automated production processes. Each series is available in thru-hole PCB or SMT mounting and plated tin, gold or selective gold as specified.



0.8mm Pin Header Options


Number of Rows
1/Single
2/Double
3/Three
4/Quad

Number of Positions
2 Position
3 Position
4 Position
5 Position
6 Position
8 Position
10 Position
12 Position
14 Position
15 Position
16 Position
17 Position
20 Position

Termination Style
SMD/SMT
Through Hole

Mounting Angle
Right Angle
Straight


0.8mm Pin Header Specifications:

Material: Standard Hi-Temp insulator: Nylon 6T, rated UL94V-0
Insulator Color: Black
Contacts: Phosphor Bronze
Plating:
U = Gold over nickel underplate
SG = Gold over nickel underplate on
contact area, tin over copper underplate on tails.
T = Tin over copper underplate overall.
Electrical:
Operating voltage: 250V AC max.
Current rating: 1 Amp max
Contact resistance: 20 mΩ max. initial
Insulation resistance: 5000 MΩ min.
Dielectric withstanding voltage: 1000V AC for 1 minute
Mechanical:
Mating durability: 500 cycles min.
Temperature Ratings: Operating temperature: -40°C to +105°C
Max process temp: 230°C for 30 ~ 60 seconds (260°C for 10 seconds)
Soldering process temperature: 260°C

Packaging:
Anti-ESD plastic bags or tubes

Approvals and Certifications:
UL Recognized File no. E224053


Pin Header,0.8Mm Male Header,0.8Mm Pin Header,0.8Mm Male Header Pins,0.8mm Pitch Pin Header,SMT 0.8mm Pin Header, THT 0.8mm Pin Header

ShenZhen Antenk Electronics Co,Ltd , https://www.antenkelec.com

Posted on