Page 209 - DCAP305_PRINCIPLES_OF_SOFTWARE_ENGINEERING
P. 209
Unit 10: Coding Standards
String m_pName; Notes
…
};
10.1 Common Errors
Software errors (we will use the terms errors, defects and bugs interchange-ably in our discussion
here;) are a reality that all programmers have to deal with. Much of effort in developing software
goes in identifying and removing bugs. There are various practices that can reduce the occurrence
of bugs, but regardless of the tools or methods we use, bugs are going to occur in programs.
Though errors can occur in a wide variety of ways, some types of errors are found more
commonly. Here we give a list of some of the commonly occurring bugs. The main purpose of
discussing them is to educate programmers about these mistakes so that they can avoid them.
char* foo(int e)
{
char *output;
if (s>0)
output (char*) malloc (size);
if (s==1) return NULL; /* if s==1 then mem leaked
return(output);
}
Similar to NULL dereference is the error of accessing uninitialized memory. This often occurs
if data is initialized in most cases, but some cases do not get covered, they were not expected.
An example of this error is:
switch( i )
{
case 0: s=OBJECT_1; break;
case 1: s=OBJECT_2:break;
}
return (s); /* s not initialized for values other than 0 or 1 */
Lack of Unique Addresses
Aliasing creates many problems, and among them is violation of unique addresses when we
expect different addresses. For example in the string concatenation function, we expect source
and destination addresses to be different. If this is not the case, as is the situation in the code
segment, it can lead to runtime errors.
strcat(src, destn);
/* In above function, if src is aliased to destn,
* then we may get a runtime error */
Write a program reduce the common error.
LOVELY PROFESSIONAL UNIVERSITY 203