当前位置:网站首页>C language: 13. Pointer and memory
C language: 13. Pointer and memory
2022-07-27 19:34:00 【Brother rabbit head】
c Language :13、 Pointers and memory
The code debugging below is centos64 Bit system
1、 Data representation in computer
Base number
Use... In a computer 2 Base number 、8 Base number 、16 Hexadecimal represents data ;
Computers process data in binary , The computer calculation results are displayed to people in decimal , When programming to represent binary data , Because the content is too long , So use hexadecimal
2、 memory management
No matter how many memory modules are inserted into the computer, the computer will calculate the memory size as a whole ;
32 Bit system can only be used even if more memory is inserted 4G Memory , because 32 Bit hardware platform cpu The address bus has only 32 position , That is, the address space of the operating system is only 32 position .32 Bit means that the memory number can only be numbered to 32 Binary bits , Therefore, it can only be operated in the end 4G Of memory space . Detailed interested friends can search by themselves .
64 The bit operating system has 2^64, Therefore, theoretically enough for our current use .
Memory management of operating system
Memory is managed by the operating system ; In addition to numbering the memory , You can also plan the memory , For example 64 Bit operating system , As long as the memory used by the user program has the previous one 48 Just a bit .
The operating system isolates user memory from operating system memory , front 48 Bits are used by the user , Called user memory , The latter is used by the operating system .
After isolation , Even if the user's memory is unreasonably full , It will not affect the memory of the operating system , You can also use the operating system to shut down unreasonable memory consuming processes . Make memory safer .
C The syntax of the language does not allow us to directly manipulate the code segment memory ;
standard C Language syntax does not support direct operation on a memory address , The operating system will be deemed as illegal operation . Because it may operate on the memory of the operating system or the memory of other applications , This is not reasonable , Only the memory allocated by the operating system to the application is a reasonable operation .
The division of memory by the operating system :
The memory address of the code segment is allocated from bottom to top , The data segment is also , Stack memory is allocated from top to bottom .
Therefore, the variable memory address in the code segment and data segment is the smaller the memory address allocated first , The variables in the stack memory are the larger the address allocated first .
3、 The nature of variables and pointers
The nature of variables :
The variable name is just a code , The essence of variables is memory .
When writing a program , The computer processes all binary data , When the program runs, binary data is loaded into memory CPU To take it out .
CPU To get data from memory , that CPU You must know from which memory unit , And the variable is to play a role of identification , tell CPU Where to get data , Or write the data somewhere . This is the nature of variables .
The nature of the pointer :
The pointer stores the memory address , So a pointer is essentially an address .
Proof of memory continuity :
The debugging example below proves that the memory is continuous :
#include <stdio.h>
int main()
{
int a = 3;
int b = 2;
int array[3];
array[0] = 1;
array[1] = 10;
array[2] = 100;
int *p = &a;
int i;
for(i=0; i<6; i++){
printf("*p=%d\n",*p);
p++;
}
printf("------------------------------\n");
p=&a;
for(i=0; i<6; i++){
printf("p[%d]=%d\n", i, p[i]);
}
return 0;
}

4、 Program in memory debug
4.1、 To write main.c
#include <stdio.h>
int global = 0;
int rect(int a, int b)
{
static int count =0;
count++;
global++;
int s=a*b;
return s;
}
int quadrate(int a)
{
static int count=0;
count++;
global++;
int s=rect(a,a);
return s;
}
4.2、 Compile code
gcc -g main.c
4.3、gdb Debugging program

analysis
It can be seen from the above figure , The source code is loaded into memory ( In the memory code segment ) after , Be able to rely on gdb Wait for debugging tools to debug , So the program is running , Program except in code segment , The information below is recorded in memory Stack in :
- Which function is being called
- How many lines does the currently called function run , And what variables are in this function , What are the values of these variables
Memory is allocated in the memory stack to the currently running function , And all the states in the function execution process are recorded in the memory stack ( It's like taking a picture ), For example, the above figure records the current code running to 27 That's ok
A function in the memory stack can be called many times , When called many times , Each call is a separate stack .
4.4、 Function memory
In the code segment, the memory address is transferred from low address to high address , The function address declared first is small , The function address declared after is large .
4.5、 Variable memory size

4.6、 Memory size of pointer variable

5、 How function stacks work
Continue debugging 4 The code in step
The characteristics of the stack : First in, then out
The stack address first allocated in the stack memory is the largest 
6、 Debugging of pointer self addition
6.1、 debugging
#include <stdio.h>
int main()
{
int a = 3;
int b = 2;
int array[3];
array[0] = 1;
array[1] = 10;
array[2] = 100;
int *p = &a[0];
int i;
for(i=0; i<6; i++){
printf("*p=%d\n",*p);
p++;
}
return 0;
}

#include <stdio.h>
int main()
{
int a = 3;
int b = 2;
int array[3];
array[0] = 1;
array[1] = 10;
array[2] = 100;
int *p = &array[0];
p += 3;
*p = 101;
/* p += 3; *p = 101; And p[4]=101; Equivalent , Pointers can also be accessed in the form of arrays */
p = &array[0];
int i;
for(i=0; i<6; i++){
printf("*p=%d\n",*p);
p++;
}
return 0;
}
Run the code above , give the result as follows 
6.2 Pointers are essentially arrays
Such as the operation of the lower two pointer offsets , Pointers can be manipulated in the form of arrays ,
It can be understood as : In essence, pointer type is array variable 、 Array data types are like array constants ;
int a = 2;
int *p = &a;
p += 3;
*p = 101
// The upper two lines of code are equivalent to the lower one
p[4] = 101;
Character array types and pointer types can also be mixed
#include <stdio.h>
int main()
{
char str[] = "hello";
char *str2 = "world";
char str3[10];
printf("input the value \n");
// Character array types and pointer types can also be mixed , below str3 It's an array of characters
scanf("%s", str3);
printf("str is %s\n", str);
printf("str2 is %s\n", str2);
printf("str3 is %s\n", str3);
}
The result of debugging the above code is as follows :
7、 A character array
#include <stdio.h>
int main()
{
char str[] = "hello";
char *str2 = "world";
char str3[10];
printf("input the value \n");
scanf("%s", str3);
printf("str is %s\n", str);
printf("str2 is %s\n", str2);
printf("str3 is %s\n", str3);
return 0;
}

边栏推荐
猜你喜欢
随机推荐
Webmagic+selenium+chromedriver+jdbc grabs data vertically.
Responsibility should be assigned to people, and Guangzhou should take multiple measures to build a "safety line" for children in summer
Take byte offer in four rounds and answer the interview questions
Greedy method, matroid and submodular function (refer)
There is another example of repeater
mysql学习笔记(1)——变量
Automatic testing of Web UI: Selenium syntax explanation is the most complete in the history
Basic concepts of Nacos and single machine deployment
Analysis of Eureka server
kettle学习——8.2版本的资源库配置变为灰色,且没有了Connect按钮
Role authorization --- complete the addition and deletion of secondary menus by adding and deleting primary menus
27、golang基础-互斥锁、读写锁
To create a MySQL data source resource group, you must choose to create a new exclusive data integration resource group? Or use a common resource group? thank you
Use fastjson JSON (simple and rough version)
[Luogu p3175] bitwise OR (min max inclusive) (high-dimensional prefix and / FWT)
WSN journal indexed by SCI
c语言:14、预处理
sql 时间处理(SQL SERVER\ORACLE)
The understanding of string in C.
win10小技巧(1)——转移桌面位置









