简体   繁体   English

如何在使用malloc旁边访问堆内存?

[英]How can I access heap memory beside using malloc?

Is there a way that you can assign memory from heap without a call to malloc? 有没有一种方法可以在不调用malloc的情况下从堆中分配内存? Can the following call be affective for it? 以下电话可以对它有效吗?

void* loc = (void*) &heap[end_of_heap];

No. The C language itself provides no such functionality. 不,C语言本身不提供此类功能。 If you only care about Unix systems conforming to a deprecated feature of an old version of the Unix standard (SUSv2 or earlier, if I remember correctly), the brk and sbrk functions provide this functionality. 如果您只关心Unix系统符合旧版Unix标准(SUSv2或更早版本,如果我没记错的话)的弃用功能,则brksbrk函数提供此功能。 But you really should not use it unless you're writing very low-level code that will never need to be portable. 但是你真的不应该使用它,除非你编写的是非常低级的代码,永远不需要是可移植的。

There is no portable way besides malloc and friends, but if you're willing to get platform-specific sbrk (and brk ) in old-fashioned Unix (not in current Posix), used to be the underlying syscalls. 除了malloc和朋友之外没有可移植的方式,但是如果你愿意在老式的Unix(不是当前的Posix)中获得特定于平台的sbrk (和brk ),那么它曾经是底层的系统调用。 Now their manpage says 现在他们的manpage说

Avoid using brk() and sbrk(): the malloc(3) memory allocation package is the portable and comfortable way of allocating memory. 避免使用brk()和sbrk():malloc(3)内存分配包是分配内存的便携和舒适的方式。

and that advice is surely good (no real advantage in using the old-fashioned syscalls even in platforms that supply them). 并且这个建议肯定是好的(即使在提供它们的平台上也没有使用老式系统调用的真正优势)。 mmap of some /dev/ is a totally different way for some modern Unix versions, Windows has its own totally different "win32 API calls" for the purpose, and so on. 对于某些现代Unix版本,某些/dev/ mmap是完全不同的方式,Windows为此目的有自己完全不同的“win32 API调用”,依此类推。

There is no way to get a pointer to new and valid heap memory other than using a heap allocating function. 除了使用堆分配函数之外,无法获得指向新的有效堆内存的指针。 You cannot simply add a pointer into the heap at the end of an existing pointer and expect to reliably access it. 您不能简单地将指针添加到现有指针末尾的堆中,并期望可靠地访问它。

The Standard does not say anything about heap (search it, if you don't believe this). 标准没有说堆的任何内容(搜索它,如果你不相信这个)。 An implementation is not even required to have a heap (as we commonly know it). 甚至不需要实现堆(如我们通常所知)。

However, the short answer to your question is, no in Standard C. Unless of course you use a platform specific API. 但是,对您的问题的简短回答是,标准C中没有。除非您使用特定于平台的API。 Typically, OS APIs sometimes do give you some leeway as to accessing memory. 通常,OS API有时会为访问内存提供一些余地。

You cannot access heap reliably without malloc, but there are alternatives for memory allocation. 没有malloc,你无法可靠地访问堆,但是有内存分配的替代方案。

If you're trying to get finer control over memory allocations, you can use other memory managers like bget memory allocator . 如果您试图更好地控制内存分配,可以使用其他内存管理器,如bget内存分配器 Here you grab a huge chunk of heap (the maximum memory requiredment anticipated + some overhead) using malloc and pass it to the bget using bpool . 在这里你使用malloc抓住一大块堆(预期的最大内存需求+一些开销)并使用bpool将其传递给bpool From there on, call bget instead of malloc to allocate memory and brel to free it. 从那里开始,调用bget而不是malloc来分配内存和brel以释放它。 bget is reportedly better in avoiding memory fragmentation. 据报道 ,bget可以更好地避免内存碎片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM