c - MS Win32 安全函数中的内存分配

标签 c security winapi memory

为什么 MS Security 示例似乎在一个函数的文档中使用一个内存分配例程,而在另一个函数中使用另一个?

我找到了一个例子,他们实际上在示例代码的不同点使用了不同的代码。查看GetAppContainerNamedObjectPath功能。

分配 SID 时使用 HeapAlloc,分配安全描述符时使用 LocalAlloc。两个分配都在示例结束前释放,为什么他们使用不同的方法?我看到人们在博客示例中使用 ::malloc

最佳答案

LocalAllocHeapAlloc 在这种情况下是相同的。情况并非总是如此,但至少已经有十年了。我知道,这很令人困惑,但这都是遗留 16 位系统和向后兼容性的结果。看这个documentation :

Windows memory management does not provide a separate local heap and global heap, as 16-bit > Windows does. As a result, the global and local families of functions are equivalent and choosing between them is a matter of personal preference.

另见 documentation :

There is no difference between memory allocated from a private heap and that allocated by using the other memory allocation functions. For a complete list of functions, see the table in Memory Management Functions.

存在差异,因此请始终阅读内存功能的文档。例如,CoTaskMemFree 处理 NULL 但 HeapFree 不处理。不同的分配函数将使您能够不同程度地控制操作系统对象(进程等)之间的内存分配和共享方式。但是,如果您只想为您的进程提供一些普通的旧内存,请始终检查您正在使用的 API 的文档,因为它可能指定您应该使用特定的分配或释放函数,否则,只需选择一个并保持一致。

至于为什么文档会把它调高?我的猜测是,随着时间的推移,它被不止一个人入侵,其中没有一个人是真正的操作系统团队成员。 MSDN 示例代码是出了名的糟糕。您应该可以通过某种方式标记它或在 MSDN 页面上留下反馈。

关于c - MS Win32 安全函数中的内存分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19254699/

相关文章:

c++ - 如何检查已安装的字体样式?

c - 显示存储在 window dc 中的位图

c++ - 为什么 strlen() 比手动循环检查空终止字符快大约 20 倍?

c - 段错误的原因与使用以下 C 代码返回随机值

java - 在浏览器中打开PDF文件漏洞

javascript - 像 Codewars 这样运行用户提交的代码的网站如何保护自己免受恶意用户的侵害?

c - 在 flex 中使用 strdup()

c - 使用 BST 对结构数组进行排序的函数的时间复杂度是多少?

java - Glassfish 安全 : Role changes should take effect immediately

c++ - 导出序号而不带 def 文件