c++ - 使用 malloc() 初始化类

标签 c++ c lua malloc new-operator

当 C++ 类的内存已从 C malloc 中保留时,应该如何使用它?

我正在使用 C 库 (lua),我需要向它公开一个 C++ 类,在这种情况下,为了垃圾收集这些保留空间,lua 会保留内存。

一个更简单的类似场景如下:

#include <string>

class Clase{
private:
    std::string valor;
public:
    Clase(){}
    Clase(const std::string & valor) : valor(valor){}
    const std::string & get() const { return this->valor; }
    void set(const std::string & valor){ this->valor = valor;}
    ~Clase(){}
};

typedef struct
{
    Clase cls;
}Estructura;

int main(int argc, char ** argv)
{
    Estructura * est = (Estructura *) malloc(sizeof(Estructura));

    est->cls.set("Hola");   // First attempt

    Clase myCls;   // Second attempt
    est->cls = myCls;

    return 0;
}

我理解并检查过,使用 malloc 不会调用类构造函数;这是预期的,因此不能使用无效实例(类中的字符串)调用复制(赋值)运算符。我怀疑在复制 Class 实例中的字符串时,第二次尝试在同一点上失败了。

所以:

  • 是否可以正确初始化一个由 malloc 保留其内存的类实例?
  • 还有哪些其他注意事项?vtables?
  • malloc 的 free 补充会导致内存泄漏吗? (我猜测由于不会调用 Clase 析构函数,因此不会正确释放字符串?[我假设字符串在实例本身之外保留内存])

在 Estructura 中使用 Clase 指针,效果很好,这是最好的解决方案吗?

作为奖励,当 lua 垃圾收集实例时删除实例的最佳方法是使用 __gc 元方法还是更好的方法?

最佳答案

使用 malloc 而不是 new 有点奇怪,但这是可能的。您需要使用新的展示位置:

void *memory = malloc(sizeof(Estructura));

Estructura *est = new(memory)Estructura;

当您完成对象后,您有责任自己调用析构函数:

est->~Estructura();

一切,例如 vtables,都会被正确初始化,因此无需担心。尴尬的一点是处理删除,因为您需要在通过 free 释放内存之前销毁对象。 delete 会自动为您执行此操作,但您需要自己执行此操作。

关于c++ - 使用 malloc() 初始化类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30046987/

相关文章:

android - 在 Android 上使用 LuaJ 从 Lua 脚本中请求其他 lua 脚本

arrays - 将二进制文件读入数组

http - 如何以简单、无痛的方式提供 lua 页面/脚本?

c++ - 具有外部说明符的全局变量的循环依赖

python - 将 C 枚举导入到 python 中而不包含完整的 header ?

c - 为什么我们不应该在printf语句中传递程序的输入呢?

c - 用 C 编写一个简单的 voip 应用程序

c++ - 二叉搜索树相对于 C++ vector 的优势

c++ - 从 struct 到 char * 的转换问题

c++ - 为什么++i 被认为是左值,而 i++ 不是?