2

The following code compiles fine on Linux using gcc -std=c99 but gets the following errors on the Visual Studio 2010 C compiler:

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

fib.c
fib.c(42) : error C2057: expected constant expression
fib.c(42) : error C2466: cannot allocate an array of constant size 0
fib.c(42) : error C2133: 'num' : unknown size

The user inputs the amount of Fibonacci numbers to generate. I'm curious as to why the Microsoft compiler doesn't like this code.

#include <stdlib.h>
#include <stdio.h>
#include <limits.h>

void fib(int max);

int main(int argc, char *argv[])
{    
    int argument;

    if (argc != 2)
    {
        puts("You must supply exactly one command line argument.");
        return 0;
    }

    argument = atoi(argv[1]);

    if (argument == 0)
    {
        puts("You gave us 0 or an invalid integer.");
        return 0;
    }
    else if (argument < 0)
    {
        puts("You gave us a negative integer.");
        return 0;
    }
    else if (argument == INT_MAX)
    {
        puts("You gave us a number that's too big to fit in an integer.");
        return 0;
    }

    printf("%d\n", argument);
    fib(argument);
    return 0;
}

void fib(int max)
{
    int num[max]; /// <- Line 42

    int i;
    for (i = 0; i < max; i++)
    {    
        if (i == 0)
            num[i] = 0;
        else if (i == 1)
            num[i] = 1;
        else
            num[i] = num[i-1] + num[i-2];

        printf("%d\t%d\n", i, num[i]);
    }
}
1

3 Answers 3

7
void fib(int max)
{
    int num[max];

Microsoft's C compiler doesn't support C99, and I believe they've said it never will. That means that arrays can only be declared with constant size.

3
  • Using alloca would be as close to VLA as you can get.
    – moshbear
    Nov 16, 2011 at 3:39
  • @moshbear: Of course alloca is non-standard; does Microsoft support it? alloca() shares one serious flaw with VLAs: it has no mechanism to report whether the allocation succeeded. Dec 17, 2011 at 21:15
  • The good: no need to free(). The bad: Only defined in BSD/GNU (no POSIX or ISO). The ugly: UB if you allocate too much. Also, MSVC defines it as _alloca (no need to rename, a #define in malloc.h does that for you).
    – moshbear
    Dec 17, 2011 at 22:34
4

The Problem lies in the fib function.

The line "int num[max];" is the problem. This is because, compiler tries to allocate space of max number of integers, but the token max is not defined properly to the compiler at the compilation time.

You can use the dynamic memory allocation to resolve this issue.

But i wonder why you might need such huge space (when max large) as you need only previous numbers to generate the sequence.

void fib(int max)
{

    int a = 0, b = 1; // first 2 numbers of the sequence.
    int c, i;
    for (i = 0; i < max; i++)
    {    
        if (i == 0)
            printf ("%d %d",i,a);
        else if (i == 1)
            printf ("%d %d",i,b); 
        else{
            c = a+b;
            printf ("%d %d",i,c);
            a = b;
            b = c;    
        }
    }
}
3

You can change your function to dynamically allocate the array and then release the memory when finished. The rest of your function will work without change.

void fib(int max)
{
    int *num = malloc(max * sizeof(int));

    int i;
    for (i = 0; i < max; i++)
    {
        /* Your code here */
    }
    free(num);
}
2
  • Thank you! Is there a reason why some people still haven't switched to the C99 standard yet? According to Wikipedia, C99 has been out for about 10 years?
    – justin
    Nov 16, 2011 at 4:24
  • @Justin - it causes some incompatibilites with C++. MSFT claim that their's is a C++ compiler and that's it's main market. See stackoverflow.com/questions/146381/… Nov 16, 2011 at 4:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.