在C语言中,malloc()的替代方法是什么?

时间:2022-09-25 16:56:59

I am writing C for an MPC 555 board and need to figure out how to allocate dynamic memory without using malloc.

我正在编写一个MPC 555板的C,需要弄清楚如何在不使用malloc的情况下分配动态内存。

13 个解决方案

#1


18  

Typically malloc() is implemented on Unix using sbrk() or mmap(). (If you use the latter, you want to use the MAP_ANON flag.)

通常malloc()在Unix上使用sbrk()或mmap()实现。(如果使用后者,则需要使用MAP_ANON标志。)

If you're targetting Windows, VirtualAlloc may help. (More or less functionally equivalent to anonymous mmap().)

如果你在测试Windows, VirtualAlloc可能会有所帮助。(在功能上或多或少等同于匿名mmap())。

Update: Didn't realize you weren't running under a full OS, I somehow got the impression instead that this might be a homework assignment running on top of a Unix system or something...

更新:我没有意识到你没有在一个完整的操作系统下运行,但是我得到了这样的印象:这可能是在Unix系统上运行的家庭作业,或者其他什么……

If you are doing embedded work and you don't have a malloc(), I think you should find some memory range that it's OK for you to write on, and write your own malloc(). Or take someone else's.

如果您正在做嵌入式工作,并且没有malloc(),我认为您应该找到一些内存范围,您可以在其中编写自己的malloc()。或者别人的。

Pretty much the standard one that everybody borrows from was written by Doug Lea at SUNY Oswego. For example glibc's malloc is based on this. See: malloc.c, malloc.h.

几乎每个人都借鉴的标准是由SUNY Oswego的Doug Lea所写。例如glibc的malloc就是基于这个。看到:malloc。c,malloc.h。

#2


4  

If your runtime doesn't support malloc, you can find an open source malloc and tweak it to manage a chunk of memory yourself.

如果您的运行时不支持malloc,您可以找到一个开源malloc,并调整它来管理内存块。

#3


4  

You might want to check out Ralph Hempel's Embedded Memory Manager.

您可能想看看Ralph Hempel的嵌入式内存管理器。

#4


3  

malloc() is an abstraction that is use to allow C programs to allocate memory without having to understand details about how memory is actually allocated from the operating system. If you can't use malloc, then you have no choice other than to use whatever facilities for memory allocation that are provided by your operating system.

malloc()是一种抽象,用于允许C程序分配内存,而不必了解如何从操作系统中实际分配内存的细节。如果不能使用malloc,那么只能使用操作系统提供的任何内存分配工具。

If you have no operating system, then you must have full control over the layout of memory. At that point for simple systems the easiest solution is to just make everything static and/or global, for more complex systems, you will want to reserve some portion of memory for a heap allocator and then write (or borrow) some code that use that memory to implement malloc.

如果您没有操作系统,那么您必须完全控制内存的布局。对于简单的系统来说,最简单的解决方法就是让所有的东西都保持静态和/或全局,对于更复杂的系统,您将需要为堆分配器保留一部分内存,然后编写(或借)一些使用该内存来实现malloc的代码。

#5


3  

You should explain why you can't use malloc(), as there might be different solutions for different reasons, and there are several reasons why it might be forbidden or unavailable on small/embedded systems:

您应该解释为什么不能使用malloc(),因为可能由于不同的原因有不同的解决方案,并且有几个原因可以解释为什么在小型/嵌入式系统上禁止或不可使用malloc():

  • concern over memory fragmentation. In this case a set of routines that allocate fixed size memory blocks for one or more pools of memory might be the solution.
  • 担忧的记忆碎片。在本例中,为一个或多个内存池分配固定大小的内存块的一组例程可能是解决方案。
  • the runtime doesn't provide a malloc() - I think most modern toolsets for embedded systems do provide some way to link in a malloc() implementation, but maybe you're using one that doesn't for whatever reason. In that case, using Doug Lea's public domain malloc might be a good choice, but it might be too large for your system (I'm not familiar with the MPC 555 off the top of my head). If that's the case, a very simple, custom malloc() facility might be in order. It's not too hard to write, but make sure you unit test the hell out of uit because it's also easy to get details wrong. For example, I have a set of very small routines that use a brain dead memory allocation strategy using blocks on a free list (the allocator can be compile-time configured for first, best or last fit). I give it an array of char at initialization, and subsequent allocation calls will split free blocks as necessary. It's nowhere near as sophisticated as Lea's malloc(), but it's pretty dang small so for simple uses it'll do the trick.
  • 运行时并没有提供malloc()——我认为嵌入式系统的大多数现代工具集确实提供了一些方法来链接malloc()实现,但是可能您使用的是不出于任何原因的。在这种情况下,使用Doug Lea的公共域malloc可能是一个不错的选择,但是对于您的系统来说,它可能太大了(我对MPC 555不是很熟悉)。如果是这样,那么可能需要一个非常简单的自定义malloc()工具。写起来并不难,但一定要让你的单位测试出因纽特人的原因,因为这样做也很容易出错。例如,我有一组非常小的例程,它们使用空闲列表上的块来使用脑死内存分配策略(分配程序可以为第一、最佳或最后匹配配置编译时)。我在初始化时给它一个char数组,随后的分配调用将根据需要分割空闲块。它远没有Lea的malloc()那么复杂,但是它非常小,所以简单地使用它就可以了。
  • many embedded projects forbid the use of dynamic memory allocation - in this case, you have to live with statically allocated structures
  • 许多嵌入式项目禁止使用动态内存分配——在这种情况下,必须使用静态分配的结构

#6


3  

Write your own. Since your allocator will probably be specialized to a few types of objects, I recommend the Quick Fit scheme developed by Bill Wulf and Charles Weinstock. (I have not been able to find a free copy of this paper, but many people have access to the ACM digital library.) The paper is short, easy to read, and well suited to your problem.

写你自己的。由于您的分配器可能专门用于一些类型的对象,我推荐Bill Wulf和Charles Weinstock开发的Quick Fit方案。(我还没能找到这篇论文的免费拷贝,但很多人都可以使用ACM数字图书馆。)这篇论文篇幅短小,易于阅读,而且很适合你的问题。

If you turn out to need a more general allocator, the best guide I have found on the topic of programming on machines with fixed memory is Donald Knuth's book The Art of Computer Programming, Volume 1. If you want examples, you can find good ones in Don's epic book-length treatment of the source code of TeX, TeX: The Program.

如果你需要一个更通用的分配程序,我找到的关于固定记忆机器编程的最佳指南是Donald Knuth的书《计算机编程的艺术》,第1卷。如果您想要示例,可以在Don对TeX的源代码(TeX: the Program)的史诗般的图书长度处理中找到好的示例。

Finally, the undergraduate textbook by Bryant and O'Hallaron is rather expensive, but it goes through the implementation of malloc in excruciating detail.

最后,Bryant和O'Hallaron写的本科教材非常昂贵,但是它通过malloc的实现进行了极其详细的介绍。

#7


2  

Write your own. Preallocate a big chunk of static RAM, then write some functions to grab and release chunks of it. That's the spirit of what malloc() does, except that it asks the OS to allocate and deallocate memory pages dynamically.

写你自己的。预先分配一大块静态RAM,然后编写一些函数来获取和释放它的块。这就是malloc()所做的工作的本质,只不过它要求操作系统动态地分配和释放内存页。

There are a multitude of ways of keeping track of what is allocated and what is not (bitmaps, used/free linked lists, binary trees, etc.). You should be able to find many references with a few choice Google searches.

有很多方法可以跟踪分配的和不分配的(位图、使用/*链表、二叉树等)。在谷歌搜索中,您应该能够找到许多引用。

#8


2  

An answer really depends on why you might need to dynamically allocate memory. What is the system doing that it needs to allocate memory yet cannot use a static buffer? The answer to that question will guide your requirements in managing memory. From there, you can determine which data structure you want to use to manage your memory.

答案取决于为什么需要动态分配内存。系统需要做什么来分配内存却不能使用静态缓冲区?这个问题的答案将指导您管理内存的需求。从这里,您可以确定要使用哪个数据结构来管理内存。

For example, a friend of mine wrote a thing like a video game, which rendered video in scan-lines to the screen. That team determined that memory would be allocated for each scan-line, but there was a specific limit to how many bytes that could be for any given scene. After rendering each scan-line, all the temporary objects allocated during that rendering were freed.

例如,我的一个朋友写了一个类似视频游戏的东西,它将视频以扇形线显示在屏幕上。该团队决定为每条扫描线分配内存,但是对于任何给定场景的字节数都有一个特定的限制。在呈现每个扫描线之后,在呈现期间分配的所有临时对象都被释放。

To avoid the possibility of memory leaks and for performance reasons (this was in the 90's and computers were slower then), they took the following approach: They pre-allocated a buffer which was large enough to satisfy all the allocations for a scan-line, according to the scene parameters which determined the maximum size needed. At the beginning of each scan-line, a global pointer was set to the beginning of the scan line. As each object was allocated from this buffer, the global pointer value was returned, and the pointer was advanced to the next machine-word-aligned position following the allocated amount of bytes. (This alignment padding was including in the original calculation of buffer size, and in the 90's was four bytes but should now be 16 bytes on some machinery.) At the end of each scan-line, the global pointer was reset to the beginning of the buffer.

避免内存泄漏的可能性,由于性能的原因(这是在90年代,电脑慢),他们把以下的方法:他们预先分配一个缓冲区足以满足所有扫描线的分配,根据现场参数决定所需的最大大小。在每条扫描线的开头,一个全局指针被设置为扫描线的开头。当从这个缓冲区中分配每个对象时,将返回全局指针值,并在分配的字节数之后将指针推进到下一个机器字对齐的位置。(这种对齐填充包括在初始的缓冲区大小计算中,在90年代是4字节,但现在在某些机器上应该是16字节)。在每条扫描线的末尾,全局指针被重置到缓冲区的开头。

In "debug" builds, there were two scan buffers, which were protected using virtual memory protection during alternating scan lines. This method detects stale pointers being used from one scan-line to the next.

在“调试”构建中,有两个扫描缓冲区,在交替扫描行期间使用虚拟内存保护进行保护。此方法检测从一个扫描线到下一个扫描线使用的过时指针。

The buffer of scan-line memory may be called a "pool" or "arena" depending on whome you ask. The relevant detail is that this is a very simple data structure which manages memory for a certain task. It is not a general memory manager (or, properly, "free store implementation") such as malloc, which might be what you are asking for.

扫描线内存的缓冲区可以根据您的请求被称为“池”或“竞技场”。相关的细节是,这是一个非常简单的数据结构,用于管理特定任务的内存。它不是一个通用内存管理器(或者,正确地说,“*存储实现”),比如malloc,它可能是您所要求的。

Your application may require a different data structure to keep track of your free storage. What is your application?

应用程序可能需要不同的数据结构来跟踪空闲存储。你的应用程序是什么?

#9


1  

malloc() and its related functions are the only game in town. You can, of course, roll your own memory management system in whatever way you choose.

malloc()及其相关功能是镇上唯一的游戏。当然,您可以以任意方式卷动自己的内存管理系统。

#10


0  

If there are issues allocating dynamic memory from the heap, you can try allocating memory from the stack using alloca(). The usual caveats apply:

如果存在从堆分配动态内存的问题,可以尝试使用alloca()从堆栈分配内存。通常的警告:

  • The memory is gone when you return.
  • 当你回来的时候记忆就消失了。
  • The amount of memory you can allocate is dependent on the maximum size of your stack.
  • 可以分配的内存数量取决于堆栈的最大大小。

#11


0  

You might be interested in: liballoc

你可能对:liballoc感兴趣

It's a simple, easy-to-implement malloc/free/calloc/realloc replacement which works.

这是一个简单、易于实现的malloc/free/calloc/realloc替换,可以工作。

If you know beforehand or can figure out the available memory regions on your device, you can also use their libbmmm to manage these large memory blocks and provide a backing-store for liballoc. They are BSD licensed and free.

如果您事先知道或能够计算出设备上的可用内存区域,您还可以使用它们的libbmmm来管理这些大型内存块,并为liballoc提供一个后台存储。它们是BSD许可和免费的。

#12


0  

FreeRTOS contains 3 examples implementations of memory allocation (including malloc()) to achieve different optimizations and use cases appropriate for small embedded systems (AVR, ARM, etc). See the FreeRTOS manual for more information.

FreeRTOS包含3个内存分配的示例实现(包括malloc())来实现不同的优化和用例适用于小型嵌入式系统(AVR、ARM等)。有关更多信息,请参阅FreeRTOS手册。

I don't see a port for the MPC555, but it shouldn't be difficult to adapt the code to your needs.

我没有看到MPC555的端口,但是根据您的需要调整代码应该不难。

#13


0  

If the library supplied with your compiler does not provide malloc, then it probably has no concept of a heap.

如果使用编译器提供的库没有提供malloc,那么它可能没有堆的概念。

A heap (at least in an OS-less system) is simply an area of memory reserved for dynamic memory allocation. You can reserve such an area simply by creating a suitably sized statically allocated array and then providing an interface to provide contiguous chunks of this array on demand and to manage chunks in use and returned to the heap.

堆(至少在无操作系统中)只是为动态内存分配预留的内存区域。只需创建一个适当大小的静态分配数组,然后提供一个接口,根据需要提供该数组的连续块,并管理使用中的块并返回到堆中,就可以保留这样的区域。

A somewhat neater method is to have the linker allocate the heap from whatever memory remains after stack and static memory allocation. That way the heap is always automatically as large as it possibly can be, allowing you to use all available memory simply. This will require modification of the application's linker script. Linker scripts are specific to the particular toolchain, and invariable somewhat arcane.

某种程度上更整洁的方法是让链接器从堆栈和静态内存分配之后剩下的任何内存中分配堆。这样,堆总是自动地尽可能地大,允许您简单地使用所有可用内存。这需要修改应用程序的链接器脚本。链接器脚本是特定于特定工具链的,并且具有某种神秘的不变性。

K&R included a simple implementation of malloc for example.

例如,K&R包含了malloc的一个简单实现。

#1


18  

Typically malloc() is implemented on Unix using sbrk() or mmap(). (If you use the latter, you want to use the MAP_ANON flag.)

通常malloc()在Unix上使用sbrk()或mmap()实现。(如果使用后者,则需要使用MAP_ANON标志。)

If you're targetting Windows, VirtualAlloc may help. (More or less functionally equivalent to anonymous mmap().)

如果你在测试Windows, VirtualAlloc可能会有所帮助。(在功能上或多或少等同于匿名mmap())。

Update: Didn't realize you weren't running under a full OS, I somehow got the impression instead that this might be a homework assignment running on top of a Unix system or something...

更新:我没有意识到你没有在一个完整的操作系统下运行,但是我得到了这样的印象:这可能是在Unix系统上运行的家庭作业,或者其他什么……

If you are doing embedded work and you don't have a malloc(), I think you should find some memory range that it's OK for you to write on, and write your own malloc(). Or take someone else's.

如果您正在做嵌入式工作,并且没有malloc(),我认为您应该找到一些内存范围,您可以在其中编写自己的malloc()。或者别人的。

Pretty much the standard one that everybody borrows from was written by Doug Lea at SUNY Oswego. For example glibc's malloc is based on this. See: malloc.c, malloc.h.

几乎每个人都借鉴的标准是由SUNY Oswego的Doug Lea所写。例如glibc的malloc就是基于这个。看到:malloc。c,malloc.h。

#2


4  

If your runtime doesn't support malloc, you can find an open source malloc and tweak it to manage a chunk of memory yourself.

如果您的运行时不支持malloc,您可以找到一个开源malloc,并调整它来管理内存块。

#3


4  

You might want to check out Ralph Hempel's Embedded Memory Manager.

您可能想看看Ralph Hempel的嵌入式内存管理器。

#4


3  

malloc() is an abstraction that is use to allow C programs to allocate memory without having to understand details about how memory is actually allocated from the operating system. If you can't use malloc, then you have no choice other than to use whatever facilities for memory allocation that are provided by your operating system.

malloc()是一种抽象,用于允许C程序分配内存,而不必了解如何从操作系统中实际分配内存的细节。如果不能使用malloc,那么只能使用操作系统提供的任何内存分配工具。

If you have no operating system, then you must have full control over the layout of memory. At that point for simple systems the easiest solution is to just make everything static and/or global, for more complex systems, you will want to reserve some portion of memory for a heap allocator and then write (or borrow) some code that use that memory to implement malloc.

如果您没有操作系统,那么您必须完全控制内存的布局。对于简单的系统来说,最简单的解决方法就是让所有的东西都保持静态和/或全局,对于更复杂的系统,您将需要为堆分配器保留一部分内存,然后编写(或借)一些使用该内存来实现malloc的代码。

#5


3  

You should explain why you can't use malloc(), as there might be different solutions for different reasons, and there are several reasons why it might be forbidden or unavailable on small/embedded systems:

您应该解释为什么不能使用malloc(),因为可能由于不同的原因有不同的解决方案,并且有几个原因可以解释为什么在小型/嵌入式系统上禁止或不可使用malloc():

  • concern over memory fragmentation. In this case a set of routines that allocate fixed size memory blocks for one or more pools of memory might be the solution.
  • 担忧的记忆碎片。在本例中,为一个或多个内存池分配固定大小的内存块的一组例程可能是解决方案。
  • the runtime doesn't provide a malloc() - I think most modern toolsets for embedded systems do provide some way to link in a malloc() implementation, but maybe you're using one that doesn't for whatever reason. In that case, using Doug Lea's public domain malloc might be a good choice, but it might be too large for your system (I'm not familiar with the MPC 555 off the top of my head). If that's the case, a very simple, custom malloc() facility might be in order. It's not too hard to write, but make sure you unit test the hell out of uit because it's also easy to get details wrong. For example, I have a set of very small routines that use a brain dead memory allocation strategy using blocks on a free list (the allocator can be compile-time configured for first, best or last fit). I give it an array of char at initialization, and subsequent allocation calls will split free blocks as necessary. It's nowhere near as sophisticated as Lea's malloc(), but it's pretty dang small so for simple uses it'll do the trick.
  • 运行时并没有提供malloc()——我认为嵌入式系统的大多数现代工具集确实提供了一些方法来链接malloc()实现,但是可能您使用的是不出于任何原因的。在这种情况下,使用Doug Lea的公共域malloc可能是一个不错的选择,但是对于您的系统来说,它可能太大了(我对MPC 555不是很熟悉)。如果是这样,那么可能需要一个非常简单的自定义malloc()工具。写起来并不难,但一定要让你的单位测试出因纽特人的原因,因为这样做也很容易出错。例如,我有一组非常小的例程,它们使用空闲列表上的块来使用脑死内存分配策略(分配程序可以为第一、最佳或最后匹配配置编译时)。我在初始化时给它一个char数组,随后的分配调用将根据需要分割空闲块。它远没有Lea的malloc()那么复杂,但是它非常小,所以简单地使用它就可以了。
  • many embedded projects forbid the use of dynamic memory allocation - in this case, you have to live with statically allocated structures
  • 许多嵌入式项目禁止使用动态内存分配——在这种情况下,必须使用静态分配的结构

#6


3  

Write your own. Since your allocator will probably be specialized to a few types of objects, I recommend the Quick Fit scheme developed by Bill Wulf and Charles Weinstock. (I have not been able to find a free copy of this paper, but many people have access to the ACM digital library.) The paper is short, easy to read, and well suited to your problem.

写你自己的。由于您的分配器可能专门用于一些类型的对象,我推荐Bill Wulf和Charles Weinstock开发的Quick Fit方案。(我还没能找到这篇论文的免费拷贝,但很多人都可以使用ACM数字图书馆。)这篇论文篇幅短小,易于阅读,而且很适合你的问题。

If you turn out to need a more general allocator, the best guide I have found on the topic of programming on machines with fixed memory is Donald Knuth's book The Art of Computer Programming, Volume 1. If you want examples, you can find good ones in Don's epic book-length treatment of the source code of TeX, TeX: The Program.

如果你需要一个更通用的分配程序,我找到的关于固定记忆机器编程的最佳指南是Donald Knuth的书《计算机编程的艺术》,第1卷。如果您想要示例,可以在Don对TeX的源代码(TeX: the Program)的史诗般的图书长度处理中找到好的示例。

Finally, the undergraduate textbook by Bryant and O'Hallaron is rather expensive, but it goes through the implementation of malloc in excruciating detail.

最后,Bryant和O'Hallaron写的本科教材非常昂贵,但是它通过malloc的实现进行了极其详细的介绍。

#7


2  

Write your own. Preallocate a big chunk of static RAM, then write some functions to grab and release chunks of it. That's the spirit of what malloc() does, except that it asks the OS to allocate and deallocate memory pages dynamically.

写你自己的。预先分配一大块静态RAM,然后编写一些函数来获取和释放它的块。这就是malloc()所做的工作的本质,只不过它要求操作系统动态地分配和释放内存页。

There are a multitude of ways of keeping track of what is allocated and what is not (bitmaps, used/free linked lists, binary trees, etc.). You should be able to find many references with a few choice Google searches.

有很多方法可以跟踪分配的和不分配的(位图、使用/*链表、二叉树等)。在谷歌搜索中,您应该能够找到许多引用。

#8


2  

An answer really depends on why you might need to dynamically allocate memory. What is the system doing that it needs to allocate memory yet cannot use a static buffer? The answer to that question will guide your requirements in managing memory. From there, you can determine which data structure you want to use to manage your memory.

答案取决于为什么需要动态分配内存。系统需要做什么来分配内存却不能使用静态缓冲区?这个问题的答案将指导您管理内存的需求。从这里,您可以确定要使用哪个数据结构来管理内存。

For example, a friend of mine wrote a thing like a video game, which rendered video in scan-lines to the screen. That team determined that memory would be allocated for each scan-line, but there was a specific limit to how many bytes that could be for any given scene. After rendering each scan-line, all the temporary objects allocated during that rendering were freed.

例如,我的一个朋友写了一个类似视频游戏的东西,它将视频以扇形线显示在屏幕上。该团队决定为每条扫描线分配内存,但是对于任何给定场景的字节数都有一个特定的限制。在呈现每个扫描线之后,在呈现期间分配的所有临时对象都被释放。

To avoid the possibility of memory leaks and for performance reasons (this was in the 90's and computers were slower then), they took the following approach: They pre-allocated a buffer which was large enough to satisfy all the allocations for a scan-line, according to the scene parameters which determined the maximum size needed. At the beginning of each scan-line, a global pointer was set to the beginning of the scan line. As each object was allocated from this buffer, the global pointer value was returned, and the pointer was advanced to the next machine-word-aligned position following the allocated amount of bytes. (This alignment padding was including in the original calculation of buffer size, and in the 90's was four bytes but should now be 16 bytes on some machinery.) At the end of each scan-line, the global pointer was reset to the beginning of the buffer.

避免内存泄漏的可能性,由于性能的原因(这是在90年代,电脑慢),他们把以下的方法:他们预先分配一个缓冲区足以满足所有扫描线的分配,根据现场参数决定所需的最大大小。在每条扫描线的开头,一个全局指针被设置为扫描线的开头。当从这个缓冲区中分配每个对象时,将返回全局指针值,并在分配的字节数之后将指针推进到下一个机器字对齐的位置。(这种对齐填充包括在初始的缓冲区大小计算中,在90年代是4字节,但现在在某些机器上应该是16字节)。在每条扫描线的末尾,全局指针被重置到缓冲区的开头。

In "debug" builds, there were two scan buffers, which were protected using virtual memory protection during alternating scan lines. This method detects stale pointers being used from one scan-line to the next.

在“调试”构建中,有两个扫描缓冲区,在交替扫描行期间使用虚拟内存保护进行保护。此方法检测从一个扫描线到下一个扫描线使用的过时指针。

The buffer of scan-line memory may be called a "pool" or "arena" depending on whome you ask. The relevant detail is that this is a very simple data structure which manages memory for a certain task. It is not a general memory manager (or, properly, "free store implementation") such as malloc, which might be what you are asking for.

扫描线内存的缓冲区可以根据您的请求被称为“池”或“竞技场”。相关的细节是,这是一个非常简单的数据结构,用于管理特定任务的内存。它不是一个通用内存管理器(或者,正确地说,“*存储实现”),比如malloc,它可能是您所要求的。

Your application may require a different data structure to keep track of your free storage. What is your application?

应用程序可能需要不同的数据结构来跟踪空闲存储。你的应用程序是什么?

#9


1  

malloc() and its related functions are the only game in town. You can, of course, roll your own memory management system in whatever way you choose.

malloc()及其相关功能是镇上唯一的游戏。当然,您可以以任意方式卷动自己的内存管理系统。

#10


0  

If there are issues allocating dynamic memory from the heap, you can try allocating memory from the stack using alloca(). The usual caveats apply:

如果存在从堆分配动态内存的问题,可以尝试使用alloca()从堆栈分配内存。通常的警告:

  • The memory is gone when you return.
  • 当你回来的时候记忆就消失了。
  • The amount of memory you can allocate is dependent on the maximum size of your stack.
  • 可以分配的内存数量取决于堆栈的最大大小。

#11


0  

You might be interested in: liballoc

你可能对:liballoc感兴趣

It's a simple, easy-to-implement malloc/free/calloc/realloc replacement which works.

这是一个简单、易于实现的malloc/free/calloc/realloc替换,可以工作。

If you know beforehand or can figure out the available memory regions on your device, you can also use their libbmmm to manage these large memory blocks and provide a backing-store for liballoc. They are BSD licensed and free.

如果您事先知道或能够计算出设备上的可用内存区域,您还可以使用它们的libbmmm来管理这些大型内存块,并为liballoc提供一个后台存储。它们是BSD许可和免费的。

#12


0  

FreeRTOS contains 3 examples implementations of memory allocation (including malloc()) to achieve different optimizations and use cases appropriate for small embedded systems (AVR, ARM, etc). See the FreeRTOS manual for more information.

FreeRTOS包含3个内存分配的示例实现(包括malloc())来实现不同的优化和用例适用于小型嵌入式系统(AVR、ARM等)。有关更多信息,请参阅FreeRTOS手册。

I don't see a port for the MPC555, but it shouldn't be difficult to adapt the code to your needs.

我没有看到MPC555的端口,但是根据您的需要调整代码应该不难。

#13


0  

If the library supplied with your compiler does not provide malloc, then it probably has no concept of a heap.

如果使用编译器提供的库没有提供malloc,那么它可能没有堆的概念。

A heap (at least in an OS-less system) is simply an area of memory reserved for dynamic memory allocation. You can reserve such an area simply by creating a suitably sized statically allocated array and then providing an interface to provide contiguous chunks of this array on demand and to manage chunks in use and returned to the heap.

堆(至少在无操作系统中)只是为动态内存分配预留的内存区域。只需创建一个适当大小的静态分配数组,然后提供一个接口,根据需要提供该数组的连续块,并管理使用中的块并返回到堆中,就可以保留这样的区域。

A somewhat neater method is to have the linker allocate the heap from whatever memory remains after stack and static memory allocation. That way the heap is always automatically as large as it possibly can be, allowing you to use all available memory simply. This will require modification of the application's linker script. Linker scripts are specific to the particular toolchain, and invariable somewhat arcane.

某种程度上更整洁的方法是让链接器从堆栈和静态内存分配之后剩下的任何内存中分配堆。这样,堆总是自动地尽可能地大,允许您简单地使用所有可用内存。这需要修改应用程序的链接器脚本。链接器脚本是特定于特定工具链的,并且具有某种神秘的不变性。

K&R included a simple implementation of malloc for example.

例如,K&R包含了malloc的一个简单实现。