Reputation: 31
I wrote a C program in Clion, using dynamic memory allocation. I noticed sometimes gave different output for the same input. At first I tought there was a problem with Clion (I was using it for the first time), but I also ran the program from the windows command line, and the same bug occured.
main.c
#include "main.h"
int main()
{
int n;
scanf("%d", &n);
int *t = (int*)calloc(n, sizeof(int));
for (int i = 0; i < n; ++i) {
scanf("%d", &t[i]);
}
int p = peak(t, n);
printf( p == -1 ? "No peak" : "Peak %d, position %d", t[p], p );
free(t);
return 0;
}
main.h
//
// Created by Botond on 2021. 02. 18..
//
#ifndef EXTRA_MAIN_H
#define EXTRA_MAIN_H
#include <stdio.h>
#include <stdlib.h>
int peak( int *t, int n)
{
int p = -1, i = 0;
while(t[i] <= t[i+1])
i++;
p = i;
while(t[i] >= t[i+1])
i++;
if(i == n + 1)
return p;
else
return -1;
}
#endif //EXTRA_MAIN_H
Upvotes: 0
Views: 540
Reputation: 57922
When the array passed to peak
is {5,5,4,3,2,1}
:
while(t[i] <= t[i+1])
i++;
will continue until i == 1
. Then
while(t[i] >= t[i+1])
i++;
will still be true when i == 4
, so the loop will iterate again and the test becomes t[5] >= t[6]
. Since the array only has 6 elements (t[0]..t[5]
), the access to t[6]
causes undefined behavior. In particular, it could read whatever garbage value happens to be next in memory, so that your program behaves as if the input contained additional garbage values that might be different from one run to the next. The program could also crash or cause other sorts of problems.
You need an extra condition in these loops that checks i
against n
, so that they don't access t[n]
and instead terminate when i+1 >= n
.
Upvotes: 2