简体   繁体   English

shared_ptr完全在堆栈上

[英]shared_ptr entirely on stack

Assuming I know the stack frame will outlive all the copies of the shared_ptr , is there any way to create a shared_ptr to a stack object such that the reference counter is also on the stack so that there's no dynamic allocation at any point? 假设我知道堆栈帧将比shared_ptr所有副本更长,有没有办法为堆栈对象创建一个shared_ptr ,这样引用计数器也在堆栈上,这样在任何时候都没有动态分配?

eg 例如

SomeObject anObject;
std::shared_ptr::ref_counter refCounter; // Does this exist?

std::shared_ptr<SomeObject>(&anObject, &refCounter, [](SomeObject* obj){
    obj->DoSomething();
});

The goal here being to use shared_ptr for its reference counting rather than as a smart pointer. 这里的目标是使用shared_ptr进行引用计数而不是智能指针。

EDIT: I'm gonna add some more explanation to make the reasons for doing this more clear. 编辑:我将添加更多解释,以便更清楚地说明这一点。

I'm trying to create a token that calls a function when it and all its copies are destroyed for a threading library I'm writing. 我正在尝试创建一个调用函数的标记,当它正在编写的线程库中销毁它的所有副本时。 Each token is essentially just a wrapper for a smart pointer to a persistent object that holds the function and calls it in its destructor. 每个标记本质上只是一个指向持久化对象的智能指针的包装器,该持久化对象保存该函数并在其析构函数中调用它。 Copying the token copies the wrapper (and thus the smart pointer), but not the persistent object. 复制令牌会复制包装器(以及智能指针),但不会复制持久对象。

Given that these tokens may be passed to many different threads the persistent object usually needs to be on the heap, but some of the time I can actually guarantee that a particular stack frame will outlive all the copies of any tokens it creates. 鉴于这些令牌可以传递给许多不同的线程,持久对象通常需要在堆上,但有时我可以保证特定的堆栈帧将比它创建的任何令牌的所有副本都活得更长。 In those situations the persistent part of the token can be created on the stack, forgoing any expensive heap allocation. 在这些情况下,可以在堆栈上创建令牌的持久部分,从而放弃任何昂贵的堆分配。

So in some situations the smart pointer does need to actually own the object it's pointing to, but in others it doesn't. 所以在某些情况下,智能指针确实需要拥有它所指向的对象,但在其他情况下则不需要。

There is no way to manage a stack allocated object with a shared pointer. 无法使用共享指针管理堆栈分配的对象。

However, there should not be any need for it either. 但是,也不应该有任何需要。 In place of the shared pointer, you can use a bare pointer or perhaps a reference. 代替共享指针,您可以使用裸指针或引用。 Since you know that the referenced object will outlive all users, it is safe. 既然您知道引用的对象将比所有用户都活跃,那么它是安全的。


I'm trying to create a token that calls a function when it and all its copies are destroyed 我正在尝试创建一个令牌,当它及其所有副本被销毁时调用该函数

For that, you don't want to use a shared pointer. 为此,您不想使用共享指针。 You should just implement your own reference counter. 您应该只实现自己的参考计数器。

you can do this but it would not be a good choice*. 你可以做到这一点,但这不是一个好的选择*。


SomeObject anObject;
std::shared_ptr<SomeObject>(&anObject, [](auto x){}});

coliru example coliru的例子


*this does not use most of the functionality of shared_ptr and is a waste, and the count can be recorded in many other ways include implement you own counting class. *这不使用shared_ptr大多数功能而且是浪费,并且可以通过许多其他方式记录计数,包括实现您自己的计数类。


I really see no reason prevent dynamic allocation. 我真的认为没有理由阻止动态分配。 Even if you can't use heap, the dynamic allocation can happens fully on stack. 即使您不能使用堆,动态分配也可以在堆栈上完全发生。

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

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