[英]How do I allocate memory for ZIP struct?
I found a ZIP library that I want to re-write with WinAPI calls.我找到了一个ZIP 库,我想用 WinAPI 调用重新编写它。 I have almost done it, but I can't allocate memory for a
TState
structure.我几乎完成了,但是我不能为一个
TState
结构分配memory。
state = new TState();
works fine!工作良好!
state = (TState*)HeapAlloc(GetProcessHeap(), 0, sizeof(TState));
breaks archives!打破档案!
If I change HeapAlloc()
to malloc()
, nothing changes!如果我将
HeapAlloc()
更改为malloc()
,什么都不会改变!
So, what am I doing wrong?那么,我做错了什么?
TState
contains some non-trivial members (namely: TTreeState ts
and TDeflateState ds
) that have their own constructors which are called properly by new
, but which are not called by malloc()
/ HealAlloc()
. TState
包含一些重要的成员(即: TTreeState ts
和TDeflateState ds
),它们有自己的构造函数,这些构造函数可以被new
正确调用,但不会被malloc()
/ HealAlloc()
调用。 As such, you would need to use placement-new
to properly construct a TState
object inside of your allocated memory, eg:因此,您需要使用
placement-new
在分配的 memory 中正确构建TState
object,例如:
buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(TState)); // or malloc()
state = new(buffer) TState;
...
state->~TState();
HeapFree(GetProcessHeap(), 0, buffer); // or free()
Otherwise, you will have to re-write TTreeState
and TDeflateState
to make them into trivial types (ie, remove their constructors).否则,您将不得不重写
TTreeState
和TDeflateState
以将它们变成普通类型(即删除它们的构造函数)。 You will just have to initialize their data members manually after you have allocated each TState
instance.在分配每个
TState
实例后,您只需手动初始化它们的数据成员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.