3 ms·
>If there’s any risk that nmemb is too large, it must be guarded. [...] However, if median is expected to safely accommodate COPY_MAX elements, it may as well a
by michaelt 7y ago
>If there’s any risk that nmemb is too large, it must be guarded. [...] However, if median is expected to safely accommodate COPY_MAX elements, it may as well always allocate an array of this size. If it can’t, then that’s not a safe maximum.
Et voilà, a library function that could have been portable between the smallest microcontroller and the largest workstation becomes restricted to a single application.
- theamk 7y agoUm, no? That COPY_MAX would still need a different value between small MCU and full-blown PC. Not to mention that using stack allocation on low-memory device is extremely dangerous... at least with malloc you get a nice diagnostic, while alloca will just do random program damage.
- bdisnainc 7y agoIt does not do random damage, you get a segmentation fault in most systems. In really small embedded you should not be allocating anything meaningful in the stack anyway.
- Gibbon1 7y agoIn small embedded things are either statically allocated or allocated on the stack. There is no heap.
- lonelappde 7y agoIsn't that like choosing your poison? If you have a low memory device, you need to statically guarantee safety and reject oversized inputs. Crashing your embedded device may work if your are a server with a client that knows to retry another backend, but isn't an elegant solution if it disables your car's brakes.