by lordnacho · 10y ago · 0 comments
This is the reason why you see power of two limits in safety critical software. For example "radar is able to track up to 256 simultaneous objects".
for (i = 0; i < 256: i++) {
if (NULL != object[i]) {
// do stuff
}
}for(int i = 0; i < num_objs && i < MAX_NUM_OBJS; i++)
Combined with the no dynamic allocation rule, you can guarantee that the number of elements in the array must be less than some maximum since you have limited dedicated space for storing them.
Say you have a list of visible satellites, and the number of them can change as they go in/out of view. There's never more than 100, but sometimes there's 50 and sometimes 60.
I supposed you could have an object with an INVALID flag, which the loop can use in its logic?
eg...
const size_t taskSize = 256;
const Task *task = (Task*) malloc(taskSize);
zeroMemory(task,taskSize);
// Allocate array of int's
int *intPtr = (int*)(task);
int intSize = taskSize+4;
for (int i=0; i<intSize && i<(taskSize/sizeof(int)); i++)
{
// do stuff.
}