datastructure_c_malloc_memory_02

//继续挖坑

 

3. Memory Mapping Segment:

  • the kernel maps contents of files directly to memory.
  • Any application can ask for such a mapping via the Linux mmap() system call (implementation) or CreateFileMapping() / MapViewOfFile() in Windows.
  • Memory mapping is a convenient and high-performance way to do file I/O, so it is used for loading dynamic libraries.
  • It is also possible to create an anonymous memory mapping that does not correspond to any files, being used instead for program data. In Linux, if you request a large block of memory via malloc(), the C library will create such an anonymous mapping instead of using heap memory. ‘Large’ means larger than MMAP_THRESHOLD bytes, 128 kB by default and adjustable via mallopt().

4. Heap:

  • The heap provides runtime memory allocation, like the stack, meant for data that must outlive the function doing the allocation, unlike the stack.
  • Satisfying memory requests is thus a joint affair between the language runtime and the kernel. In C, the interface to heap allocation is malloc() and friends, whereas in a garbage-collected language like C# the interface is the new keyword.
  • If there is enough space in the heap to satisfy a memory request, it can be handled by the language runtime without kernel involvement. Otherwise the heap is enlarged via the brk()system call (implementation) to make room for the requested block.

5. Lowest Segments Of Memory:BSS, data, and program text

    • both bss and data store contents for static(global) variables in c.
    • The difference is that BSS stores the contents of uninitialized static variables, whose values are not set by the programmer in source code. The BSS memory area is anonymous: it does not map any file. If you say static int cntActiveUsers, the contents of cntActiveUsers live in the BSS.
    • The data segment, on the other hand, holds the contents for static variables initialized in source code. This memory area is not anonymous. It maps the part of the program’s binary image that contains the initial static values given in source code.
    • So if you say static int cntWorkerBees = 10, the contents of cntWorkerBees live in the data segment and start out as 10.
    • Even though the data segment maps a file, it is a private memory mapping, which means that updates to memory are not reflected in the underlying file. This must be the case, otherwise assignments to global variables would change your on-disk binary image.
    •  
      const char *gonzo="God's own prototype"

       

    • In that case, the contentsof pointer gonzo – a 4-byte memory address – live in the data segment. The actual string it points to does not, however. The string lives in the text segment, which is read-only and stores all of your code in addition to tidbits like string literals.
    • The text segment also maps your binary file in memory, but writes to this area earn your program a Segmentation Fault. This helps prevent pointer bugs, though not as effectively as avoiding C in the first place.

 

posted @ 2017-02-15 13:33  lixinnjupt  阅读(188)  评论(0编辑  收藏  举报