why the code about array can be compiled?

Discussion in 'C++' started by andrew, Jul 4, 2007.

  1. andrew

    andrew Guest

    I use "g++ array.cpp" on freebsd, surprised, the code can be compiled
    #include <iostream>
    using namespace std;
    int getInt()
    int a = 10;
    int b = 2;
    return a * b;
    int main()
    int a = getInt();
    int b[a][a];
    return 0;
    andrew, Jul 4, 2007
    1. Advertisements

  2. andrew

    Sumit Rajan Guest

    Why don't you try to compile it and see what the compiler says?

    The number of elements in an array needs to be a constant expression.
    Something like:

    int a = 5;
    int arr[a];

    will not compile.

    However, you could change it to something like:

    const int a = 5;
    int arr[a];

    Or you could use a std::vector in case you need variable bounds.
    Sumit Rajan, Jul 4, 2007
    1. Advertisements

  3. GCC C extensions http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC75

    Neelesh Bodas, Jul 4, 2007
  4. The new C standard (C99) lets you have variable sized arrays. GCC allows it
    for C++ as an extension.
    Thomas J. Gritzan, Jul 4, 2007
  5. andrew

    Sumit Rajan Guest

    I'm sorry I missed out reading the above.

    And hence, I misunderstood your post as being one where you were
    wondering why it did *not* compile. :)

    Once again, my apologies. I need to be more careful next time.

    Sumit Rajan, Jul 4, 2007
  6. andrew

    James Kanze Guest

    Try it with the usual options required to make g++ a C++
    g++ -std=c++98 -pedantic
    Like every other "C++" compiler I know, g++ does not compile C++
    by default; you have to give it special options for it to do so.
    (VC++ requires "cl /vmg /GR /EHs /D_CRT_SECURE_NO_DEPRECATE",
    for example.)

    <pet peave>
    Why don't any introductory texts even mention this? The
    very first thing you do when using a compiler (or any tool,
    for that matter) for the first time is carefully read the
    documentation, and decide what you want and need from it,
    and how to get it. I've been programming in C++ for well
    over 15 years, with 10 years of C before that, and in all
    that time, I've never seen a compiler where the
    defaults---just invoking g++, or cl, or whatever---did
    anything useful. (Typically, you'll not only want standard
    compliance, but various warnings and runtime checks
    activated. The compiler invocations from my makefiles tend
    to require several hundreds of characters.)
    </pet peave>
    James Kanze, Jul 4, 2007
  7. andrew

    Lionel B Guest

    Interesting... I've always used:

    g++ -ansi -pedantic

    Even after RTFM I'm still none the wiser as to what precisely the
    difference (if any) is...

    Lionel B, Jul 4, 2007
  8. andrew

    James Kanze Guest

    James Kanze, Jul 5, 2007
  9. andrew

    Lionel B Guest

    Lionel B, Jul 5, 2007
  10. andrew

    Marcus Kwok Guest

    Don't forget /Za, to disable extensions. Also, I think it is the
    default in VS2005, but I also throw in /Zc:forScope,wchar_t for good
    Marcus Kwok, Jul 5, 2007
  11. This may be off-topic, but I can't compile anything using windows.h
    when using /Za :-(
    I discovered that when I tried to use /Za in order to remove an
    auto_ptr extension which holds a potential resource leak :(
    (I started a thread talking about this issue, last year)

    Diego Martins, Jul 5, 2007
  12. andrew

    Marcus Kwok Guest

    Please don't quote signatures. I went ahead and snipped it for you.
    Well, if you're relying on stuff in windows.h, then you are probably no
    longer in the realm of Standard C++ anyway :)
    Marcus Kwok, Jul 5, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.