使用 new、malloc 和 free 布局

发布于 2024-12-13 04:01:14 字数 992 浏览 3 评论 0原文

基本上,我有一个使用 malloc 分配的内存块,我想开始使用placement new 将对象放入其中。我知道当我删除这些对象时,必须显式调用它们的析构函数,但我想确保我完全理解这一点,并且我正在以正确的方式处理它。我几乎肯定会放弃这种方法并以更直接的方式处理事情,但我认为我只是为了理解而要求。

我有一个 WordIndex 类,其中包含以下私有成员变量:

struct Word
{
    std::string word;
    std::vector<unsigned int> index;
};

Word* m_words;
std::string* m_lines;

使用 malloc/realloc 为两个指针分配/重新分配内存,并且使用以下代码创建对象(它们不会覆盖任何预先存在的对象):

new (&m_words[pos]) Word();
new (&m_lines[m_numoflines]) std::string();

我有一个析构 函数如下所示,但我几乎 100% 确定它没有正确执行操作,尽管运行时没有显示任何错误。

WordIndex::~WordIndex()
{
    unsigned int i;
    for( i = 0; i < m_numofwords; i++)
    {
        m_words[i].~Word();
    }

    free(m_words);
    free(m_lines);
}

我的问题如下:

  1. 考虑到Word结构体有一个向量,是否只需简单地调用~Word就足够了,或者我是否需要预先单独调用向量的析构函数?
  2. 我是否需要为字符串(m_lines 和 Word.word)调用析构函数,如果需要,我该怎么做?它似乎没有像 ~string 这样我可以调用的函数。
  3. 我还错过了什么吗?我相当确定我有。

提前致谢!

Basically, I have a block of memory allocated using malloc that I want to start placing objects into using placement new. I know that the destructors for these objects will have to be explicitly called when I'm deleting them, but I want to make sure that I understand this completely, and that I'm going about it the right way. I'm almost certainly going to scrap this approach and go about things in a more straightforward manner, but I figured that I'd ask just for understanding's sake.

I have a class WordIndex that contains the following private member variables:

struct Word
{
    std::string word;
    std::vector<unsigned int> index;
};

Word* m_words;
std::string* m_lines;

Memory is allocated/reallocated for both pointers using malloc/realloc, and objects are being created using the following code (they're not overwriting any preexisting objects):

new (&m_words[pos]) Word();
new (&m_lines[m_numoflines]) std::string();

I have a destructor as follows, but I'm almost 100% sure that it's not doing things correctly, despite not showing any errors when running.

WordIndex::~WordIndex()
{
    unsigned int i;
    for( i = 0; i < m_numofwords; i++)
    {
        m_words[i].~Word();
    }

    free(m_words);
    free(m_lines);
}

My questions are as follows:

  1. Considering the Word struct has a vector, is it enough to simply call ~Word, or do I need to call the destructor for the vector separately beforehand?
  2. Do I need to call a destructor for a string (both for m_lines and for Word.word), and if so, how do I do that? It doesn't appear to have a function like ~string that I can call.
  3. Have I missed anything else? I'm fairly certain that I have.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转身泪倾城 2024-12-20 04:01:14
  1. 考虑到 Word 结构体有一个向量,是否只需调用 ~Word 就足够了,还是需要调用向量的析构函数
    事先分开吗?

如果需要,您可以为 struct 编写析构函数,但在本例中没有必要。

  1. 我是否需要为字符串(m_lines 和 Word.word)调用析构函数?如果需要,我该怎么做?看来没有
    有一个像 ~string 这样的函数我可以调用。

是的,你知道。析构函数应该在那里,它是从 basic_string 中进行 typedef 编辑的,因此您应该对其调用 ~basic_string()

  1. 我还错过了什么吗?我相当确定我已经做到了。

string 的析构函数。

另外,请确保正确计算 malloc 的空间,您不会显示该代码,但请验证其应有的内容。

  1. Considering the Word struct has a vector, is it enough to simply call ~Word, or do I need to call the destructor for the vector
    separately beforehand?

You can write a destructor for your struct if you want, but in this case it's not necessary.

  1. Do I need to call a destructor for a string (both for m_lines and for Word.word), and if so, how do I do that? It doesn't appear to
    have a function like ~string that I can call.

Yes, you do. The destructor should be there, it's typedefed from basic_string, so you should be calling ~basic_string() on it.

  1. Have I missed anything else? I'm fairly certain that I have.

The destructor for string.

Also, make sure you calculate the space for malloc correctly, you don't show that code, but verify its what its supposed to be.

暮倦 2024-12-20 04:01:14

您唯一缺少的是对所有字符串析构函数的调用。它被称为 ~basic_stringstring 实际上只是带有一些模板参数的 basic_stringtypedef)。

不,您不需要调用 vector 的析构函数,因为它们是由编译器为您的 Word 结构提供的析构函数调用的。

The only thing you're missing is the call to all the strings' destructors. It's called ~basic_string (string is actually just a typedef for basic_string with some template parameters).

No, you don't need to call the vectors' destructors, because they're called by the compiler-provided destructor for your Word struct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文