Reputation: 15
How to detect memory leak in third party C code by static analysis(without using any tool). Like how do we verify that the allocated memory has been freed without using any tools?
Upvotes: 0
Views: 631
Reputation: 17364
Talking in absolute terms, "you can't".
How can you spot a leak in this code (it has no sense, it's only to make you understand). If the users pass 1 as the command line parameters, the code will not leak. However, if he pass 2...
int main(int argc, const char * argv[]) {
//insert code here...
int numberOfLoops = atoi(argv[1]);
int i = 0;
void *ptr;
for (i = 0; i <= numberOfLoops; i++) {
ptr = malloc(sizeof(int));
printf("loop\n");
}
free(ptr);
return 0;
}
Upvotes: 2
Reputation: 399793
If you're not going to be using any tool, then of course all you can do is read the code, and think about how it executes.
Not using any tools for this is a very strange restriction, of course.
Upvotes: 3