以这种方式创建的数组 int a[5]
包含 5 个整数内存块,内存在运行时无法更改。
以这种方式创建的数组 int *ptr=new int[5]
也包含 5 个整数 block ,在这种情况下也无法在运行时增加和减少内存,因此,从这个角度来看它被称为动态内存。
最佳答案
口语术语“动态内存”来自语言定义术语“动态存储持续时间”。参见 Storage duration :
dynamic storage duration. The storage for the object is allocated and deallocated per request by using dynamic memory allocation functions. See new-expression for details on initialization of objects with this storage duration.
使用new
或new[]
创建并使用delete
或delete[]
销毁的对象具有动态存储持续时间。
它是动态的,因为生命周期在开发人员希望的时候开始和结束。每隔一个存储持续时间都有关于生命周期何时开始和何时结束的严格规则。它与数组大小或调整数组大小无关。非数组对象也可以具有动态存储持续时间。
关于c++ - 为什么我们将使用 "new"关键字创建的内存称为 "dynamic memory"因为它也是固定内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68834963/