使用 new、malloc 和 free 布局
基本上,我有一个使用 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);
}
我的问题如下:
- 考虑到Word结构体有一个向量,是否只需简单地调用~Word就足够了,或者我是否需要预先单独调用向量的析构函数?
- 我是否需要为字符串(m_lines 和 Word.word)调用析构函数,如果需要,我该怎么做?它似乎没有像 ~string 这样我可以调用的函数。
- 我还错过了什么吗?我相当确定我有。
提前致谢!
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:
- 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?
- 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.
- Have I missed anything else? I'm fairly certain that I have.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果需要,您可以为
struct
编写析构函数,但在本例中没有必要。是的,你知道。析构函数应该在那里,它是从
basic_string
中进行typedef
编辑的,因此您应该对其调用~basic_string()
。string
的析构函数。另外,请确保正确计算
malloc
的空间,您不会显示该代码,但请验证其应有的内容。You can write a destructor for your
struct
if you want, but in this case it's not necessary.Yes, you do. The destructor should be there, it's
typedef
ed frombasic_string
, so you should be calling~basic_string()
on it.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.您唯一缺少的是对所有字符串析构函数的调用。它被称为
~basic_string
(string
实际上只是带有一些模板参数的basic_string
的typedef
)。不,您不需要调用
vector
的析构函数,因为它们是由编译器为您的Word
结构提供的析构函数调用的。The only thing you're missing is the call to all the
string
s' destructors. It's called~basic_string
(string
is actually just atypedef
forbasic_string
with some template parameters).No, you don't need to call the
vector
s' destructors, because they're called by the compiler-provided destructor for yourWord
struct.