Today, I looked more carefully into Rule 1.
Guidelines for C Programming
Rule 1
Restrict to simple control flow constructs.
Do not use
1. goto statements,
2. setjmp or longjmp constructs,
3. direct or indirect recursion.
Simpler control flow is easy to verify and improves code clarity.
The advise NOT TO USE direct or indirect recursion is perhaps the biggest surprise here. Function call graph is acyclic without recursion and cyclic with recursion.This way , all exections that should be bounded are in fact bounded.
Important note : Poorly written code does not suddenly become reliable if we just omit these language constructs from it. But, it is true that reliable code is almost always written without them.
Gotos first got a bad name when Edsger Dijkstra published his letter in the Communications of the ACM in 1968 "Goto statement considered harmful". This letter sparked what may well be one of the most heated debates in software engineering in the last
few decades.
There is a lot of solid code around that does have goto statements. Of course, most of that code is not written to control safety critical systems.
If someones life is at stake, we should do anything we can to make sure we stay far from any potential danger zone, real or imagined. Because gotos can make the code structure a little more complex than it has to be, the wise thing to do is to find another way to structure it.
Setjmp, longjmp follow with Very much the same reasoning.Fortunately, they are very rarely used, so it is not a grave restriction.
Recursion: Avoiding recursion has a different justification. Safety critical software typically runs as embedded software on resource-constrained systems, with little opportunity for
user intervention if something goes wrong. Recursion is an elegant programming paradigm based on the mathematical notion of induction.
I have to understand the above points in greater detail and look more closely as to why recursion is to be avoided. Because, we know that recursive code implie lesser memory space and lesser memory space occupying code is attractive for embedded system programmers - then why should we use recursive code ? Got it ?
More on these points tomorrow....
Today , I give another code in C for which there is a small prize for the first right answer
-------------------------------------------
main ()
{
double a[2][3] ;
printf(" %d ",sizeof(a)) ;
printf(" %d ",sizeof(a[1])) ;
printf(" %d ",sizeof(a[1][1])) ;
}
Q. What is the output of this program ?
-------------------------------------------
Have a nice day
ADVISORY TO STUDENTS (Answer to "Sir, College is there today ?")
Quote :
Previous Posts
Tuesday, June 08, 2010
Subscribe to:
Post Comments (Atom)
The Out put of above program is 48 24 8
ReplyDeleteExp:
main ()
{
double a[2][3] ;
printf(" %d ",sizeof(a)) ;
/*sizeof(a)=2*3*8(since double =8 Bytes)=48*/
printf(" %d ",sizeof(a[1])) ;
/*sizeof(a[1])=1*3*8=24*/
printf(" %d ",sizeof(a[1][1])) ;
/*sizeof(a[1][1])=8*/
}
Well done Soumen !
ReplyDelete48 24 8 is the right answer.
Get in touch for your prize.