From top to bottom, virtual memory is divided into Stack segments, data segments, code segments, and stack segments. The stack zone allocates memory from top to bottom, and the stack zone allocates memory from bottom to top. Data segments are divided into static and global regions. The two have different scopes. Code segments are divided into read-only and code areas. Finally, the BSS zone is not involved yet.
The six regions are defined as follows:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int number = 200; 5 6 int hello() 7 { 8 static int s = 400; 9 int n = 100;10 char *p = malloc(10);11 char *m = "hello world";12 printf("stack is %p\n", &n);13 printf("global is %p\n", &number);14 printf("static is %p\n", &s);15 printf("heap is %p\n", p);16 printf("read only is %p\n", m);17 }18 19 int main()20 {21 int i = 10;22 hello();23 }
In the past few days, basic C training is inseparable from the memory graph. This may be the biggest difference between studying uncle Tan's C language at school. Knowing the distribution of program code in the memory and drawing the memory graph of the program in operation is very helpful for the program's understanding, such as the function pointer mentioned today.
The function name represents the first address of the function. In this way, we can call the function through the function pointer. For example, if the function Hello () is defined, the function pointer is int (* FP) () = Hello. In this case, FP can be used to replace hello. Of course, function pointers can also be used as function parameters. We call a function with a function pointer in a parameter called a callback function, and a function pointer can be defined in a struct. The following functions call functions through function pointers, callback functions, and struct.
1 #include<stdio.h> 2 3 struct person{ 4 char *name; 5 int (*fp)(char *); 6 int (*fd)(struct person); 7 }; 8 9 int hello(char *s)10 {11 printf("my name is %s\n",s);12 }13 int pson(struct person jack)14 {15 printf("jack is name is %s\n",jack.name);16 17 }18 int main()19 {20 struct person tom;21 tom.name = "tom";22 tom.fp = hello;23 tom.fp(tom.name);24 struct person jack;25 jack.name = "jack";26 tom.fd = pson;27 tom.fd(jack);28 29 30 }
The callback function can also be called back. The following code is available:
1 #include<stdio.h> 2 3 int func(int i) 4 { 5 printf("i is %d\n",i); 6 } 7 int call(int (*fp)(int),int i) 8 { 9 fp(i);10 11 }12 int recall(int (*fd)(int (*)(int),int ),int (*fp)(int),int i)13 {14 fd(fp,i); 15 }16 17 int main()18 { 19 int (*fp)(int);20 fp = func;21 call(fp,10);22 int (*fd)( int (*)(int),int) = call;23 recall(fd,fp,20);24 }
The parameters of the callback function after the callback of 12 rows are confusing... However, the omnipotent memory diagram comes at this time. Draw a picture to solve the problem.
Old Liu is confused by function pointers. In the afternoon, I talked about basic Linux commands. Lsusb and lspci view the configuration of the device TFTP. These will not be written.
Daily: C-based memory allocation and function pointer