linux--Virtual Memory

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.