why the code about array can be compiled?

A

andrew

Hi:
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;
}
 
S

Sumit Rajan

andrew said:
Hi:
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];



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.
return 0;
}

Regards,
Sumit.
 
T

Thomas J. Gritzan

andrew said:
Hi:
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;
}

The new C standard (C99) lets you have variable sized arrays. GCC allows it
for C++ as an extension.
 
S

Sumit Rajan

I'm sorry I missed out reading the above.

#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];



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.
return 0;
}


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.

Regards,
Sumit.
 
J

James Kanze

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;
}

Try it with the usual options required to make g++ a C++
compiler:
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>
 
L

Lionel B

[...]

Try it with the usual options required to make g++ a C++ compiler:
g++ -std=c++98 -pedantic

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...

[...]
 
J

James Kanze

Try it with the usual options required to make g++ a C++ compiler:
g++ -std=c++98 -pedantic
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...

I think the idea is that "-ansi" is a bit outdated. (It's also
a bit US-centric. The international standard for C++ is ISO,
not ANSI.) It says you want to be conform to an ANSI standard,
but it doesn't say which one. Currently, g++ only supports one
ISO C++ standard, so it doesn't make much difference, but I
imagine that you'd get different behavior for a C program
depending on whether you specified -std=c90 or -std=c99, and
presumably, the same thing will hold once g++ supports
-std=c++03. I don't know which one -ansi gives for C, and I
don't know which one it will give in the future for C++.

As I read in somebody's .sig once: "The nice thing about
standards is that there are so many of them to choose from."
GCC's use of -std=xxx, rather than -ansi, is just a recognition
of this fact.
 
L

Lionel B

Try it with the usual options required to make g++ a C++ compiler:
g++ -std=c++98 -pedantic
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...

I think the idea is that "-ansi" is a bit outdated. (It's also a bit
US-centric. The international standard for C++ is ISO, not ANSI.) It
says you want to be conform to an ANSI standard, but it doesn't say
which one. Currently, g++ only supports one ISO C++ standard, so it
doesn't make much difference, but I imagine that you'd get different
behavior for a C program depending on whether you specified -std=c90 or
-std=c99, and presumably, the same thing will hold once g++ supports
-std=c++03. I don't know which one -ansi gives for C, and I don't know
which one it will give in the future for C++.

As I read in somebody's .sig once: "The nice thing about standards is
that there are so many of them to choose from." GCC's use of -std=xxx,
rather than -ansi, is just a recognition of this fact.

Thanks for the clarification,
 
M

Marcus Kwok

James Kanze said:
(VC++ requires "cl /vmg /GR /EHs /D_CRT_SECURE_NO_DEPRECATE",
for example.)

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
measure.
 
D

Diego Martins

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
measure.

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
 
M

Marcus Kwok

Please don't quote signatures. I went ahead and snipped it for you.
This may be off-topic, but I can't compile anything using windows.h
when using /Za :-(

Well, if you're relying on stuff in windows.h, then you are probably no
longer in the realm of Standard C++ anyway :)
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top