Kevin T. answered 01/09/25
Mathematics & Programming for all!
1. Are those 500000 bytes safely allocated with the code I posted?
Yes, the 500000 bytes are allocated by Simulink when you set the output port width using ssSetOutputPortWidth(S, 0, 500000)
.
2. Are those 500000 bytes contiguous?
Yes, Simulink ensures that the memory allocated for output ports is contiguous. When you call ssGetOutputPortSignal(S, 0)
, the pointer returned points to a contiguous block of memory that is large enough to hold the specified number of elements (500000 in this case).
3. Is it safe/legit to fill in the output array like this? Is there a better way of doing this?
You can write into the buffer by treating it as an array:uint8_T *payload = (uint8_T*) ssGetOutputPortSignal(S, 0);
However, given that your system crashes when accessing positions beyond ~128k, there might be issues with:
- Memory limitations on your embedded system: Check if the system has enough memory to handle a contiguous block of 500kB. Many embedded systems have constraints on heap or stack sizes that could cause crashes when large arrays are allocated.
- Compiler or build settings: Ensure that your toolchain and runtime environment allow for sufficient memory allocation for large arrays.
- Execution environment constraints: Embedded systems may have limitations on the size of memory blocks that can be allocated contiguously.
Recommendations:
A. Check memory availability:
Verify the available memory on your embedded system and ensure it is sufficient to allocate 500kB for the output port. You might need to reconfigure your system's memory allocation (e.g., increasing heap size).
B. Reduce output buffer size:
If memory is constrained, consider breaking the buffer into smaller chunks and process them in parts. For example, use multiple output ports or split the operation into smaller steps.