User assertion
assert
statement fails
Description
This check determines whether the argument to an assert
macro
is true.
The argument to the assert
macro must be
true when the macro executes. Otherwise the program aborts and prints
an error message. Polyspace® models this behavior by treating
a failed assert
statement as a run-time error.
This check allows you to detect failed assert
statements
before program execution.
Diagnosing This Check
Examples
Red assert
on Array Index
#include<stdio.h>
#define size 20
int getArrayElement();
void initialize(int* array) {
for(int i=0;i<size;i++)
array[i] = getArrayElement();
}
void printElement(int* array,int index) {
assert(index < size);
printf("%d", array[index]);
}
int getIndex() {
int i = size;
return i;
}
void main() {
int array[size];
int index;
initialize(array);
index = getIndex();
printElement(array,index);
}
In this example, the assert
statement in printElement
causes
program abort if index >= size
. The assert
statement
makes sure that the array index is not outside array bounds. If the
code does not contain exceptional situations, the assert
statement
must be green. In this example, getIndex
returns
an index equal to size
. Therefore the assert
statement
appears red.
assert
failureWhen an assert
statement is red, investigate
the cause of the exceptional situation. In this example, one possible
correction is to force getIndex
to return an index
equal to size-1
.
#include<stdio.h> #define size 20 int getArrayElement(); void initialize(int* array) { for(int i=0;i<size;i++) array[i] = getArrayElement(); } void printElement(int* array,int index) { assert(index < size); printf("%d", array[index]); } int getIndex() { int i = size; return (i-1); } void main() { int array[size]; int index; initialize(array); index = getIndex(); printElement(array,index); }
Orange assert
on malloc
Return Value
#include <stdlib.h>
void initialize(int*);
int getNumberOfElements();
void main() {
int numberOfElements, *myArray;
numberOfElements = getNumberOfElements();
myArray = (int*)malloc(numberOfElements);
assert(myArray!=NULL);
initialize(myArray);
}
In this example, malloc
can return NULL
to myArray
.
Therefore, myArray
can have two possible values:
myArray == NULL
: Theassert
condition is false.myArray != NULL
: Theassert
condition is true.
Combining these two cases, the User assertion check
on the assert
statement is orange. After the orange assert
, Polyspace considers
that myArray
is not equal to NULL
.
NULL
One possible correction is to write a customized function myMalloc
where
you always check the return value
of malloc
for NULL
.
#include <stdio.h>
#include <stdlib.h>
void initialize(int*);
int getNumberOfElements();
void myMalloc(int **ptr, int num) {
*ptr = (int*)malloc(num);
if(*ptr==NULL) {
printf("Memory allocation error");
exit(1);
}
}
void main() {
int numberOfElements, *myArray=NULL;
numberOfElements = getNumberOfElements();
myMalloc(&myArray,numberOfElements);
assert(myArray!=NULL);
initialize(myArray);
}
Imposing Constraint Through Orange assert
#include<stdio.h> #include<math.h> float getNumber(); void squareRootOfDifference(float firstNumber, float secondNumber) { assert(firstNumber > secondNumber); if(firstNumber > 0 && secondNumber > 0) printf("Square root = %.2f",sqrt(firstNumber-secondNumber)); } void main() { double firstNumber = getNumber(), secondNumber = getNumber(); squareRootOfDifference(firstNumber,secondNumber); }
In this example, the assert
statement in squareRootOfDifference()
causes
program abort if firstNumber
is less than secondNumber
.
Because Polyspace does not have enough information about firstNumber
and secondNumber
,
the assert
is orange.
Following the assert
, all
execution paths that cause assertion failure terminate. Therefore,
following the assert
, Polyspace considers
that firstNumber >= secondNumber
. The Invalid
use of standard library routine check on sqrt
is
green.
Use assert
statements to help Polyspace determine:
Relationships between variables
Constraints on variable ranges
Check Information
Group: Other |
Language: C | C++ |
Acronym: ASRT |
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)