C++ Placement语法解析[译]

8 placement new的使用 第三种形式的operator new就是我们介绍的placement new,它不分配内存空间,而是借用ptr指针所指向的内存空间调用构造函数构造一个对象,最后返回ptr指针。需要额外注意的是,如果使用placement new时构造的是一个类对象,则释放指针前,需要手动调用类对象的析构函数。回到文章开始提到的第二行语句,其作用为:取整型变量a的地址作为ptr,在整型变量a指向的内存空间构造一个新的整型数据88。阅读完上面的内容后,想必聪明的你已经弄懂了placement new的相关知识。 阅读详情

Placement syntax

From Wikipedia, the free encyclopedia

  (Redirected from Placement new)

In the C++ programming language, placement syntax allows programmers to explicitly specify the memory management of individual objects — i.e. their "placement" in memory. The "placement" versions of the new and delete operators and functions are known as placement newand placement delete.[1]

在C++编程语言中,placement语法使得程序员能够明确地指定单个对象的内存管理,比如,他们在内存中的“位置”。new, delete操作符以及函数的”placement”版本被称为placement new和placement delete.

A new expression, placement or otherwise, calls a new function, also known as an allocator function, whose name is operator new. Similarly, a delete expression calls a delete function, also known as a deallocator function, whose name is operator delete.[2][3]

一个new表达式,placement或者其他叫做new 函数,也被称为内存分配函数,或者new操作符。同样的,一个delete表达式叫做delete函数,也被称为内存释放函数,或者delete操作符。

Any new expression that uses the placement syntax is a placement newexpression, and any operator new or operator delete function that takes more than the mandatory first parameter ( size_t and void *, respectively) is a placement new or placement delete function.[4]

一个使用了placement语法的new表达式叫做一个placement new 表达式,使用多个参数的new 操作符或者delete操作符是一个placement new或者placement delete 函数。

Contents

[hide]

·             1 Expressions

o                1.1 Compiler quirks

·             2 Functions

·             3 Use

o                3.1 Default placement

o                3.2 Preventing exceptions

o                3.3 Custom allocators

o                3.4 Debugging

·             4 Placement delete

·             5 References

·             6 Further reading

 [edit] Expressions表达式

The Standard C++ syntax for a non-placement new expression is[2]

用于非placement new 表达式的标准C++语法是:

new new-type-id ( optional-initializer-expression-list )

The placement syntax adds an expression list immediately after the newkeyword. This expression list is the placement. It can contain any number of expressions.[2][3][5]

placement 语法在new关键字添加表达式列表,这个表达式列表就是placement.它可以包含任何数目的表达式。

new ( expression-list ) new-type-id ( optional-initializer-expression-list )

There is no placement delete expression.[6]

没有placement delete 表达式。

[edit] Compiler quirks 编译器相关

Versions of the g++ compiler prior to version 2.3.1 accept a different syntax for placement new expressions:[7]

g++ 2.3.1版本以前的编译器可以使用一种不同的placement new 表达式语法:

new { expression-list } new-type-id ( optional-initializer-expression-list )

Up until version 2.2.2, this was the only placement syntax that that compiler would accept.[7]

直到2.2.2版本,

new ( expression-list ) new-type-id ( optional-initializer-expression-list )

才是编译器唯一可以接受的Placement new 表达式语法。

[edit] Functions 函数

The placement new functions are overloads of the non-placement new functions. The declaration of the non-placement new functions, for non-array and array new expressions respectively, are:[8][9]

Placement new 函数是非placement new函数的重载函数。Non-placement针对非数组和数组的new表达式分别是:

void * operator new (size_t)throw(std::bad_alloc);
void * operator new[] (size_t)throw(std::bad_alloc);

The Standard C++ library provides two placement overloads each for these functions. Their declarations are:[8][9]

标准的C++库对这些上述函数分别提供两个对应的placement重载函数:

void * operator new (size_t, conststd::nothrow_t &) throw();
void * operator new (size_t, void *) throw();
void * operator new[] (size_t, conststd::nothrow_t &) throw();
void * operator new[] (size_t, void *)throw();

In all of the overloads, the first parameter to the operator new function is of type size_t, which when the function is called will be passed as an argument the amount of memory, in bytes, to allocate. All of the functions must return type void *, which is a pointer to the storage that the function allocates.[2]

在所有的重载函数中,new操作符函数中的第一个参数的类型是size_t, 在函数调用的时候,将作为申请内存数目的参数进行传递,申请内存大小以byte为单位进行分配。所有的函数必须返回一个void*类型的指针,用于指向分配获得的内存空间。

 

There are also placement delete functions. They are overloaded versions of the non-placement delete functions. The non-placement delete functions are declared as:[8][9]

同样也有placement delete 函数, 他们是non-placement delete函数的重载版本,非placement delete 函数的delete函数申请为:

void operator delete (void *) throw();
void operator delete[] (void *) throw();

 

The Standard C++ library provides two placement overloads each for these functions. Their declarations are:[8][9]

标准C++库同样也提供了针对上述两个函数的重载版本,它们被声明如下形式:

 

void operator delete (void *, conststd::nothrow_t &) throw();
void operator delete (void *, void *) throw();
void operator delete[] (void *, conststd::nothrow_t &) throw();
void operator delete[] (void *, void *)throw();

In all of the overloads, the first parameter to the operator delete function is of type void *, which is the address of the storage to deallocate.[2]

在所有的重载函数中,delete操作符函数第一个参数的类型是void*,用于指定释放内存的地址。

 

For both the new and the delete functions, the functions are global, are not in any namespace, and do not have static linkage.[2]

对于new和delete函数,它们是全局的,不在任意一个命名空间,也没有静态链接。

[edit] Use 使用

Placement syntax has four main uses: default placement, preventing exceptions, custom allocators, and debugging.

Placement语法有四个主要的用法:默认placement, 防止异常,自定义分配子,以及调试。

[edit] Default placement 默认placement

The placement overloads of operator new and operator delete that employ an additional void * parameter are used for default placement, also known as pointer placement. Their definitions by the Standard C++ library, which it is not permitted for a C++ program to replace or override, are:[8][9][10]

应用一个额外的void*参数的new操作符和delete操作符的placement 重载参数用于默认placement,也叫做指针placement. 它们是根据标准C++库进行的定义,不允许C++程序替换或者覆盖:

void * operator new (size_t, void * p) throw(){ return p ; }
void * operator new[] (size_t, void * p)throw() { return p ; }
void operator delete (void *, void *) throw(){ }
void operator delete[] (void *, void *)throw() { }

Default placement does not require the inclusion of the Standard C++ library header <new> in the source code of a C++ program.[2]

默认的placement不要求在C++程序中包含标准C++库头文件<new>

There are various uses for default placement.

默认placement有有多的应用。

Bjarne Stroustrup originally observed, in his book The Design and Evolution of C++, that pointer placement new is necessary for hardware that expects a certain object at a specific hardware address. It is also required for the construction of objects that need to reside in a certain memory area, such as an area that is shared between several processors of a multiprocessor computer.[11]

Bjarne Stroustrup 在他的“C++的设计及演化”一书中提到,指针placement new操作符对于那些在特定硬件地址上期许某个对象的硬件是必须的。它同时也要求创建需要存在在某个内存区域中的对象,如多处理器计算机中几个处理器共享的一个区域。

Other uses, however, include calling a constructor directly, something which the C++ language does not otherwise permit.[3]

其他的使用包含直接调用构造函数,和其他一些C++语言不支持的不同方式。

The C++ language does allow a program to call a destructor directly, and, since it is not possible to destroy the object using a delete expression, that is how one destroys an object that was constructed via a pointer placement new expression. For example:[12][6]

C++语言确实允许程序直接调用析构函数,同时由于不可能使用delete表达式销毁对象(这是销毁通过指针placement new表达式建立起来的对象的一种方式)如:

p->~T() ;

[edit] Preventing exceptions 防止异常

Normally, the (non-placement) new functions throw an exception, of typestd::bad_alloc, if they encounter an error, such as exhaustion of all available memory. This was not how the functions were defined by Stroustrup's Annotated C++ Reference Manual, but was a change made by the standardization committee when the C++ language was standardized. The original behaviour of the functions, which was to return aNULL pointer when an error occurred, is accessible via placement syntax.[3][5][4]

正常情况下,非placement new 函数在碰到一个错误(如可用内存的耗尽)扔出一个类型为std::bad_alloc异常。这个函数的定义和Stroustrup的“C++注解”一书中所做的定义是不同的,而是C++语言标准化组织所做的更改。原来函数碰到错误的时候,就会返回一个NULL指针。

 

Programmers that wish to do this in their programs must include the Standard C++ library header <new> in the source code. This header declares the global std::nothrow object, which is of type std::nothrow_t(also declared in the header), which is used to call the overloaded new functions that are declared as taking const nothrow_t & as their second parameter. For example:[10]

程序员要想在自己的程序中使用这种功能的话,就必须在程序的源文件中包含<new>头文件。这个头文件声明了全局std::nothrow对象,其类型为std::nothrow_t(同样也在头文件中被声明)。这个对象用于调用使用const notrhow_t& 作为其第二个参数的new重载函数,如:

#include <new>

struct T {} ;

int main ()
{
    
// Call the function operator new(size_t, const nothrow_t &) and (if successful) construct the object.
    T * p = new (std::nothrow) T ;
    
if (p) {
        
// The storage has been allocated and the constructor called.
        delete p ;
    
} else
        ; // An error has occurred.  No storage has been allocated and no object constructed.
    return 0 ;
}

[edit] Custom allocators  定制分配子

Placement syntax is also employed for custom allocators. This does not use any of the allocator and deallocator functions from the Standard C++ library header <new>, but requires that programmers write their own allocation and deallocation functions, overloaded for user-defined types. For example, one could define a memory management class as follows:[9][8]

placement 语法也能用于定制分配子。它并不使用标准C++库头文件<new>中的任何分配子和解析子,而是需要程序员编写自己的分配和解析函数,重载用户自定义的类型,如可以定义如下的内存管理类:

#include <cstdlib>
class A {
public:
    
void * allocate ( size_t ) ;
    
void deallocate ( void * ) ;
} ;

And define custom placement allocation and deallocation functions as follows:[9][8]

自定义placement 分配和析构函数如下:

void *
operator 
new (size_t size, A & arena)
{
    
return arena.allocate(size) ;
}
void
operator delete (void * p, A & arena)
{
    arena.
deallocate(p) ;
}

The program would employ the placement syntax to allocate objects using different instances of the A class as follows:[9][8]

程序会使用如下类型A的不同实例通过placement语法来分配对象:

A first_arena, second_arena ;
T * p1 = 
new (first_arena) T ;
T * p2 = 
new (second_arena) T ;

Destroying an object whose storage is allocated in such a fashion requires some care. Because there is no placement delete expression, one cannot use it to invoke the custom deallocator. One must either write a destruction function that invokes the custom deallocator, or call the placement delete function directly, as a function call.[6][9][8]

销毁一个通过如上方式分配内存的对象需要特别注意。由于不存在用户自定义的delete表达式,所以就无法调用用户自定义的析构子。解决方案就是要么在析构函数中调用自定义的析构子,要么就直接调用placement delete函数。

The former would resemble:[9]

和上面的代码相似

void
destroy (T * p, A & arena)
{
    p->~T
() ;   // First invoke the destructor explicitly.
    arena.deallocate(p) ;  // Then call the deallocator function directly.
}

which would be invoked from a program as:

它可以在函数中通过如下方式被调用:

A arena ;
T * p = 
new (arena) T ;
/* ... */
destroy(p, arena) ;

The latter would involve simply writing the destructor invocation and delete function call into the program:[8][13]

前者(原文是后者,笔者认为这里应该是前者)简化了编写析构函数调用代码和delete函数。

A arena ;
T * p = 
new (arena) T ;
/* ... */
p->~T() ;    // First invoke the destructor explicitly.
operator delete(p, arena) ;   // Then call the deallocator function indirectly via operator delete(void *, A &) .

A common error is to attempt to use a delete expression to delete the object. This results in the wrong operator delete function being called. Dewhurst recommends two strategies for avoiding this error. The first is to ensure that any custom allocators rely upon the Standard C++ library's global, non-placement, operator new, and are thus nothing more than simple wrappers around the C++ library's memory management. The second is to create new and delete functions for individual classes, and customize memory management via class function members rather than by using the placement syntax.[13]

通常出现的错误是试着使用delete表达式来删除对象。它会导致调用了错误的delete 操作符函数。Dewhurst推荐了两种规避这种错误的策略,第一个是,保证任何自定义分配子依赖于标准C++库的全局,非placement,new操作符,这样的话,就能保证使用C++库内存管理的简单的容器。第二种方法是为每个独立的类创建和new和delete函数,并且通过类中的成员函数而非使用placement语法来自定义内存管理。

[edit] Debugging 调试

Placement new can also be used as a simple debugging tool, to enable programs to print the filename and line number of the source code where a memory allocation has failed. This does not require the inclusion of the Standard C++ library header <new>, but does require the inclusion of a header that declares four placement functions and a macro replacement for the new keyword that is used in new expressions. For example, such a header would contain:[10][14]

placement new 能用于简单的调试工具,以便输出应用程序内存分配出错的源文件的文件名以及源代码所在的行数。这无需包含标准C++库头<new>, 但是却必须包含一个声明了四个placement 函数以及用于替换new的一个宏的头文件,如:

#if defined(DEBUG_NEW)
void * operator new (size_t size, const char*file, int line);
void * operator new[] (size_t size, constchar* file, int line);
void operator delete (void * p, const char*file, int line);
void operator delete[] (void * p, const char*file, int line);
#define New new(__FILE__, __LINE__)
#else
#define New new
#endif

This would be employed in a program as follows:[10][14]、

在程序中,可以这样用:

T * p = New T ;

The custom-written placement new functions would then handle using the supplied file and line number information in the event of an exception. For example:[10][14]

用户自定义的placement new函数在发生异常的时候,就会使用提供的文件名和行数来处理异常事件。如:

#include <new>
#include <cstdlib>

class NewError {
public:
    NewError
(const char * file, int line) { /* ... */ }
    
/* ... */
} ;

void *
operator 
new (size_t size, const char* file,int line)
{
    
if (void * p = ::operator new (size, std::nothrow))
        
return p ;
    throw NewError
(file, line) ;
}

[edit] Placement delete

As noted above, there is no placement delete expression. It is not possible to call any placement operator delete function using a deleteexpression.[6][15]

如上所述,不存在placement delete expression. 无法通过使用delete表达式来调用任何placement delete 操作符函数。

The placement delete functions are called from placement newexpressions. In particular, they are called if the constructor of the object throws an exception. In such a circumstance, in order to ensure that the program does not incur a memory leak, the placement delete functions are called. A placement new expression first calls the placement operator newfunction, then calls the constructor of the object upon the raw storage returned from the allocator function. If the constructor throws an exception, it is necessary to deallocate that storage before propagating the exception back to the code that executed the placement new expression, and that is the purpose of the placement delete functions.[6][2][4][15]

placement delete函数由placement new 表达式进行调用。尤其是,如果对象的创建扔出了一个异常的话,它们就会被调用以释放分配的内存。在这种情况下,为了使得应用程序不至于引起一个内存泄漏,就会调用placement delete函数。Placement new 表达式首先调用placement new操作符函数,然后在分配函数中返回的初始内存上调用对象的构造函数。如果对象的构造函数抛出了一个异常,在异常蔓延到执行placement new表达式的代码时,构造函数有必要抛出一个异常,这也就是placement delete函数的意义所在。

The placement delete function that is called matches the placement new function that was invoked by the placement new expression. So, for example, if the following code is executed, the placement delete function that is called will be operator delete(void *, const A &):[6][2][15]

调用的placement delete函数和由placement new expression调用的placement new 函数必须匹配。所以,如下的代码,调用的placement delete函数将是operator delete(void* , const A&)

#include <cstdlib>

struct A {} ;
struct E {} ;

class T {
public:
    T
() { throw E() ; }
} ;

void * operator new ( size_t, const A & ) ;
void operator delete ( void *, const A & ) ;

int main ()
{
    A a ;
    T * p = 
new (a) T ;
    
return 0 ;
}

This is why the pointer placement delete functions are defined as no-operations by the Standard C++ library. Since the pointer placement new functions do not allocate any storage, there is no storage to be deallocated in the event of the object's constructor throwing an exception.[6]

这也是为什么指针placement delete函数被标准C++库定义为无操作,因为指针placement new函数无需分配任何空间,在对象构造函数扔出异常的时候也就没有内存可以释放了。

If no matching placement delete function exists, no deallocation function is called in the event of an exception being thrown by a constructor within a placement new expression. There are also some (older) C++ implementations that do not support placement delete (which, like the exception-throwing allocator functions, were an addition made to C++ when it was standardized) at all. In both such situations, an exception being thrown by a constructor when allocating using a custom allocator will result in a memory leak. (In the case of the older C++ implementations, a memory leak will also occur with non-placement new expressions.)[4][15]

如果不存在匹配的placement delete函数,在placement new表达式中调用的构造函数引发异常时将不会调用析构函数。还有一些(老的)C++ 实现,他们根本不支持placement delete(就像一场处理分配函数函数一样,是在C++标准化以后才添加上去的)。在以上两种情况下,在通过使用自定义分配子函数分配内存空间,调用构造函数时发生的异常将会导致内存泄漏。(在老式C++实现中,内存泄漏在非-placement new 表达式中也会发生。)

[edit] References

  1. ^ "Placement New/Delete". C++ Language and Library. Glen McCluskey & Associates LLC (2000-06-26). Retrieved on 2008-11-26.
  2. ^ a b c d e f g h i Ray Lischner (2003). C++ in a Nutshell. O'Reilly. pp. 72–73,128–129,310,623–625. ISBN 059600298X.
  3. ^ a b c d Stanley B. Lippman (1997). C++ Gems. Cambridge University Press. pp. 386–389. ISBN 0135705819.
  4. ^ a b c d Scott Meyers (1998-04-01). "Placement new and placement delete", Dr. Dobb's Journal, United Business Media LLC.
  5. ^ a b Kyle Loudon (2003). C++ Pocket Reference. O'Reilly. pp. 109–110. ISBN 0596004966.
  6. ^ a b c d e f g Nicholas Solter and Scott Kleper (2005). Professional C++. Wiley. pp. 458–461. ISBN 0764574841.
  7. ^ a b Joe Buck (1997-05-12). "3.4. g++ won't accept the placement new syntax.". Frequently asked questions about the GNU C++ compiler. Retrieved on 2008-11-26.
  8. ^ a b c d e f g h i j Dirk Vermeir (2001). Multi-paradigm Programming Using C++. Springer. pp. 113–115. ISBN 1852334835.
  9. ^ a b c d e f g h i j Stroustrup, Bjarne (1997). The C++ Programming Language (Third Edition ed.). Addison-Wesley. pp. 255–256,576.ISBN 9780201889543.
  10. ^ a b c d e Gail Anderson (1998). "Object Storage Management".Navigating C++ and Object-oriented Design. Prentice Hall. pp. 345–356. ISBN 0135327482.
  11. ^ Stroustrup, Bjarne (1994). "Memory Management". Design and Evolution of C++. Addison-Wesley. ISBN 9780201543308.
  12. ^ Graham M. Seed and Barry J. Cooper (2001). An Introduction to Object-oriented Programming in C++ (Second Edition ed.). Springer. pp. 435–436. ISBN 1852334509.
  13. ^ a b Stephen C. Dewhurst (2003). "Gotcha #62: Replacing Global New and Delete". C++ Gotchas. Addison-Wesley. pp. 173–176.ISBN 9780321125187.
  14. ^ a b c Wu Yongwei (2007-12-31). "A Cross-Platform Memory Leak Detector". Wu Yongwei's Programming Page. Retrieved on 2008-11-26.
  15. ^ a b c d Gail Anderson (1998). "Exception Handling". Navigating C++ and Object-oriented Design. Prentice Hall. pp. 631–632. ISBN 0135327482.

[edit] Further reading

·             Franek, Frantisek (2004). Memory as a Programming Concept in C and C++. Cambridge University Press. ISBN 9780521520430.

·             Marshall Cline (2006-09-25). "11.10: What is "placement new" and why would I use it?". C++ FAQ Lite. Retrieved on 2008-11-26.

·             "C++ new Operator". IBM's Mac OS X compilers. IBM (2003). Retrieved on 2008-11-27.

·             "The operator new Function". MSDN. Microsoft. Retrieved on2008-11-27.

·             "new Operator (C++)". MSDN. Microsoft. Retrieved on 2008-11-27.

·             Danny Kalev (1998-07-18). "Placement new". Tip of the day. Jupitermedia Corporation. Retrieved on 2008-11-27.

Retrieved from "http://en.wikipedia.org/wiki/Placement_syntax"

 

OpenClaw 接入智谱 GLM-5-Turbo 龙虾套餐:完整避坑指南与 SOP OpenClaw 接入智谱 GLM-5-Turbo 的核心在于“端点匹配”和“多渠道验证”。端点匹配:龙虾套餐必须用标准端点。多渠道验证:遇到 TUI 不显示时,用curl或飞书机器人验证,不要被 Bug 误导。希望这份 SOP 能帮你顺利跑通 OpenClaw + GLM-5-Turbo,享受强大的 AI 编程助手! 阅读详情

相关推荐

保姆级教程:用Docker Compose在Linux上一键部署Milvus 2.5.8(附配置文件详解)

本文提供了一份详细的Milvus 2.5.8安装指南,通过Docker Compose在Linux系统上一键部署这款高性能向量数据库。教程涵盖环境准备、配置文件解析、服务启动验证及常见问题解决,帮助开发者快速搭建AI应用所需的基础设施。

weixin_28829629的博客 269

Placement New的使用的完整总结版

今天看STL源码遇到一个问题:   template inline void _Construct(_T1* __p, const _T2& __value) {   new ((void*) __p) _T1(__value); }   上网搜到了一些文章,分享了:   原帖地址: http://www.ksarea.com/articles/20080124_cc.html

u013721481的专栏 1760

物联网低功耗广域网NB-IoT开发(一):初识NB-IoT

(二)NB-IoT的关键特性: (三)NB-IoT的应用场景: 二、NB-IOT介绍

weixin_49763156的博客 853

C++语言中placement 语法

/* *C++ 语言学习 *     —— placement 语法 *    内容来自网上及摘自 C++ Strategies and Tactics */Placement New    关键字new可以接受参数:      p = new(arg1, arg2, arg3) D;    这些参数会被隐含地传给new操作函数:      p = operator new(sizeof(D),

wang_xf的Study home 1777

emqx Java 订阅以及发送 结合若依框架

emqx Java 订阅以及发送 结合若依框架

Spo的博客 1613

2024东南大学533复试C++程序设计笔试真题精讲

【东南553复试C++】2024东南大学533复试C++程序设计笔试真题精讲

转遍世界 3414

C++ 的placement new和placement delete

2、

木牛的博客 247

【高清照片】深圳光博会看PCIe Over Fibre 实现方案

本文分析了PCIe over Fibre技术及其在数据中心的应用前景。该技术通过光纤延伸PCIe总线,解决了传统铜缆距离限制问题,支持跨机架资源池化。目前主要有两种实现方式:CDFP混合光缆方案和Retimer转接卡方案,各有优缺点。文章还探讨了CDFP接口的现状、低速信号传输解决方案,以及PCIe速率与光通信发展的对应关系。最后对比了PCIe光缆与以太网光缆的关键差异,指出PCIe光互连在协议透明度和边带信号处理上的特殊要求。随着标准推进,PCIe over Fibre将成为数据中心扩展高带宽、低时延互连

Saniffer的博客 1385

探秘C++中的Placement New:深度解析与常规new的对比

为C++开发者提供了在特定内存地址构造对象的能力,与常规new相比,它虽需手动管理内存生命周期,但却赋予了内存使用的高度灵活性和性能优化空间。在内存敏感或追求极致性能的场景,如内存池实现、硬件交互中,成为不可或缺的工具,通过直接在预分配内存上创建对象,有效规避了频繁内存分配的开销与碎片化问题,展现了其在特定应用中的独特价值。

DoctorDragon的博客 5251

❤️爆肝十二万字《python从零到精通教程》,从零教你变大佬❤️(建议收藏)

文章目录强烈推荐系列教程,建议学起来!!一.pycharm下载安装二.python下载安装三.pycharm上配置python四.配置镜像源让你下载嗖嗖的快4.1pycharm内部配置4.2手动添加镜像源4.3永久配置镜像源五.插件安装(比如汉化?)5.1自动补码神器第一款5.2汉化pycharm5.3其它插件六.美女背景七.自定义脚本开头八、这个前言一定要看九、python入门十、python缩进十一、Python注释1.单行注释2.多行注释十二、Python 变量1.变量定义理解2. 变量名命名3. 分

全栈川川 26万+

使用RV1126实现人脸识别——将Facenet模型转换为RKNN模型

在本文中,我们将探讨如何在RV1126芯片上实现人脸识别,并将Facenet模型转换为RKNN(Rockchip Neural Network)模型,以在RV1126上进行高效的人脸识别。Fine-tuning是指在预训练模型的基础上,使用自己的数据集进行进一步训练,以适应特定的任务。同时,我们需要下载Facenet模型的预训练权重,这些权重将作为我们的基础模型。综上所述,我们详细介绍了如何在RV1126上实现人脸识别,并将Facenet模型转换为RKNN模型。方法将模型导出为RKNN格式的文件。

code_program481的博客 1522

C++:43---placement new(定位new)

一、什么是placement new placement new就是在用户指定的内存位置上(这个内存是已经预先分配好的)构建新的对象,因此这个构建过程不需要额外分配内存,只需要调用对象的构造函数在该内存位置上构造对象即可 语法格式: address:placement new所指定的内存地址 ClassConstruct:对象的构造函数 Object * p = new (addr...

董哥的黑板报 1万+

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

Cpp 对象模型探索 / placement new 实现原理

一、placement new 调用过程 1、源代码 #include <new> class CTest { public: int i_ = 0; int t_ = 0; }; int main() { void *pbuffer = (void*)new char[sizeof(CTest)]; CTest *ptest = new (pbuffer) CTes...

布袋和尚 724

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

C++ 开发必须要了解placement new的这些技巧

本文介绍了C++中placement new的核心概念和实用技巧。placement new允许在预分配内存上构造对象,实现内存分配与对象构造的分离。关键点包括:语法形式new (pointer) Type、不分配内存仅构造对象、必须手动调用析构函数以及内存来源的多样性。主要应用于内存池优化、特定地址构造、避免数据拷贝等场景。文章通过代码示例展示了内存池实现和栈上对象构造,并强调使用时需注意内存对齐、手动析构和正确释放原始内存。

本博客分享软硬件一体化开发实战。硬件涵盖 PCB 设计、STM32 等 MCU 底层开发;软件包含 C++、Rust,Qt/QML、PyQt、Slint、TouchGFX、LVGL 各类 GUI 框架。方便软硬件开发者查阅学习。欢迎关注! 1181

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

C++中使用placement new

一.new new operator就是new操作符,不能被重载,假如A是一个类,那么A * a=new A;实际上执行如下3个过程。 (1)调用operator new分配内存,operator new (sizeof(A)) (2)调用构造函数生成类对象,A::A() (3)返回相应指针 事实上,分配内存这一操作就是由operator new(size_t)来完成的,如果类A重载了...

游戏开发、游戏引擎开发、逆向破解爱好者 503

Effective C++条款52:定制new和delete之(写了placement new也要写placement delete)

placement new和placement delete在C++中可能并不常用,但是你需要了解它们 关于placement new语法可以参阅:https://blog.csdn.net/qq_41453285/article/details/104690501 一、正常签名式的new和delete 对于使用new来创建对象时,其可能在两个地方会抛出异常: ①当在调用new()时,ne...

董哥的黑板报 938

C++中placement new操作符(经典)

placement new是重载operator new的一个标准、全局的版本,它不能被自定义的版本代替(不像普通的operator new和operator delete能够被替换成用户自定义的版本)。 它的原型如下: void *operator new( size_t, void *p ) throw() { return p; } 首先我们区分下几个容易混淆的关键词:new、......

Think88666的博客 1605

Effective C++ 条款52:写了 placement new 也要写 placement delete

深入解析 Effective C++ 条款52,揭示 placement new 与 placement delete 的配对必要性,分析构造函数异常时的隐微内存泄漏问题,以及如何避免遮掩正常版本

凡人叶枫的博客 234

C++中的定位放置new(placement new)

一般来说,使用new申请空间时,是从系统的“堆”(heap)中分配空间。申请所得的空间的位置时根据当时的内存的实际使用情况决定的。但是,在某些特殊情况下,可能需要在程序员指定的特定内存创建对象,这就是所谓的“定位放置new”(placement new)操作。 定位放置new操作的语法形式不同于普通的new操作。例如,一般都用如下语句A* p=new A;申请空间,而定位放置new操作则使用如下语...

昔拉的博客 535

C++ placement new

什么是placement new? 所谓placement new就是在用户指定的内存位置上构建新的对象,这个构建过程不需要额外分配内存,只需要调用对象的构造函数即可 placement new的好处 在已分配好的内存上进行对象的构建,构建速度快 已分配好的内存可以反复利用,有效的避免内存碎片问题。 operate new 重载 当我们创建一个类的对象时,调用如下: class MyClass...

【黑键】 1043

全面解析C++中的new,operator new与placement new

全面解析C++中的new,operator new与placement new   以下是C++中的new,operator new与placement new进行了详细的说明介绍,需要的朋友可以过来参考下 new operator/delete operator就是new和delete操作符,而operator new/operator delete是函数。 new operat

walkerkalr的专栏 1465

Placement New实现自More Effective C++ Item4

#include using namespace std; class EquipmentPiece { public: EquipmentPiece(int IDNumber) { this->IDNumber = IDNumber; } void PrintOut() { cout << IDNumber << end

Yerasel的专栏 399

C++中new、operator new和placement new的区别

new和delete是C++中的表达式,用于创建一个新的对象。它们是对堆中的内存进行申请和释放,而且这两个都是不能被重载的。要实现不同的内存分配行为,需要重载operator new和operator delete而不是new和delete。 1.仔细说说newexpression 在使用了new运算符之后,编译器实际做了哪些事情呢? Foo* p = new Foo(); 在这个代码中,这里的new实际上是执行如下3个过程: 1调用operator new分配内存,大小为Foo对象所占...

init_bin的博客 541

C++ Placement New

C++ Placement New  placement new是重载operator new的一个标准、全局的版本,它不能被自定义的版本代替(不像普通的operator new和operator delete能够被替换成用户自定义的版本)。 它的原型如下: void *operator new( size_t, void *p ) throw()  { return

游蓝海 的专栏 1074
上一篇: vim常用操作命令
下一篇: 在delete和delete[]背后,编译器的不同对待
origin_lee
博客等级 码龄12年 29粉丝 5原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值