Can someone gives me an example of deterministic bug in a program?
Thanks.
Can someone gives me an example of deterministic bug in a program?
Thanks.
void bug()
{
int *ptr = 0;
ptr[0] = 10;
}
Accessing a null pointer. Always happens.
Here's a deterministic bug:
void myStrCpy (char *s, char *d) {
for (int i = 0; i < strlen (s) - 1; i++)
*d++ = s[i];
*d = '\0';
}
It very obviously forgets to copy the final character of the string meaning the copy is one character less.
For one with an assignment statement, you could use:
#include <stdio.h>
int main (void) {
short x = 2094967295;
printf ("%d\n", x);
return 0;
}
which outputs -22017
consistently.
Or even a little snippet from a recent question on SO:
int x = 2^5; // set x to 32 for bitmasking later.
That's a bug, albeit a user error one.
Or even:
int *x = malloc (4);
in an application that's supposed to be portable.