什么是存储在堆,什么是存储在堆栈? [英] What is stored on heap and what is stored on stack?

查看:133
本文介绍了什么是存储在堆,什么是存储在堆栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释清楚,在C,C ++和Java的条款。
那张叠什么都,什么都去堆,并分配完成后。

Can anyone clearly explain, in terms of C,C++ and Java. What all goes on stack and what all goes on Heap and when is the allocation done.

据我所知,

所有的局部变量是否原语,指针或引用变量每次函数调用是一个新的堆栈帧。

All local variables whether primitives,pointers or reference variables per function call are on a new stack frame.

和新的或创建的malloc任何事情都会发生堆。

and anything created with new or malloc goes on heap.

我感到困惑的一些事情。

I am confused about few things.

是引用/图元这些都对堆上创建的对象的成员也存储在堆中?

Are references/primitives which are members of an object created on heap also stored on heap ?

和有关在每个帧递归创建的方法的那些本地成员什么。
他们都是在栈上,如果是,则在运行时分配的堆栈内存?
也为文字,是他们的code段的一部分吗?
什么关于使用C全局变量,在C ++ / Java静态和静态的温度。

and what about those local members of a method that are being recursively created in each frame. Are they all on stack, If yes then is that stack memory allocated at runtime ? also for literals, are they part of the code segment ? and what about globals in C, static in C++/Java and static in C .

推荐答案

内存中的程序的结构

以下是任何程序的基本结构在存储器中加载时

The following is the basic structure of any program when loaded in the memory.

 +--------------------------+
 |                          |
 |      command line        |
 |        arguments         |
 |    (argc and argv[])     |
 |                          |
 +--------------------------+
 | Stack                    |
 | (grows-downwards)        |
 |                          |
 |                          |
 |                          |
 |         F R E E          |
 |        S P A C E         |
 |                          |
 |                          |
 |                          |
 |                          |
 |     (grows upwards) Heap |
 +--------------------------+
 |                          |
 |    Initialized data      |
 |         segment          |
 |                          |
 +--------------------------+
 |                          |
 |     Initialized to       |
 |        Zero (BSS)        |
 |                          |
 +--------------------------+
 |                          |
 |      Program Code        |
 |                          |
 +--------------------------+

几点要注意:


  • 数据段

    • 初始化数据段(由程序员初始化明确初始化)

    • 未初始化的数据段(已初始化为零的数据段 - BSS [块开始符号])

    数据段

    数据段包含了明确包含intialized值用户初始化的全局和静态数据。

    The data segment contains the global and static data that are explicitly initialized by the users containing the intialized values.

    数据段的其他部分被称为BSS(因为旧的IBM系统的有这样的段初始化为零)。它是存储器中的部分,其中在OS初始化存储器块到零。这是未初始化的全局数据和静态get默认值为零如何。这个区域是固定的,并具有静态大小

    The other part of data segment is called BSS (because of the old IBM systems had that segment initialized to zero). It is the part of memory where the OS initializes the memory block to zeros. That is how the uninitialized global data and static get default value as zero. This area is fixed and has static size.

    的数据区被分离成基于显式初始化两个区域,因为要被初始化的变量可以初始化一个接一个。然而,未初始化的变量不必明确初始化为0的一个接一个。替代的是,初始化变量的作业是留给操作系统。此散装初始化可以大大减少加载的可执行文件所需的时间。

    The data area is separated into two areas based on explicit initialization because the variables that are to be initialized can be initialized one-by-one. However, the variables that are not initialized need not be explicitly initialized with 0's one-by-one. Instead of that, the job of initializing the variable is left to the OS. This bulk initialization can greatly reduce the time required to load the executable file.

    晴数据段的布局是底层操作系统的控制,还是有些装载机给部分控制权交给用户。这个信息可以是在应用中有用,例如嵌入式系统。

    Mostly the layout of the data segment is in the control of the underlying OS, still some loaders give partial control to the users. This information may be useful in applications such as embedded systems.

    这方面可以得到解决,并使用指针从code访问。自动变量的开销,在初始化每次都需要和code,需要做初始化时间的变量。然而,因为初始化只进行一次,而且也在加载时在数据区中的变量不具有这样的运行时的过载。

    This area can be addressed and accessed using pointers from the code. Auto variables have overhead in initializing the variables each time they are required and code is required to do that initialization. However, the variables in the data area does not have such runtime overload because the initialization is done only once and that too at loading time.

    code段

    程序code是code区里的可执行文件code是可供执行。这个区域也是固定的大小。这只能被访问是函数指针,而不是由其它数据指针。这里要注意的另一个重要信息是,该系统可能会考虑这个区域作为只读存储器区域,任何试图在这一领域写入导致不确定的行为。

    The program code is the code area where the executable code is available for execution. This area is also of fixed size. This can be accessed only be function pointers and not by other data pointers. Another important information to note here is that the system may consider this area as read only memory area and any attempt to write in this area leads to undefined behavior.

    常量字串可以以code或数据区被放置,并且取决于实现。

    Constant strings may be placed either in code or data area and that depends on the implementation.

    写code区的作法会导致不确定的行为。例如(我打算只给 C 基于例子)以下的code可能导致运行错误,甚至导致系统崩溃。

    The attempt to write to code area leads to undefined behavior. For example (I'm going to give only C based examples) the following code may result in runtime error or even crash the system.

    int main()
    {
        static int i;
        strcpy((char *)main,"something");
        printf("%s",main);
        if(i++==0)
        main();
    }
    

    栈和堆区

    有关执行程序使用两个主要部分,栈和堆。堆栈帧在堆栈功能和堆动态内存分配创建。栈和堆是未初始化的区域。因此,无论发生在那里在存储器成为在该空间中创建的对象的初始(垃圾)值。

    For execution, the program uses two major parts, the stack and heap. Stack frames are created in stack for functions and heap for dynamic memory allocation. The stack and heap are uninitialized areas. Therefore, whatever happens to be there in the memory becomes the initial (garbage) value for the objects created in that space.

    让我们看一个示例程序,显示获取存储哪些变量,其中,

    Lets look at a sample program to show which variables get stored where,

    int initToZero1;
    static float initToZero2;
    FILE * initToZero3; 
    // all are stored in initialized to zero segment(BSS)
    
    double intitialized1 = 20.0;
    // stored in initialized data segment
    
    int main()
    {
        size_t (*fp)(const char *) = strlen;
        // fp is an auto variable that is allocated in stack
        // but it points to code area where code of strlen() is stored
    
        char *dynamic = (char *)malloc(100);
        // dynamic memory allocation, done in heap
    
        int stringLength;
        // this is an auto variable that is allocated in stack
    
        static int initToZero4; 
        // stored in BSS
    
        static int initialized2 = 10; 
        // stored in initialized data segment   
    
        strcpy(dynamic,"something");    
        // function call, uses stack
    
        stringLength = fp(dynamic); 
        // again a function call 
    }
    

    或者考虑一个更复杂的例子,

    Or consider a still more complex example,

    // command line arguments may be stored in a separate area  
    int main(int numOfArgs, char *arguments[])
    { 
        static int i;   
        // stored in BSS 
    
        int (*fp)(int,char **) = main;  
        // points to code segment 
    
        static char *str[] = {"thisFileName","arg1", "arg2",0};
        // stored in initialized data segment
    
        while(*arguments)
            printf("\n %s",*arguments++);
    
        if(!i++)
            fp(3,str);
    }
    

    希望这有助于!

    这篇关于什么是存储在堆,什么是存储在堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆