Memory space allocation for processes under Linux

Source: Internet
Author: User

This is mainly in the C language, for example, other languages developed programs, each process will have a similar space. Here is a C code:

#include <stdlib.h>#include<stdio.h>Doublet[0x02000000];voidsegments () {Static ints = the; void*p =malloc(2); printf ("stack\t%010p\nbrk\t%010p\nheap\t%010p\n"            "static\t%010p\nstatic\t%010p\ntext\t%010p\n",            &p, SBRK (0),p,t,&s,segments);}voidWritefreespace () {Char*p = SBRK (0) -1; *p =1; printf ("Assign to SBRK (0)-1 is succed! \ n"); P= SBRK (0) +1; *p =1; printf ("Assign to SBRK (0) +1 is succed! \ n");}intMainintargcChar*argv[])    {segments ();    Writefreespace (); Exit (0);}

This is mainly printed here: The address of the pointer p (stack), the position of the current break of the process (the bounds of the heap), the address (heap) that the pointer p points to, the address of the global variable T, the address of the local static variable, and the address of the function segments ().

After using GCC, the result of this code is as follows:

Stack    0xbfaa9edcbrk    0x18856000heap     0x18835008  Static    0x0804a060static    0x0804a024text      0x08048494Size of Heap:20ff8 Assign to SBRK (0)-1 is succed! Segmentation Fault

This is a good proof of the distribution in the relationship:

Among them, the SBRK () function may not be common. In general, it is not recommended to use SBRK () when applying programming, so we do not have the least. By man, sbrk (int) is used to increase the size of the heap, when it is fed parameter 0, it returns the heap boundary (SBRK (0) returns the address already in the heap (), can change the address in the writefreespace () attempt; As can be seen, the minimum heap value is 132k Bytes (20ff8+8=21000), the first 8 byte is reserved, the specific role needs to be understood again.

Memory space allocation for processes under Linux

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.