The question is raised
pro1.c
#include <stdio.h> #include <stdlib.h>main () {int *a=malloc (4); *a=9999;//* (a+1) =1000;//* (a+1000) = 10000; printf ("%p\n", a); while (1);}
GCC pro1.c-omain1 run main1 results: 0x8a01008
Pro2.c
#include <stdio.h> #include <stdlib.h>main () {int *a= (int*) 0x8a01008;//logical address does not have a corresponding physical address to establish association is meaningful This process is memory-mapped printf ("%d\n", *a); while (1);}
Guess will not print 9999
GCC pro2.c-omain2 Run main2 result: segment error
Reason:
A program cannot access the space that the address of another program points to.
Understand:
1. Start address of each program 0x80084000
2. The address used in the program is not a physical address, but a logical address (virtual memory).
The logical address is just a number. The number is represented by an int 4-byte integer.
4294967296=4g
Each program provides 4G of access
Problem:
It makes sense to associate a logical address with a physical address: a process is called a memory map.
Conclusion:
The virtual address and physical address map have a basic unit: at least 4K is mapped
4k 1000 memory page.
Segment Error: Invalid access. That memory is not mapped.
Non-legal access: for example, a space outside of malloc's allotted space can be accessed (without a fragment error), but access is illegal. Because of cross-border access
There are two kinds of memory accesses: one is accessible, but not necessarily legal, such as malloc a few bytes,
Memory will give you a map of 4K space, int* p=malloc (0); * (p+1000) = 9999; theory says it's accessible.
linux--Virtual Memory