Do you know where the variables in embedded c are stored?

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

Local variable: stack area

Local static variable: static area

Global variable: Constant area of ​​static area

Global static variable: static area

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

1. Stack area - The compiler automatically allocates the release, stores the parameter values ​​of the function, the value of the local variable, and so on. It operates in a similar way to the stack in the data structure.

2, heap (heap) - generally released by the programmer, if the programmer does not release, the program may be recycled by the OS at the end of the program. Note that it is not the same as the heap in the data structure, the allocation is similar to the linked list.

3, global area (static area) (static) -, global variables and static variables are stored in a block, initialized global variables and static variables in a region, uninitialized global variables and uninitialized static variables in the phase Another area of ​​the neighborhood. - System release after program ends

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

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

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 is in the constant area, p3 is on the stack.

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

P1 = (char *) malloc(10);

P2 = (char *)malloc(20);

The area allocated 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 the "123456" pointed to by p3.

}

In the C language program, there are the following points to note about the use of variables:

1. The 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 compiler.

2. The memory allocated by the functions that allocate memory using malloc, calloc, realloc, etc. is on the heap. The program must be guaranteed to use free release, otherwise a memory leak will occur.

3. All functions are defined in the global variable in vitro. The variables added with static are placed in the global area either inside or outside the function.

4. Variables defined with const will be placed in the read-only data area of ​​the program.

Three: the use of the middle of the program

The following is a simple example to illustrate the correspondence between variables and segments in C language. The global area (static area) in the C language program actually corresponds to the following segments: 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"}; //Initialized read and write data segment

Char BSS_1[ 100]; // uninitialized data segment

Const char *ptrconst ="constant data"; //string placed in only read data segment

Int main()

{

Short b; // on the stack, occupy 2 bytes

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

Char s[ ]="abcdefg"; //s on the stack, occupying 4 bytes

// "abcdefg" itself is placed in a read-only data store, which occupies 8 bytes.

Char *p1; //p1 on the stack, occupying 4 bytes

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

//"123456" in the middle read data area

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, which is 5 bytes.

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

Return 0;

}

GPS Antenna

China GPS Antenna,High Gain GPS Antenna.Factory Price GPS Antenna

gps glonass antenna,gps active antenna,marine gps antenna,gnss & gps antenna

Yetnorson Antenna Co., Ltd. , https://www.yetnorson.com