0

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

different output for the same input from command line

7
  • 6
    Your peak function has no checks to avoid running off the end of the array. Commented Feb 19, 2021 at 22:22
  • 4
    Also you should generally not define functions in .h files. The .h file should only contain the declaration int peak(int *, int); and the code itself should go in a separate .c file which also includes the .h file, and which you compile separately and link together. Commented Feb 19, 2021 at 22:25
  • 1
    Regarding .c and .h files, stackoverflow.com/questions/5904530/… might be good reading. Commented Feb 19, 2021 at 22:30
  • 1
    Accessing outside the array bounds causes undefined behavior. That's why you can get different results. Commented Feb 19, 2021 at 22:34
  • 1
    see also: c - Do I cast the result of malloc? - Stack Overflow Commented Feb 19, 2021 at 22:48

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.