C + + stack growth direction

Source: Internet
Author: User

Stack Area: Staging Area
#include <iostream>using namespacestd; #include<stdio.h>intMain () {intA= -; intb=Ten; cout<<"/********************************/"<<Endl; printf ("%d\n",&a); printf ("%d\n",&b); if(&a>&b) {cout<<" Down"<<Endl; }Else{cout<<" up"<<Endl; }    intc[Ten]; cout<<"/********************************/"<<Endl;  for(intI=0;i<Ten; i++) {C[i]=i; } cout<<"/********************************/"<<Endl;  for(intj=0;j<Ten; j + +) {printf ("%d\n",&C[j]); } cout<<"/********************************/"<<Endl; float*p=NULL; Double*q=NULL; printf ("%d\n", p); printf ("%d\n", q); printf ("%d\n",&p); printf ("%d\n",&q); cout<<"/********************************/"<<Endl; return 0;}

Conclusion: &a>&b. Define the variable a first, and then define the variable B. Variable A variable b is in the staging area. So the stack grows downward. For the array address as the subscript grows larger, this is due to the growth direction of the stack and memory space buf storage direction is two different concepts.

Heap Area:
#include <iostream>using namespacestd; #include<stdio.h>#include<stdlib.h>intMain () {Char*p=NULL; Char*q=NULL; P=(Char*)malloc( -*sizeof(Char)); Q=(Char*)malloc( -*sizeof(Char)); printf ("\n%d\n",sizeof(Char)); printf ("%d\n",&p); printf ("%d\n",&q); printf ("\np[0]:%d", &p[0]); printf ("\np[1]:%d", &p[1]); printf ("\nq[0]:%d", &q[0]); printf ("\nq[1]:%d", &q[1]); if(p!=NULL) {         Free(P); }    if(q!=NULL) {         Free(q); }    return 0;}

Conclusion: the P pointer and malloc area are defined first, and the Q pointer and malloc area are defined. In the heap area P[0] larger than q[0]. and p[1] larger than p[0]. It is known that the heap grows upward.

C + + stack growth direction

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.