Re:The Power of Ten – Rules for Developing Safety Critical Code
by lordnacho on Thursday July 14, 2016 @08:55AM
(#12093302)
How does that fixed upper bound on loops work? If there's an array of dynamic size that needs to be looped over, how do you do that?
[ Parent
| on HN ]
Re:The Power of Ten – Rules for Developing Safety Critical Code
by gernerz on Thursday July 14, 2016 @09:03AM
(#12093347)
I cant speak for NASA, but in general if you are writing extremely safety critical code you simply don't use dynamically sized things. In addition to safety, the dynamically sized objects cause issues for the real-time constraints on these system as well.
Re:The Power of Ten – Rules for Developing Safety Critical Code
by nabla9 on Thursday July 14, 2016 @11:41AM
(#12094762)
There are no dynamically sized arrays. All memory is allocated at startup and that's it.
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
}
}
Re:The Power of Ten – Rules for Developing Safety Critical Code
by robmccoll on Thursday July 14, 2016 @09:03AM
(#12093348)
#define MAX_NUM_OBJS 100
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.
Re:The Power of Ten – Rules for Developing Safety Critical Code
by lordnacho on Thursday July 14, 2016 @09:51AM
(#12093740)
There might not be a valid object at every location in the preallocated memory.
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?
Re:The Power of Ten – Rules for Developing Safety Critical Code
by robmccoll on Thursday July 14, 2016 @12:22PM
(#12095141)
That's a good point. If you do have a situation where the number of objects is somewhat dynamic, you could use layers of protection such as (re)initializing the unused objects to a known safe state, adding a flag as you mentioned, adding sentinel values to the end, and at the very least you do have the guarantee that you aren't running off into memory that was actually dedicated to another purpose and contains fundamentally different data. If you are doing something really dynamic, you could embed two linked lists (or an embedded list and a free index stack or even two packed stacks). The reason for stacks over lists is that lists can accidentally become cycles.
Re:The Power of Ten – Rules for Developing Safety Critical Code
by hackits on Thursday July 14, 2016 @09:09AM
(#12093393)
My take on it is. If have a task that can only allocate `once` a area of memory for initialization, then you can use the allocation size to put a constraint on the iterators based on the datatype size.
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.
}
Re:The Power of Ten – Rules for Developing Safety Critical Code
by mapmap on Friday July 15, 2016 @01:07AM
(#12099041)
Agreed, this puzzled me as well. I also have thought it was curious that we didn't get dynamically sized arrays in C until C99. Before that you had to malloc to the heap. It makes a little more sense now though having read this.