开发者

how can we determine our program well working?

开发者 https://www.devze.com 2023-02-08 19:18 出处:网络
Can we determine if a program is well working by considering the following questions? Memory is succesfully reclaimed by the Operating system

Can we determine if a program is well working by considering the following questions?

  1. Memory is succesfully reclaimed by the Operating system
  2. There is no Stack Overflow

after writing and running some code to determine whether there is any overflowing going on?

Our assistant give us feedback after running our program through some other program.

In the feedback, it shows whether if there was an overflow , whether we were using using free / delete in wrong way, and 开发者_Go百科some other things.

I wonder How did she found out ?


You say that person has the source code - that means he can analyze the program - either by reading and thinking or by using some clever program.

For example he could see the following:

void someCleverFunction()
{
   char* buffer = new char[100];
   if( someCondition ) {
       //do something with the buffer
       delete[] buffer;
       return;
   }
   Sleep( 1000 );
}

and then it's obvious to him that once someCondition isn't met delete[] is not called and that's a sure memory leak.

That's called static code analysis.


There could be a number of ways to deduce the above.

  1. Use a tool such as valgrind : this helps detect memory leaks and such

  2. A simulator which would be creating inputs to your programs for both positive and negative testcases.

Cheers!


I would define the "well working" as the effectiveness of the program: does it do as was asked and does it do that in all situations? This should be handled be 1) clearly describing what the program should do and 2) test all situations as thoroughly as possible.

However buffer overruns, memory leaks and stack overflows are part of "well behaving" which also means robustness of the program: there should be no cases in which the program crashes: here thorough testing is also needed, but code reviewing and tools as static analysis (e.g. coverity) and dynamic analysis (e.g. valgrind) should be used too since issues with memory leaks can be unnoticed until much later when you actually run out of memory. Which tools to use depend on your platform and on the scale of the program.


Do you use Visual C++? If so, read this: http://msdn.microsoft.com/en-us/library/5at7yxcs(VS.71).aspx

On linux with gcc use Valgrind. It has a really good documentation.

0

精彩评论

暂无评论...
验证码 换一张
取 消