C++ 方法积累

C++ 复制代码
std::numeric_limits<UInt32>::max()

placement new

用于控制分配空间

cpp 复制代码
        PostingListInMemory * posting_list = arena.alloc<PostingListInMemory>();
        new (posting_list) PostingListInMemory();
cpp 复制代码
/// placement new;
    PostingListInMemory & operator=(const PostingListInMemory & other)
    {
        if (this != &other)
        {
            this->~PostingListInMemory();

            if (other.m.set.type != 0)
                new (&m.bitmap) roaring::Roaring(other.m.bitmap);
            else
                new (&m.set) PostingListSet(other.m.set);
        }
        return *this;
    }


    PostingListInMemory * local_posting_list = external_posting_list;
    if (!local_posting_list)
    {
        local_posting_list = arena.alloc<PostingListInMemory>();
        new (local_posting_list) PostingListInMemory();
    }
slignas union
cpp 复制代码
struct alignas(8) PostingListInMemory
{
    union M
    {
        PostingListSet set;
        roaring::Roaring bitmap;

        M() : set() {}

        ~M()
        {
            if (set.type != 0)
                bitmap.~Roaring();
        }
    } m;

这段代码定义了一个名为 PostingListInMemory 的结构体,其中包含了一个匿名联合体(union)M。在C++中,联合体是一种特殊的数据结构,它允许在相同的内存位置存储不同的数据类型,但同一时间只能存储其中的一种类型。

联合体 M 的定义

联合体 M 包含了两个成员:

  1. PostingListSet set;

  2. roaring::Roaring bitmap;

并且有一个构造函数和一个析构函数。

联合体的作用

在这个上下文中,联合体 M 允许 PostingListInMemory 结构体在相同的内存位置存储两种不同类型的数据结构,即 PostingListSet 和 roaring::Roaring。这意味着 PostingListInMemory 的实例可以根据需要在两种表示方法之间切换,而不需要额外的内存开销。

构造函数和析构函数

构造函数 M():初始化联合体的 set 成员。

析构函数 ~M():这是一个自定义的析构函数,它的目的是确保在销毁 M 联合体时,正确地调用所存储对象的析构函数。这里使用了一个条件判断 if (set.type != 0) 来检查当前联合体中存储的是哪个对象。如果 set.type 不为0,这意味着 set 成员被使用过,因此需要显式调用 bitmap 的析构函数。这种做法是为了避免在销毁时调用错误的析构函数,因为联合体中的成员共享内存空间。

注意事项

这种使用联合体和条件析构的做法是比较底层的操作,需要对内存管理和对象生命周期有深入的理解。

在现代C++中,通常推荐使用智能指针或其他RAII(Resource Acquisition Is Initialization)技术来管理资源,以避免这类复杂的内存管理问题。

此外,alignas(8) 指令确保了整个 PostingListInMemory 结构体按8字节对齐,这可能是为了优化性能或满足某些硬件平台的要求。

总之,这段代码展示了一种在C++中使用联合体来实现类型双关(type punning)的方法,允许在相同的内存位置存储和使用两种不同的数据结构。

相关推荐
天赐学c语言7 分钟前
12.18 - 有效的括号 && C语言中static的作用
数据结构·c++·算法·leecode
Dream it possible!31 分钟前
LeetCode 面试经典 150_回溯_组合(99_77_C++_中等)
c++·leetcode·面试·回溯
再睡一夏就好1 小时前
深入解析Linux页表:从虚拟地址到物理内存的映射艺术
linux·运维·服务器·c语言·c++·页表·缺页异常
Starry_hello world1 小时前
C++ 线程 (3)
c++
雍凉明月夜2 小时前
c++ 精学笔记记录Ⅲ
c++·笔记·学习
oioihoii2 小时前
C++共享内存小白入门指南
java·c++·算法
布茹 ei ai2 小时前
QtWeatherApp - 简单天气预报软件(C++ Qt6)(附源码)
开发语言·c++·qt·开源·开源项目·天气预报
Bruce_kaizy2 小时前
c++图论————图的基本与遍历
c++·算法·图论
Zmm147258369_2 小时前
好用的PC耐力板机构
c++
Code Slacker2 小时前
LeetCode Hot100 —— 普通数组(面试纯背版)(五)
数据结构·c++·算法·leetcode·面试