Understanding of heap, stack, static storage area

Source: Internet
Author: User

Heap: Allocated by programmers themselves (with malloc and free, or new and delete), if we do not release manually, it will be released to the end of the program. If the allocated space is not freed when the allocation is blindly, then it may cause a memory leak, its capacity depends on the virtual memory, large.

#include <iostream> #include <cstdio> #include <windows.h> #include <cstdlib> #include < Cstring>using namespace Std;char * getmen (int num) {    char *p1 = NULL;    P1 = (char *) malloc (sizeof (char) * num);    if (P1 = = null)        return null;    return p1;} int main () {    char *tmp = NULL;    TMP = Getmen (ten);    if (tmp = = NULL)        return 0;    strcpy (tmp, "111222");    printf ("%s\n\n", TMP);    System ("pause");    return 0;}

The above program outputs "111222", which shows that the allocated memory space on the heap, if we do not recycle manually, even if the function ends are refactored, we can still use the space allocated in the function.


Stack: Automatically allocated by the compiler release, which is stored in the key function of the function of the next sentence code, function parameters and local variables, limited capacity, small.

#include <iostream> #include <cstdio> #include <windows.h> #include <cstdlib> #include < Cstring>using namespace Std;char * getmen (int num) {    char *p1 = NULL;    P1 = (char *) malloc (sizeof (char) * num);    if (P1 = = null)        return null;    return p1;} Char *getmen2 () {    char buf[64];  Temporary variable stack store    strcpy (buf, "123456789");    printf ("buf:%s", buf);    return BUF;} The stack function ends with the destructor int main () {    char *tmp = NULL;    TMP = Getmen (ten);    if (tmp = = NULL)        return 0;    strcpy (tmp, "111222");    TMP = GetMen2 ();    printf ("tmp:%s\n\n", TMP);    System ("pause");    return 0;}

In the function getMen2, the definition array is allocated to the stack area, and when the function finishes being refactored, the array of the stack is released, and return is not the 64 bytes in the memory block, but the first address of the memory block is returned, so the last output of TMP is an indeterminate factor.

By code it can be known that the heap and stack growth pattern is exactly the opposite, the heap growth pattern is upward by the low address to high address growth, while the stack is high address to low address growth. This allows each function to have a stack, which increases from a high address to a low address to avoid a stack overflow.


Static storage: Distributed by the compiler at compile time, released by the system, which holds global variables, static variables, and constants.

#include <cstdio> #include <iostream> #include <windows.h>using namespace Std;char * GETSTR1 () {    Char *p1 = "ABCDEFG2";    return p1;} char * GETSTR2 () {    char *p2 = "ABCDEFG2";    return p2;} int main () {    char *p1 = NULL;    char *p2 = NULL;    printf ("p1:%d, p2:%d \ n", p1, p2);    P1 = GetStr1 ();    P2 = getStr2 ();    Print the data that P1 P2 points to the memory space    printf ("p1:%s, p2:%s \ n", p1, p2);    P1 p2 data    printf ("p1:%d, p2:%d \ n", p1, p2);    System ("pause");    return 0;}

The "ABCDEFG2" defined in the global zone, the structure is like


In the case of variables defined in the global zone, if it is a variable, the system will not separate two spaces to store two identical, only the position of the data that the pointer points to this global zone

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Understanding of heap, stack, static storage 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.