使用placement new,malloc和free [英] Using placement new, malloc, and free

查看:218
本文介绍了使用placement new,malloc和free的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我有包含以下私有成员变量的类WordIndex:

  struct Word 
{
std :: string字;
std :: vector< unsigned int>指数;
};

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_words [i]。〜Word();
}

free(m_words);
免费(m_lines);
}

我的问题如下:


  1. 考虑到Word结构有一个向量,是否足够简单地调用〜Word,或者我需要单独为向量单独调用析构函数?

  2. 我需要为字符串调用析构函数(对于m_lines和Word.word),如果是这样,我该如何做?它似乎没有像〜string这样的函数,我可以调用。

  3. 我错过了什么吗?

提前感谢!

解决方案



  1. 考虑Word结构有一个向量,是否足够简单地调用〜Word,或者我需要调用析构函数为向量
    单独预先?


你可以为你的<



    如果你想要的话,
  1. 我需要为字符串调用析构函数(对于m_lines和Word.word),如果是这样,我该如何做?
    似乎没有像我可以调用的〜string这样的函数。


是的,你做。析构函数应该在那里,它 typedef ed从 basic_string ,所以你应该调用〜 basic_string()




string的析构函数



此外,请确保您正确计算 malloc 的空间,不显示该代码,它应该是什么。


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!

解决方案

  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.

这篇关于使用placement new,malloc和free的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆