Debug code
Debug c
Uses of debug c
Debug code's
It is recommended as a best practice that debugging code be removed from production versions of applications, as it can slow them down.[1] However some games leave these commands and cheats available for the players to use as a way to enhance their play experience. For example, the PC version of Skyrim allows the player access to the command console, giving them the ability to modify certain aspects of their game as it is being run. These commands include giving the player invincibility, teleportation and unlimited gold.[2]
Examples of
void TestFunction(int timesToRun) {
cout << "the algorithm should run " << times
// run algorithm
algorithm();
// debug print statement
cout << "algorithm run " << i++ << " times." << std::endl;
}
}
There is a bug in the above code. On an input of 5 the program should print the following to the console.
the algorithm should run 5 times algorithm run 1 times. algorithm run 2 times. algorithm run 3 times. algorithm run 4 times. algorithm run 5 times.
The actual output is the following, which is incorrect.
the algorithm should run 5 times algorithm run 1 times. algorithm run 2 times. algorithm run 3 times. algorithm run 4 times. algorithm run 5 times. algorithm run 6 times.
Our function is running through the algorithm an extra time, and upon closer inspection it is clear that our loop is coded wrong.
C example
int i, a[10];
for (i = 0; i < 10; ++i)
{
a[i] = 10-i;
}
for (i = 0; i < 10; ++i)
{
a[a[i]] = a[i];
}
The above code will cause has an out of bounds error which can lead to some unexpected results. The code can be written in a safer way, using assertions, as shown below.
#include <assert.h>
int i, a[10];
for (i = 0; i < 10; ++i)
{
assert(0 <= i && i < 10);
a[i] = 10-i;
}
for (i = 0; i < 10; ++i)
{
assert(0 <= i && i < 10);
assert(0 <= a[i] && a[i] < 10);
a[a[i]] = a[i];
}
- ^ "Archived copy". Archived from the original on 2010-04-02. Retrieved 2010-03-26.
{{cite web}}
: CS1 maint: archived copy as title (link) - ^ http://www.pcgamer.com/2011/11/16/skyrim-console-commands-let-you-cheat-and-do-other-stuff/