3

So far as I know,C++/C doen't support dynamic array on stack . In the following delcaration :

int data[n] ; // if the n is not decided at compiling time ,this leads to error

But recently , I read some other guys code as following:

//**
It seems the n number can not be decided at compling time,but when I run it , if i fprintf the formation, each time i got the correct array size !!!!!!
the G++ version is 4.7.1 
Is this because the G++ 4.7.1 support C++11 x which allow dynamic array?
 **//

#include <cstdio>
#include <algorithm>
using namespace std;

#include <stdio.h>

char s[31];

int Hash()
{
    int sum=0;
    for(int i=0,k=0;k<7;i++)
    {
        if(s[i]>='0'&&s[i]<='9')
        {
            sum*=10;k++;
            sum+=(s[i]-'0');
        }
        else if(s[i]>='A'&&s[i]<'Z')
        {
            sum*=10;k++;
            sum+=((s[i]-'A'-(s[i]>'Q'))/3+2);
        }
    }
    return sum;
}

int main()
{

    int n;scanf("%d",&n);
    int data[n];getchar();
    //fprintf(stderr,"size is %d\n",sizeof(data)/sizeof(data[0]));
    //**
        It seems the n number can not be decided at compling time,but when I run it , if i fprintf the formation, each time i got the correct array size !!!!!!
     *//
    for(int tmp=0;tmp<n;tmp++)
    {
        gets(s);
        data[tmp]=Hash();
    }
    sort(data,data+n);
    bool p=false;n--;
    for(int i=0,num=1;i<n;i+=num=1)
    {
        while(data[i]==data[i+1])
        {
            num++;
            i++;
        }
        if(num>1)
        {
            printf("%03d-%04d %d\n",data[i]/10000,data[i]%10000,num);
            p=true;
        }
    }
    if(!p)printf("No duplicates.\n");
    return 0;
}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
Charlie.cui
  • 63
  • 1
  • 4
  • The confusion usually comes from C++11 §1.1: "C++ is [...] based on the C programming language as specified in ISO/IEC 9899:1999." C99 does support VLAs, but C++11 does not (use gcc's `--pedantic` or `--std=c++11`). **Related**: http://stackoverflow.com/questions/8593643/does-c-support-variable-length-arrays – Andreas Spindler Feb 26 '13 at 09:17
  • In Standards-compliance C++, using a `vector` is typical in this situation. If you *must* use the stack, on many platforms you could use `alloca()` but you'll need to use placement-`new` to initialise the elements then invoke the destructors manually. It's fiddly and error-prone e.g. ensuring destruction of the right set of elements after an exception (that might have been thrown during one of those placement-`new`s) – Tony Delroy Feb 26 '13 at 09:32

2 Answers2

7

These are called variable length arrays (VLA) and are a g++ compiler extension which is not part of the C++ standard. Do not use it in C++ if you want your code to be portable.

You can make g++ emit a warning with the -Wvla compilation flag, or an error with flag -Werror=vla. I usually compile with -pedantic-errors, which catches this and many other deviations from the standard.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

C and C++ are different languages. One example of the differences between the languages is that C does support dynamic arrays on the stack (though a stack isn't really required by either C or C++). They're called VLAs (variable length arrays): http://en.wikipedia.org/wiki/Variable-length_array

g++ may have an extension that allows this feature in C++, but that extension isn't required for all C++ implementations.

autistic
  • 1
  • 3
  • 35
  • 80