The growth direction of the address of the stack area with the BUF address is two completely different concepts

Source: Internet
Author: User

One, the stack area address growth direction

To verify that the stack area is opening up or down, the operation of the first pressure variable A and pressure variable B, if the first address of a is larger than the first address of B to explain the opening downward, if the first address of B is larger than the first address of a, then the opening up, the following code to verify:

#include "stdio.h"
#include "stdlib.h"

int main () {
    int A;
    int b;
    printf ("&a:%d\n", &a);
    printf ("&b:%d\n", &b);
    System ("pause");
    return 0;
}

Debug output results:

Output results under release:

Can see two versions, A's first address is always larger than the first address of B, basically can determine the stack area openings downward, some people's release version may appear the first address of a is always smaller than the first address of B, in fact, this also does not matter.

stack area opening downward is mainly convenient to avoid stack overflow problem, define a starting maximum value, and then open the stack downward, until 0 can not press stack.

Second, the address of the heap area growth direction

#include "stdio.h"
#include "stdlib.h"

int main () {
    int *a= (int*) malloc;
    int *b = (int*) malloc (a);
    printf ("A:%d\n", a);
    printf ("B:%d\n", b);
    System ("pause");
    return 0;
}

Output results:

You can see that the heap area is open up.

Third, the growth direction of BUF address

Regardless of the opening direction of the stack area, the growth direction of the BUF address in the stack space is upward, if the growth direction downward, to BUF first address + 1 o'clock will not run to other memory space.

Third, summary

1, the stack area openings downward (general situation);

2, the heap area openings upward;

3, the address growth direction of the stack area and the growth direction of the BUF address are two completely different concepts.

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.