Thread: Is this standard?

  1. #1
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132

    Is this standard?

    This is a new one for me. Visual Studio 2008 does not accept creating an array with a non-const size, but g++ (3.4.4 on cygwin) accepts it. Is Microsoft behind the standard?

    Code:
    #include <stdlib.h>
    int main(int argc, char* argv[])
    {
      int i = atoi(argv[1]);
      char c[i];
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's C99, not C++.

  3. #3
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Thanks.
    Are the languages diverging so that C may end up with features not in C++?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Are the languages diverging so that C may end up with features not in C++?
    Only a tiny bit.

    C++0x FAQ - see the comment about VLAs.
    (Note that this page was last modified today, so I assume it's up-to-date.)

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    It's a GNU extension for C++.

    cyberfish@cyberfish-desktop:/tmp$ g++ -pedantic a.cpp
    a.cpp: In function ‘int main(int, char**)’:
    a.cpp:5: error: ISO C++ forbids variable length array ‘c’
    cyberfish@cyberfish-desktop:/tmp$ gcc -v
    Using built-in specs.
    Target: i486-linux-gnu
    Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.3.3-5ubuntu4' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
    Thread model: posix
    gcc version 4.3.3 (Ubuntu 4.3.3-5ubuntu4)

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    don't think C supports that either. Mostly because whether to allocate at runtime or at compile time is not part of the standard, and so would be implimentation dependent. Some applications may not support dynamic memory allocation (its part of the libraries not the language itself). I know there are some microcontrollers where this would be an difficult issue.

    try this instead -

    Code:
    #include <stdlib.h>
    
    int main(int argc, char* argv[]){
    	int i = atoi(argv[1]);
    	char* c = (char*)malloc(sizeof(char) * i);
    
    	return 0;
    	}
    Last edited by abachler; 08-31-2009 at 09:15 PM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by abachler View Post
    don't think C supports that either.
    And yet, it does. Granted C99 is needed not C89.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Standard efficiency
    By Jorl17 in forum C Programming
    Replies: 3
    Last Post: 06-18-2009, 11:48 AM
  2. array initialization & C standard
    By jim mcnamara in forum C Programming
    Replies: 2
    Last Post: 09-12-2008, 03:25 PM
  3. standard for comparing
    By chrismiceli in forum C Programming
    Replies: 2
    Last Post: 08-09-2003, 05:27 PM
  4. C/C++ standard
    By confuted in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-06-2003, 03:22 AM
  5. standard language, standard compiler?
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-03-2001, 04:21 AM