Everyone,
I wrote this trivial program in minutes to see how well it would come out. I produces the correct answers. But I don't know how to plug the memory leak Splint found. Would you please tell me how to solve the problem.
If you think I caught OCD, you're. right. I'd hate to hear Splint complain about anything. That's why I assign function values to integer variables when most programmers wouldn't do that.
Code:
#define MAXNUMS 30#include <stdio.h>
#include <string.h>
#include <stdbool.h>
static bool between(const unsigned int low, const unsigned int high, const unsigned int value)
{
return value >= low && value <= high;
}
static void print_list(unsigned int list[], const unsigned int length)
{
register unsigned int place;
int n;
n = puts("Fibonacci numbers");
for (place = 0; place < length; ++place)
n = printf("%u ", list[place]);
n = putchar('\n');
}
static unsigned int *fibonacci_numbers(unsigned int numbers[], const unsigned int n)
{
register unsigned int place;
numbers[0] = 0;
numbers[1] = 1;
for (place = 2; place <= n; ++place)
numbers[place] = numbers[place-1] + numbers[place-2];
return numbers;
}
int main(void)
{
int n;
unsigned int count;
unsigned int numbers[MAXNUMS];
n = puts("How many Fibonacci numbers should I generate?");
n = scanf("%u", &count);
if (!between(1, MAXNUMS, count))
printf("%u is not betweem 1 and %u.\n", count, MAXNUMS);
else
print_list(fibonacci_numbers(numbers, count), count);
return 0;
}