Skip to main content
10 events
when toggle format what by license comment
Nov 16, 2017 at 17:19 comment added ThatAintWorking "it can be dangerous if you don't know the ins and outs of it, but it is useful." pretty much sums up all development in C/C++. :-)
May 20, 2015 at 18:33 comment added StuffAndyMakes @ChrisStratton Right, that chunk of code doesn't check if malloc() works or not. Then again, that chunk of code is using notoriously fat Arduino libraries and routines. :) Thanks for your insight. Very reasonable and valuable.
May 19, 2015 at 17:46 comment added Chris Stratton Dynamically allocating a buffer of only the size needed is risky, as if anything else allocates before you free it you can be left with fragmentation - memory that you can't re-use. Also, dynamic allocation has tracking overhead. Fixed allocation doesn't mean you can't multiply use the memory, it just means that you have to work the sharing into the design of your program. For a buffer with purely local scope, you might also weigh use of the stack. You haven't checked for the possibility of malloc() failing either.
May 19, 2015 at 17:34 comment added StuffAndyMakes @ChrisStratton It's because of the memory constraints that I believe dynamically allocating the buffer of only the size required in the moment is ideal. If the buffer isn't need during regular operation, the memory is available for something else. But, I see what you're saying: Allocate a reserved space so that something else doesn't take away the space you may need for the buffer. All great information and thoughts.
May 19, 2015 at 17:29 comment added StuffAndyMakes @EdgarBonet Yes, exactly. Just wanted to share.
May 18, 2015 at 16:48 comment added Chris Stratton In other words, the problems will come up when you start sharing the processor with other unknown code - which is precisely the problem you think you are avoiding. Generally, if you want something that will always work or else fail during linking, you make a fixed allocation of the maximum size and use it over and over again, for example by having your user pass it in to you in initialization. Remember you are typically running on a chip where everything has to fit in 2048 bytes - maybe more on some boards but also maybe a lot less on others.
May 18, 2015 at 15:33 comment added Edgar Bonet Your test code conforms to the usage pattern 2. Allocate only short-lived buffers I described in my previous answer. This is one of those few usage patterns known to be safe.
May 18, 2015 at 15:04 review Late answers
May 18, 2015 at 16:31
May 18, 2015 at 14:49 review First posts
May 18, 2015 at 22:34
May 18, 2015 at 14:49 history answered StuffAndyMakes CC BY-SA 3.0