The checking logic would be refactored into an aspect file, as follows:
after(void * s) : (call($ malloc(...)) || call($ calloc(...)) || call($ realloc(...)))
&& result(s) {
char * result = (char *)(s);
if (result == NULL) {
/* routine to handle the case when memory allocation fails */
}
}
Now, the core program looks as follows:
... int *x ; x = (int *)malloc(sizeof(int) * 4); <--- dynamic memory allocation /* routine for handling the normal case */ ...
A Reusable Aspect for Memory Allocation Checking
原文:http://www.cnblogs.com/xiaohuihui123/p/4563753.html