Computing > Programming

How to change a buffer from main RAM (DATA or BSS) to the heap?

<< < (5/5)

dunkemhigh:
That's quite a long post, but well worth reading through in its entirety  :-+

I would like to say, though, that you might use this for dynamically allocating buffers. Perhaps when some comms function starts up you need some buffers, and then when it's closed down you don't. Instead of having them wasting resources you grab them when you need them and lose them when you don't.

SiliconWizard:
All this is nice but probably a bit too much in this context. The main point here was about understanding what arrays are in C, which, for some reason, is something that many "beginners" have trouble with. Probably because this a base type that is not consistent with most other base types. In particular, you can't assign an array to an array, or return an array from a function. Another factor is that an array identifier essentially acts as a const pointer, except for the sizeof operator.

Nominal Animal:

--- Quote from: dunkemhigh on June 01, 2021, 05:45:34 pm ---I would like to say, though, that you might use this for dynamically allocating buffers.
--- End quote ---
That's exactly what I suggested in my first post in this thread, above.  I even used the name "buffer" there, instead of ucHeap.

Typical example I can think of is (re)using a buffer for reading a configuration file off SD card at bootup, and later on for buffering commands/data between the host and the microcontroller.  The latter one is logically an asymmetric pair of buffers (usually receive side larger than send side), but they are not needed yet when parsing the configuration file.  Instead of trying to juggle them all in one "heap array", it is better to dynamically allocate the exact structures one needs, then free them when no longer needed; and document the dynamic memory use well.  When strictly non-overlapping, there is no risk of memory fragmentation either.

Or, a firmware could be able to communicate with a host via any one of UART, SPI, or I2C.  (As an example, consider an intelligent display module.)  The configuration could be done at run time, either via a selector pin or by dynamic detection.  If various buffers are needed, their relative sizes can be chosen at run time to not exceed supportable limits (keeping enough free memory on a microcontroller for stack); with more generous buffers used when the situation allows.


--- Quote from: SiliconWizard on June 01, 2021, 06:26:32 pm ---All this is nice but probably a bit too much in this context.
--- End quote ---
True!  Not remembering what those hurdles are, severely hinders my ability to stay on track and be useful.  :-[

It does not help that the web is full of crappy examples, tutorials, and even libraries, that "kinda work" (at least as long as you have the exact same development environment, versions, and hardware as their writer), but are definitely not good examples.  And it is too aggravating to wade through tons of them, to find the good ones... I always feel a pang of guilt when asked, and I cannot really point to any.

Navigation

[0] Message Index

[*] Previous page

There was an error while thanking
Thanking...
Go to full version