面试题答案
一键面试1. 缓存机制优化
- 原理:对于频繁使用的
std::weak_ptr
,缓存其过期状态可以避免重复的昂贵的lock()
操作(lock()
操作会尝试提升weak_ptr
为shared_ptr
,若weak_ptr
过期则返回空的shared_ptr
)。 - 实现方式:可以使用一个
std::unordered_map
来缓存weak_ptr
的过期状态。当检查weak_ptr
是否过期时,先在缓存中查找,若存在则直接返回缓存的结果;若不存在,则进行lock()
操作并将结果存入缓存。
#include <memory>
#include <unordered_map>
std::unordered_map<std::weak_ptr<int>, bool> weakPtrCache;
bool isWeakPtrExpired(const std::weak_ptr<int>& wp) {
auto it = weakPtrCache.find(wp);
if (it != weakPtrCache.end()) {
return it->second;
}
bool expired = wp.lock().expired();
weakPtrCache[wp] = expired;
return expired;
}
2. 过期策略调整
- 原理:默认情况下,
std::weak_ptr
的过期判断基于std::shared_ptr
的引用计数变为零。在某些场景下,可以通过自定义对象生命周期管理来提前标记对象为 “逻辑过期”,即使shared_ptr
的引用计数不为零。 - 实现方式:可以在对象类中添加一个标志位来表示逻辑过期。当对象逻辑过期时,即使还有
shared_ptr
指向它,weak_ptr
也应该被视为过期。
class MyClass {
public:
bool isLogicallyExpired = false;
};
bool isWeakPtrLogicallyExpired(const std::weak_ptr<MyClass>& wp) {
auto sp = wp.lock();
if (!sp) {
return true;
}
return sp->isLogicallyExpired;
}
3. 与其他智能指针的协同优化
- 原理:在使用
std::weak_ptr
时,结合std::unique_ptr
等其他智能指针可以减少不必要的weak_ptr
引用。例如,对于一些临时对象或单例对象的管理,可以优先使用std::unique_ptr
,只有在需要解决循环引用等特定场景下才使用std::weak_ptr
。 - 实现方式:假设存在一个单例对象,使用
std::unique_ptr
来管理其核心数据,而通过std::weak_ptr
来提供外部弱引用。
class SingletonData {
public:
// 单例数据的具体内容
};
class Singleton {
private:
static std::unique_ptr<SingletonData> data;
static std::weak_ptr<SingletonData> weakRef;
public:
static std::weak_ptr<SingletonData> getWeakRef() {
if (weakRef.expired()) {
data = std::make_unique<SingletonData>();
weakRef = data;
}
return weakRef;
}
};
std::unique_ptr<SingletonData> Singleton::data;
std::weak_ptr<SingletonData> Singleton::weakRef;
通过上述优化方案,可以在使用 std::weak_ptr
进行内存管理时,从缓存机制、过期策略调整以及与其他智能指针协同等方面提升整体的内存管理效率。