c++ - make_shared 与自定义新运算符

标签 c++ c++11 new-operator shared-ptr

这可能是重复的,但我无法在任何地方找到解决方案。

我有这样的源代码:

struct Blob{
    //...    

    static void *operator new(size_t size_reported, size_t size) {
        return ::operator new(size);
    }
};

我是这样使用的:

std::shared_ptr<Blob> blob;
// ...
size_t size = calcSize(); // it returns say 231
Blob *p = new(size) Blob();
blob.reset(p);

我能以某种方式更改代码以便我可以使用 std::make_sharedstd::allocate_shared 所以我有 single allocation 而不是两次分配?


更新

我能够消除 new 并将代码简化为以下内容:

struct Blob{
    //...    
};

std::shared_ptr<Blob> blob;
// ...
size_t size = calcSize(); // it returns say 231

// allocate memory
void *addr = ::operator new(size);

// placement new
Blob *p = ::new(addr) Blob();

blob.reset(p);

它做的事情完全一样,但我想现在我想做的事情更清楚了。

最佳答案

这是得出的结论。

由于无法将大小传递给分配器,您可以通过全局变量类成员来传递。

在这两种情况下,解决方案都一点也不优雅,而且相当危险 - 灾难等待现在或以后需要维护代码时发生。

如果 allocate_sharedshared_ptr 控制 block 放在缓冲区类的之后,则可能会发生另一个意想不到的问题。

在这种情况下会出现明显的缓冲区溢出,因为 sizeof(buffer) 将报告大小为 1 个字节左右。

再一次 - 代码可以正常工作,但将来肯定会出现问题。


#include <stdio.h>
#include <string.h>

#include <memory>

// ============================

class buffer{
public:
    buffer(const char *s){
        strcpy(x, s);
    }

    char x[1];
};

// ============================

template <typename T>
struct buffer_allocator {
    using value_type = T;

    buffer_allocator() = default;

    template <class U>
    buffer_allocator(const buffer_allocator<U>&) {}

    T* allocate(size_t n) {
        void *p = operator new(n * sizeof(T));

        printf("Allocate   %p, (%zu)\n", p, get_size());

        return (T*) p;
    }

    void deallocate(T* p, size_t n) {
        delete p;

        printf("Deallocate %p, (%zu)\n", p, get_size());
    }

    size_t get_size(){
        return size;
    }

    void set_size(size_t size){
        this->size = size;
    }

private:
    size_t size = 0;
};

template <typename T, typename U>
inline bool operator == (const buffer_allocator<T>&, const buffer_allocator<U>&) {
  return true;
}

template <typename T, typename U>
inline bool operator != (const buffer_allocator<T>& a, const buffer_allocator<U>& b) {
  return !(a == b);
}

// ============================

int main(int argc, char *argv[]){
    buffer_allocator<buffer> ba;

    const char *s = "hello world!";

    ba.set_size( strlen(s) + 1 );

    auto b = std::allocate_shared<buffer>(ba, s);

    printf("Pointer    %p\n", b.get());

    printf("%s\n", b->x);
    printf("%zu\n", b.use_count());
    auto b1 = b;
    printf("%zu\n", b1.use_count());

    return 0;
}

关于c++ - make_shared 与自定义新运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31904439/

相关文章:

php - ** 是新的幂运算符,而不是 php 中的 power()

language-design - 为什么C#和Java会困扰“new”运算符?

c++ - 访问命名 union 中的字段

c++ - 如何设置枚举类型变量的默认值?

c++ - 段错误 : 11 c++ Error

c++ - 链接器声称该对象已定义

c++ - 检测公共(public)基类

c++ - 使用表复制指向对象的指针

c++ - 即使在离开范围后,您如何访问在 C++ 中分配在堆上的变量?

python - 在 python 代码中处理来自 C 库的对象