什么时候为变量或类对象保留内存? [英] When does the memory reserved for a variable or class object ?

查看:108
本文介绍了什么时候为变量或类对象保留内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我不知道内存实际上是什么时候为变量分配的。

几个问题如下:



1.分配给简单原始数据类型的内存何时说为int。

2.何时分配给a的meomory类对象。



我尝试了什么:



我搜索了net但意见不一致。

Hi all,

I don't know when does the memory is actually allocated for a variable .
Few questions as below :

1. when does the memory allocated to a simple primitive data type say int .
2. when does the meomory allocated to a class object .

What I have tried:

I searched on net but there are discrepancy of opinions .

推荐答案

内存分配实际上是一个很大的 C ++ 主题。您可以开始阅读教程,例如 C ++教程:内存分配 - 2018 [ ^ ],然后在一个好的 C ++ <上研究它/ code>预订。因为,作为 C ++ 开发人员,你必须掌握它。
Memory allocation is actually a big C++ topic. You could start reading a tutorial, see, for instance C++ Tutorial: Memory Allocation - 2018[^], and then study it on a good C++ book. Because, as C++ developer, you have to master it.


存在不同类型的存储区域:

There are different kinds of storage areas:
  1. 文字:包含可执行代码(此处为完整性而命名)

  2. 初始化数据:全局和静态变量

  3. 未初始化的数据:全局变量和静态变量(应用程序启动时初始化为零)

  4. 堆栈:局部变量

  5. 堆:使用 new() malloc()
    $分配存储空间b $ b

前两种内存是可执行文件本身的一部分,在将应用程序加载到内存时由操作系统分配。



单位化全局数据的内存也由操作系统分配并初始化为零。



堆栈内存由线程启动时由操作系统分配。对于主线程,大小由可执行文件确定。在运行时创建的其他线程将使用与默认值相同的大小,但可以将不同的大小作为参数传递给线程创建函数。



每次局部变量为创建后,将为其分配堆栈指针的地址,并按变量的大小递减(堆栈在自上而下的方向使用内存)。一旦堆栈指针到达底部,就会发生堆栈溢出,应用程序终止,因为堆栈内存在运行时不可调整大小。



编译器也可能决定存储本地CPU寄存器中的变量而不是堆栈。这也可以通过寄存器关键字提出。



堆运行内存是在运行时分配的。这是由编译器插入到每个应用程序(称为堆管理器)的代码完成的。当无法从当前堆内存中满足内存请求时,堆管理器将从操作系统请求更大的内存块。



通常有内存分配或基本类型和复杂类型的分配没有区别(除了编译器可能将基本类型存储在寄存器而不是堆栈中)。



一些例子:

Memory for the first two kinds is part of the executable itself and is allocated by the operating system when loading the application into memory.

The memory for unitialised global data is also allocated by the operating system and initialised to zero.

Stack memory is allocated by the operating system when a thread is started. For the main thread, the size is determined from the executable file. Other threads created during runtime will use the same size as default value but a different size can be passed as parameter to the thread creation function.

Each time a local variable is created, it will be assigned the address of the stack pointer and that is decremented by the size of the variable (stacks are using the memory in top down direction). Once the stack pointer reaches the bottom, a stack overflow occurs and the application terminates because the stack memory is not resizable during runtime.

A compiler may also decide to store local variables in CPU registers instead of the stack. This can be also proposed with the register keyword.

Heap memory is allocated in chunks during runtime. This is done by code inserted by the compiler to every application (called heap manager). When a request for memory can't be fulfilled from the current heap memory, the heap manager will request a larger block of memory from the operating system.

There is generally no difference between the memory allocation or assignemnt of basic and complex types (besides that the compiler might store basic types in registers rather than the stack).

Some examples:

// Initialised global data
int global1 = 1;
static int global2 = 2;

// Uninitialised global data (will be set to zero)
int global1U;
static int* globalPtr;

void some_func()
{
    // Initialised static data
    static int localS1 = 1;

    // Uninitialised static data
    static int localS2;

    // Local variables on the stack
    // The compiler might store some of them in CPU registers
    int local1;
    int local2 = 2;
    int localArr[10];
    SomeObj obj;

    // Local variable stored in CPU register
    // Deprecated with C++17 and the compiler may ignore it (use the stack instead)
    register r = 0;

    // Heap memory where the pointer is stored as local variable
    int *localPtr = new int[10];
    SomeObj *objPtr = new SomeObj();
    
    // Heap memory where the pointer is stored in a global variable
    globalPtr = new int[10];


}


在创建对象时分配成员的内存。一个很大的区别是指针,它们只是普通的数值。

The memory of member is allocated while object creation. A big difference are pointers, they are only plain numeric values.
Object obj;//memory of an Object instance
Object *p = 0;//only a pointer (long) allocated
p = new Object();//Object allocated and pointer set 



你可以通过步进来试试使用调试器进入代码。


You can try it by stepping into the code with the debugger.


这篇关于什么时候为变量或类对象保留内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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