简体   繁体   English

内存分配运算符和表达式

[英]Memory allocation operators and expressions

In C++, is 'new' an operator or an expression or some kind of keyword? 在C ++中,'new'是一个运算符还是一个表达式或某种关键字? a similar question that comes in mind is, should i call '=' an operator or expression? 我想到一个类似的问题是,我应该将'='称为运算符或表达式吗?

C++ separates the notion of memory allocation and object lifetime. C ++分离了内存分配和对象生存期的概念。 This is a new feature compared to C, since in C an object was equivalent to its memory representation (which is called "POD" in C++). 这是与C相比的新特性,因为在C中,对象等同于其内存表示(在C ++中称为“POD”)。

An object begins its life when a constructor has completed, and its life ends when the destructor has completed. 一个对象在构造函数完成时开始它的生命,它的生命在析构函数完成时结束。 For an object of dynamic storage duration, the life cycle thus consists of four key milestones: 对于动态存储持续时间的对象,生命周期因此包括四个关键里程碑:

  1. Memory allocation. 内存分配。
  2. Object construction. 对象构造。
  3. Object destruction. 对象破坏。
  4. Memory deallocation. 内存释放。

The standard way in C++ to allocate memory dynamically is with the global ::operator new() , and deallocation with ::operator delete() . C ++中动态分配内存的标准方法是使用global ::operator new() ,并使用::operator delete()释放。 However, to construct an object there is only one method: A new expression : 但是,要构造一个对象,只有一个方法:一个新的表达式

T * p = new T;

This most common form of the new expression does allocation and construction in one step. 这种新表达式的最常见形式是一步完成分配和构造。 It is equivalent to the broken down version: 它相当于细分版本:

void * addr = ::operator new(sizeof(T));
T * p = new (addr) T;  // placement-new

Similarly, the delete expression delete p; 同样, 删除表达式 delete p; first calls the destructor and then releases memory. 首先调用析构函数然后释放内存。 It is equivalent to this: 它相当于:

p->~T();
::operator delete(addr);

Thus, the default new and delete expressions perform memory allocation and object construction in one wash. 因此, 默认的 new和delete表达式在一次清洗中执行内存分配和对象构造。 All other forms of the new expression, collectively called "placement new", call a corresponding placement-new operator to allocate memory before constructing the object. 新表达式的所有其他形式(统称为“placement new”)在构造对象之前调用相应的placement-new 运算符来分配内存。 However, there is no matching "placement delete expression", and all dynamic objects created with placement-new have to be destroyed manually with p->~T(); 但是,没有匹配的“放置删除表达式”,并且必须使用p->~T();手动销毁使用placement-new创建的所有动态对象p->~T(); .

In summary, it is very important to distinguish the new expression from the operator new . 总之,将新表达式operator new区分开来非常重要。 This is really at the heart of memory management in C++. 这实际上是C ++中内存管理的核心。

It's all of those. 这就是所有这些。

2.13 Table 4 explicitly lists new as a keyword. 2.13表4明确地将new列为关键字。

5.3.4 Introduces the new-expression . 5.3.4介绍new-expression This is an expression such as new int(5) which uses the new keyword, a type and an initial value. 这是一个表达式,例如new int(5) ,它使用new关键字,类型和初始值。

5.3.4/8 Then states that operator new is called to allocate memory for the object created by the new-expression 5.3.4 / 8然后声明调用operator new来为new-expression创建的对象分配内存

= works quite the same. =工作完全一样。 Each class has an operator= (unless explicitly deleted), which is used in assignment expressions. 每个类都有一个operator= (除非明确删除),它在赋值表达式中使用。 We usually call a=5; 我们通常称a=5; just an assignment, even when it's technically "an expression statement containing an assignment expression." 只是一个赋值,即使它在技术上是“包含赋值表达式的表达式语句”。

new is operator. 新的是运营商。 You can overload it and write your own version of it. 你可以重载它并编写自己的版本。 Also I think that = is operator. 另外我认为=是运营商。 Expression is more complex thing which consist of operators, variables, function calls etc. And please try to get C++ language standard. 表达式是更复杂的东西,包括运算符,变量,函数调用等。请尝试获得C ++语言标准。 It must describe all things you mentioned. 它必须描述你提到的所有事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM