
Due to changes in the public account’s push rules, please click “See” and add “Star” to get exciting technical shares at the first time
Source from the internet, please delete if infringing
1. Memory Areas
1.1 Analysis of Data Type Essence
1.1.1 Concept of Data Type
● “Type” is an abstraction of data
● Data of the same type has the same representation format, storage format, and related operations
● All data used in the program must belong to a certain data type
1.1.2 Essence of Data Type
● Data types can be understood as molds for creating variables: they are aliases of fixed memory size.
● The role of data types: the compiler estimates the size of memory space allocated for the object (variable).
● Note: Data types are just molds; the compiler does not allocate space, only when creating variables (actual objects) based on types (molds) will the compiler allocate space.
1.2 Analysis of Variable Essence
1.2.1 Concept of Variable
Concept: A memory object that can be read and written is called a variable; an object that cannot be modified after initialization is called a constant.
Variable definition format: type identifier, identifier, …, identifier;
1.2.2 Essence of Variable
1. The program applies for and names memory space through variables, e.g., int a = 0.
2. Access memory space through variable names.
1.3 Memory Area Model of Program
Process description
1. The operating system loads the physical hard disk code into memory
2. The operating system divides the C code into four areas
Stack Area (stack): Automatically allocated and released by the compiler, stores function parameter values, local variable values, etc. |
Heap Area (heap): Generally allocated and released by the programmer (dynamic memory allocation and release), if the programmer does not release it, it may be reclaimed by the operating system when the program ends. |
Global Area (Static Area) (static): The storage of global variables and static variables is placed in a block, initialized global variables and static variables are in one area, uninitialized global variables and uninitialized static variables are in an adjacent area, which is released by the operating system after the program ends. |
Constant Area: The storage location of string constants and other constants, released by the operating system after the program ends. |
Program Code Area: Stores the binary code of the function body. |
3. The operating system finds the entry point of the main function to execute
1.4 Function Call Model
1.5 Analysis of Variable Passing in Function Calls
(1)
(2)
(3)
(4)
(5)
1.5 Growth Direction of Stack and Memory Storage Direction
Related code:
02_data_type_essence.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
int main()
{
int a;//Tell the compiler to allocate 4 bytes
int b[10];//Tell the compiler to allocate 4*10 bytes
/*
Essence of type: fixed memory block size alias
Can be tested with sizeof()
*/
printf("sizeof(a)=%d,sizeof(b)=%d\n", sizeof(a), sizeof(b));
//Print address
//Array name, first element address, array base address
printf("b:%d,&b:%d\n",b,&b);//Addresses are the same
//b, &b array types are different
//b, base address of the first element, one element is 4 bytes, +1 address +4
//&b, base address of the entire array, one array is 4*10=40 bytes, +1 address +40
printf("b+1:%d,&b+1:%d\n", b + 1, &b + 1);//Different
//Pointer type length, 32-bit machine 32-bit system length is 4 bytes
// 64 64 8
char********* p = NULL;
int* q = NULL;
printf("%d,%d\n", sizeof(p), sizeof(q));//4 , 4
return 0;
}
03_give_type_alias.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
typedef unsigned int u32;
//typedef used with structure
struct Mystruct
{
int a;
int b;
};
typedef struct Mystruct2
{
int a;
int b;
}TMP;
/*
void no type
1. Function parameter is empty, use void to modify when defining function int fun(void)
2. Function has no return value: use void void fun (void)
3. Cannot define ordinary variable of void type: void a;//err cannot determine what type
4. Can define void* variable void* p;//ok 32-bit system is always 4 bytes
5. Essence of data type: fixed memory block size alias
6. void *p universal pointer, function return value, function parameter
*/
int main()
{
u32 t;//unsigned int
//Define structure variable, must add struct keyword
struct Mystruct m1;
//Mystruct m2;//err
TMP m3;//typedef used with structure
struct Mystruct2 m4;
printf("\n");
return 0;
}
04_variable_assignment.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
int main()
{
//Essence of variable: an alias for a continuous memory space
//Variable is like a door number, memory is like a room
int a;
int* p;
//Direct assignment
a = 10;
printf("a=%d\n", a);
//Indirect assignment
printf("&a:%d\n", &a);
p = &a;
printf("p=%d\n", p);
*p = 22;
printf("*p=%d,a=%d\n", *p, a);
return 0;
}
05_global_area_analysis.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
int main()
{
//Essence of variable: an alias for a continuous memory space
//Variable is like a door number, memory is like a room
int a;
int* p;
//Direct assignment
a = 10;
printf("a=%d\n", a);
//Indirect assignment
printf("&a:%d\n", &a);
p = &a;
printf("p=%d\n", p);
*p = 22;
printf("*p=%d,a=%d\n", *p, a);
return 0;
}
06_heap_stack_analysis.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
char* get_str()
{
char str[] = "abcdef";//Content allocated in stack, memory released after function execution
printf("%s\n", str);
return str;
}
char* get_str2()
{
char* temp = (char*)malloc(100);
if (temp == NULL)
{
return NULL;
}
strcpy(temp, "abcdefg");
return temp;
}
int main()
{
char buf[128] = { 0 };
//strcpy(buf,get_str());
//printf("buf = %s\n", buf);//Garbage, uncertain content
char* p = NULL;
p = get_str2();
if (p != NULL)
{
printf("p=%s\n", p);
free(p);
p = NULL;
}
return 0;
}
07_static_local_variable.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
int* getA()
{
static int a = 10;//In static area, static area in global area
return &a;
}
int main()
{
int* p = getA();
*p = 5;
printf("%d\n",);
return 0;
}
08_stack_growth_direction.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
int* getA()
{
static int a = 10;//In static area, static area in global area
return &a;
}
int main()
{
int* p = getA();
*p = 5;
printf("%d\n",);
return 0;
}
If you are over 18 years old and think learning [C language] is too difficult? Want to try other programming languages, then I recommend you learn Python, currently a valuable 499 yuan Python beginner course is available for free, limited to 10 spots!
▲ Scan the QR code - Get it for free
