Memory in C

Source: Internet
Author: User

Because I want to learn IOS development and learn C language from the very beginning to better learn OC, this article mainly records the memory layout in C language and the features of different memory regions.

C language program memory layout, from high to low: Stack zone, heap zone, uninitialized data zone, initialized data zone, code zone.

I. Stack Zone


Automatically managed by the compiler, without manual control by the programmer. Stores function parameter values and local variable values. Stack content is allocated from high address to low address, and accessed from low address to high address.

    int a = 0;    int b = 0;    int array[5] = {1, 2, 3, 4, 5};        printf("&a......%p\n", &a);    printf("&b......%p\n", &b);    printf("array...%p\n", array);

Output:

    &a......0x7fff5fbff944    &b......0x7fff5fbff940    array...0x7fff5fbff920


As shown in the preceding program running result, the stack content is that the defined variables are stored in the stack area, and the Defined variables are placed below, the space size is determined by the type of the variable.

    printf("&array[0]...%p\n", &array[0]);    printf("&array[1]...%p\n", &array[1]);    printf("&array[2]...%p\n", &array[2]);    printf("&array[3]...%p\n", &array[3]);    printf("&array[4]...%p\n", &array[4]);    &array[0]...0x7fff5fbff920    &array[1]...0x7fff5fbff924    &array[2]...0x7fff5fbff928    &array[3]...0x7fff5fbff92c    &array[4]...0x7fff5fbff930

From the running result above, we can see that the address of each element in the array is getting bigger and bigger. For example, array [0] is the first element in the array, but the address is the smallest one.


We can also see that the last element address in the array is not connected to the address of B, in addition to array [4], the 4-byte space occupied by array [4] differs from B's address by 12 bytes, because when the system allocates memory, each time, the memory occupied by the element that occupies the largest memory in all variables will be opened up, and then allocated from high to low. If the memory space used this time is insufficient to store the next variable, the memory space of the largest element will be re-opened. For example, the three variables above will open up 20 bytes of space each time (int array [5] => 5*4), and a occupies 4, B occupies 4 and the remaining 12 cannot store array arrays. Therefore, a 20-byte memory space is opened up.


It is the data location of our program at runtime.




Related Article

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.