linux-Memory Space Allocation __linux

Source: Internet
Author: User

A. Linux memory allocation

Linux memory allocation simple meaning is divided into four paragraphs

1. Code Area

2. Global variable Area

3. Stack

4. Heap

Verify by using the following program

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int add (int a,int b)
{ return
	a+b;
}
int a1=1;
static int a2=2;
const int a3=3;
int A4;

Main ()
{
	int b1=4;
	static b2=5;
	Const B3=6;
	
	int *p1=malloc (4);
	
	printf ("a1:%p\n", &a1);
	printf ("a2:%p\n", &a2);
	printf ("a3:%p\n", &a3);
	printf ("a4:%p\n", &a4);
	printf ("b1:%p\n", &b1);
	printf ("b2:%p\n", &b2);
	printf ("b3:%p\n", &b3);
	printf ("p1:%p\n", p1);
	printf ("main:%p\n", main);
	printf ("add:%p\n", add);
	
	printf ("%d\n", Getpid ());
	while (1);
}

Add:

Address is heap > Stack > Global area > Code area respectively


Run Effect chart

Discover A1 is going on the global, A2 is in the global area, A3 is in the code area (note that the const variable is in the code area)

A4 in the global area, B1 in the stack area, B2 in the global area, B3 in the stack area, p1 in the heap area, main and add in the code area

Sum up:

1. Normal global variables are allocated in the global region

2. Static regardless of local or global variables are in the global zone

3. Global const variable allocated in code area, local const variable allocated in stack area

4. malloc allocated in the heap

5. Code is assigned to code area two. Heap and stack validation on code

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

main ()
{
	int a1= Ten;
	int a2=20;
	int a3=30;
	
	int *p1=malloc (4);
	int *p2=malloc (4);
	int *p3=malloc (4);
	
	printf ("%p\n", &a1);
	printf ("%p\n", &a2);
	printf ("%p\n", &a3);
	printf ("%p\n", p1);
	printf ("%p\n", p2);
	printf ("%p\n", p3);

	printf ("%d\n", Getpid ());
	while (1);
}

The Operation effect chart is as follows:

Why are all int types, the stack occupies 4 bytes, and the heap occupies 16 bytes (ignoring byte alignment).

->malloc at the lower level is maintained through a linked list, presumably: malloc memory + Previous address pointer (4 byte) + one address pointer (4 byte) + This request memory size (4 byte)

So the init type malloc is 16byte.

And the stack is the pressure stack exists, one after another, so the above stack interval is 4byte (int type size)

To verify the above statement: do a malloc verification

The code is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

main ()
{
	
	int *p1= malloc (4);
	int *p2=malloc (4);
	int *p3=malloc (4);
	
	*p1=1;
	* (p1+1) =2;
	* (p1+2) =3;
	* (p1+3) =4;
	* (p1+4) =5;
	* (p1+5) =6;
	* (p1+6) =7;
	* (p1+7) =8;		
	printf ("%d\n", *P2);
	Free (p1);	
}

What will the P2 output? What will be the result of the operation?

The output of P2 is 5, because: 16 byte from P1 offset

* (p1+4) = 5; it's this.

But free will cause

GLIBC Detected * * * */MAIN:FREE (): Invalid next size (FAST): 0x099bc008 * * *

Free when the error, because we have the entire malloc linked list structure damage, resulting in free error

So the most taboo in the program is malloc after the Cross-border access situation


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.