C Memory Distribution

Source: Internet
Author: User


First look at the test code:

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

int g_val;
int g_ival = 5;

char *g_ptr;
Char *g_iptr = "Hongchunhua";

static int s_val;
static int s_ival = 5;

int Print_add (int val)
{
	int A;
	int b = 1;
	static int C;
	
	char *p = NULL;
	p = (char *) malloc (1);
	
	printf ("Address g_iptr=\t0x%x.\n", g_iptr);
	printf ("Address print_add=\t0x%x.\n", &print_add);
	printf ("Address g_ival=\t0x%x.\n", &g_ival);
	printf ("Address g_iptr=\t0x%x.\n", &g_iptr);
	printf ("Address s_ival=\t0x%x.\n", &s_ival);
	printf ("Address g_val=\t0x%x.\n", &g_val);
	printf ("Address g_ptr=\t0x%x.\n", &g_ptr);
	printf ("Address s_val=\t0x%x.\n", &s_val);
	printf ("Address c=\t0x%x.\n", &c);
	printf ("Address p=\t0x%x.\n", &p);
	printf ("Address val=\t0x%x.\n", &val);
	printf ("Address a=\t0x%x.\n", &a);
	printf ("Address b=\t0x%x.\n", &b);	
	
	Free (p);
	p = NULL;
	return 0;
}

int main ()
{return
	print_add (2);
}


Output results:

Address g_iptr= 0x8048620.
Address print_add= 0x80483e4.
Address g_ival= 0x8049834.
Address g_iptr= 0x8049838.
Address s_ival= 0x804983c.
Address g_val= 0x8049850.
Address g_ptr= 0x8049854.
Address s_val= 0x804984c.
Address c= 0x8049848.
Address p= 0xbfb5d49c.
Address val= 0xbfb5d4b0.
Address a= 0xbfb5d4a4.
Address b= 0xbfb5d4a0.


Largely from small to large output, in general we know 0-3g for user space, the remaining 1G for kernel space.

From small to large in sequence for

Code area Print_add and "Hongchunhua" this string.

Global already initialized area includes G_ival, S_ival

Global uninitialized zone, including G_val, G_ptr, C,s_val

Pile, that's p.

Stacks, including Val,a and B.


In fact, in the heap and the middle of the stack there is a region, called the mapping area, mmap use this area.


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.