C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

compiling difference b/w G++ and VC++

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Arun Kumar
Guest





PostPosted: Mon Sep 15, 2003 9:53 pm    Post subject: compiling difference b/w G++ and VC++ Reply with quote



hello,
The following C Prog is reporting different messages while
compiling with G++ and Microsoft Visual C++ compiler.

#include<iostream.h>
int main()
{
int n;
cin >> n;
int a[n];
}

The following errors were reported after compiling in VC++

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'a' : unknown size

But G++ didn't report any error or warnings.

Thus above C++ program (compiled by G++, running on Linux) is
supporting runtime array memory allocation.

But VC++ on windows doesn't allow the above feature. Why ??????


Arun Kumar. S

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Alf P. Steinbach
Guest





PostPosted: Tue Sep 16, 2003 6:39 am    Post subject: Re: compiling difference b/w G++ and VC++ Reply with quote



On 15 Sep 2003 17:53:12 -0400, [email]smsabu2002 (AT) yahoo (DOT) com[/email] (Arun Kumar) wrote:

Quote:
The following C Prog is reporting different messages while
compiling with G++ and Microsoft Visual C++ compiler.

#include<iostream.h

Assuming corrected program of


#include using namespace std;

Quote:
int main()
{
int n;
cin >> n;
int a[n];
}

The following errors were reported after compiling in VC++

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'a' : unknown size

But G++ didn't report any error or warnings.

Thus above C++ program (compiled by G++, running on Linux) is
supporting runtime array memory allocation.

But VC++ on windows doesn't allow the above feature. Why ??????

Because dynamic arrays are not part of C++.

They're part of C99, if I'm not terribly mistaken.

With g++ try compiling with pedantic and language specification.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Bo Persson
Guest





PostPosted: Tue Sep 16, 2003 6:40 am    Post subject: Re: compiling difference b/w G++ and VC++ Reply with quote




"Arun Kumar" <smsabu2002 (AT) yahoo (DOT) com> skrev i meddelandet
news:84afc8e.0309150639.799f0a3f (AT) posting (DOT) google.com...
Quote:
hello,
The following C Prog is reporting different messages while
compiling with G++ and Microsoft Visual C++ compiler.

#include<iostream.h
int main()
{
int n;
cin >> n;
int a[n];
}

The following errors were reported after compiling in VC++

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'a' : unknown size

But G++ didn't report any error or warnings.

Thus above C++ program (compiled by G++, running on Linux) is
supporting runtime array memory allocation.

But VC++ on windows doesn't allow the above feature. Why ??????

Because this is a C99 feature supported by G++ (as an extension). It
is not part of standard C++.


Bo Persson


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Tutone
Guest





PostPosted: Tue Sep 16, 2003 6:40 am    Post subject: Re: compiling difference b/w G++ and VC++ Reply with quote


"Arun Kumar" wrote:
Quote:
The following C Prog
is reporting different messages while
compiling with G++ and Microsoft Visual C++
compiler.

#include<iostream.h
int main()
{
int n;
cin >> n;
int a[n];
}

The following errors were reported after compiling
in VC++

error C2057: expected constant expression
error C2466: cannot allocate an array of constant
size 0
error C2133: 'a' : unknown size

But G++ didn't report any error or warnings.

Presumably because you didn't turn on the warnings
when you compiled.

You don't mention what version of gcc you are using.
But please keep in mind that gcc does not turn on
warnings by default. A good habit is to include the
following command line options when you compile with
gcc:

-Wall -W -pedantic

When I compile your program with gcc 3.3 and the above
command line options, I get the following three
warnings:

In file included from
c:/djgpp/lang/cxx/3.3/backward/iostream.h:31,
from test.cpp:1:
c:/djgpp/lang/cxx/3.3/backward/backward_warning.h:32:2:
warning: #warning This file includes at least one
deprecated or antiquated header. Please consider using
one of the 32 headers found in section 17.4.1.2 of
the C++ standard. Examples include substituting the
<X> header for the <X.h> header for C++ includes, or
<sstream> instead of the deprecated header
<strstream.h>. To disable this warning u
se -Wno-deprecated.

(This warning results from your mistakenly using
<iostream.h>, which is not a standard header, instead
of <iostream>.)

test.cpp: In function `int main()':
test.cpp:6: warning: ISO C++ forbids variable-size
array `a'

(This warns you that you are using a gcc extension,
and that standard C++ does not permit variable-sized
arrays.)

test.cpp:6: warning: unused variable `int a[((n - 1) +
1)]'

(This warns you that you declare a variable, a, but
never use it.)

Quote:
Thus above C++ program (compiled by G++, running on
Linux) is
supporting runtime array memory allocation.

Yes, as noted above, that's a gcc extension - it's not
part of standard C++. Hence the warning. Please
consult the gcc documentation for more details.

Quote:
But VC++ on windows doesn't allow the above
feature. Why ??????

Because it's not standard C++. And neither is
<iostream.h>.

Best regards,

Tom

















__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ron Natalie
Guest





PostPosted: Tue Sep 16, 2003 6:40 am    Post subject: Re: compiling difference b/w G++ and VC++ Reply with quote


"Arun Kumar" <smsabu2002 (AT) yahoo (DOT) com> wrote

Quote:
hello,
The following C Prog is reporting different messages while
compiling with G++ and Microsoft Visual C++ compiler.

The program is non-standard (as are your compilers).

Quote:

#include

This is not a standard header.

Quote:
int a[n];

This is not allowed in C++ (currently). The size of an array must
be a constant expression. Use a vector if you want a variable
array.

Quote:
Thus above C++ program (compiled by G++, running on Linux) is
supporting runtime array memory allocation.

But VC++ on windows doesn't allow the above feature. Why ??????

VC++ is doing the right thing. The dynamic array length is an G++ extension.
Try using a CURRENT version of G++ or turn on the -pedantic-errors flag on
older ones to identify these insidious extensions.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
llewelly
Guest





PostPosted: Tue Sep 16, 2003 6:46 am    Post subject: Re: compiling difference b/w G++ and VC++ Reply with quote

[email]smsabu2002 (AT) yahoo (DOT) com[/email] (Arun Kumar) writes:

Quote:
hello,
The following C Prog is reporting different messages while
compiling with G++ and Microsoft Visual C++ compiler.

#include<iostream.h
int main()
{
int n;
cin >> n;
int a[n];
}

The following errors were reported after compiling in VC++

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'a' : unknown size

But G++ didn't report any error or warnings.
[snip]


Try using -pedantic:
$ g++ -pedantic arun.cc
arun.cc: In function `int main()':
arun.cc:6: warning: ISO C++ forbids variable-size array `a'


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jack Klein
Guest





PostPosted: Tue Sep 16, 2003 6:47 am    Post subject: Re: compiling difference b/w G++ and VC++ Reply with quote

On 15 Sep 2003 17:53:12 -0400, [email]smsabu2002 (AT) yahoo (DOT) com[/email] (Arun Kumar) wrote
in comp.lang.c++.moderated:

Quote:
hello,
The following C Prog is reporting different messages while
compiling with G++ and Microsoft Visual C++ compiler.

#include<iostream.h
int main()
{
int n;
cin >> n;
int a[n];
}

The following errors were reported after compiling in VC++

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'a' : unknown size

But G++ didn't report any error or warnings.

Thus above C++ program (compiled by G++, running on Linux) is
supporting runtime array memory allocation.

But VC++ on windows doesn't allow the above feature. Why ??????


Arun Kumar. S

Because in this particular case, Visual C++ is being more standard
conforming than the version of g++ you are using. This is a g++
extension that is not part of the C++ language at all.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ulrich Eckhardt
Guest





PostPosted: Tue Sep 16, 2003 7:06 am    Post subject: Re: compiling difference b/w G++ and VC++ Reply with quote

Arun Kumar wrote:
Quote:
#include<iostream.h

You might consider writing a not-so-ancient tongue af C++ ... get ideas
about which are good books at accu.org.

Quote:
int main()
{
int n;
cin >> n;
int a[n];

GCC accept arrays with a not-compile-time size as an extension. Use alloca
or new/malloc in combination with a smart pointer.

Uli

--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Daniel Spangenberg
Guest





PostPosted: Tue Sep 16, 2003 7:08 am    Post subject: Re: compiling difference b/w G++ and VC++ Reply with quote

Hello, Arun Kumar!

Arun Kumar schrieb:

Quote:
hello,
The following C Prog is reporting different messages while

This is neither a Standard C program (C programs don't know of a
<iostream.h> header)...

Quote:
compiling with G++ and Microsoft Visual C++ compiler.

#include<iostream.h
int main()
{
int n;
cin >> n;
int a[n];
}

The following errors were reported after compiling in VC++

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'a' : unknown size

But G++ didn't report any error or warnings.

Thus above C++ program (compiled by G++, running on Linux) is
supporting runtime array memory allocation.

But VC++ on windows doesn't allow the above feature. Why ??????

... nor a valid C++ program. Reasons are:

1) <iostream.h> is not a valid C++ header, normally you should use
<iostream>.
<iostream.h> was part of the "old", pre-standard C++ library. The only
reason for possibly using this header is in cases, where the compiler is
not
aware of or does not provide compliant standard headers.

2) Everything from a C++ library which is **pure** C++ is contained in
the
namespace std. Thus you have to qualify cin by std::cin.

3) Current C++98 as well as C89 do not provide variable-length C arrays.

All C arrays need a integral constant expression which describes the
dimensions
of this array. Dynamic or variable-length C arrays are part of C99 (But
even C99
does not help for the points (1) and (2)).

Greetings from Bremen,

Daniel Spangenberg




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Marco Manfredini
Guest





PostPosted: Wed Sep 17, 2003 9:36 am    Post subject: Re: compiling difference b/w G++ and VC++ Reply with quote

Arun Kumar wrote:

Quote:
#include<iostream.h
int main()
{
int n;
cin >> n;
int a[n];
}

The following errors were reported after compiling in VC++

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'a' : unknown size

But G++ didn't report any error or warnings.

Current versions g++ of implement a substantial subset of the new C9x
standard in both the C and the C++ compiler - it actually compiles a
kind of C9x++. Declaring an array with runtime size is allowed
according to this standard. A nice roundup is here:

http://home.tiscalinet.ch/t_wolf/tw/c/c9x_changes.html

You should be able to select language standard with the -std= switch and
-pedantic-errors.

Marco

--
The text above is a result of a bug in my newsreader and I take no
responsibility for this text appearing in my post.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
André Pönitz
Guest





PostPosted: Fri Sep 19, 2003 10:12 am    Post subject: Re: compiling difference b/w G++ and VC++ Reply with quote

In comp.lang.c++.moderated Jack Klein <jackklein (AT) spamcop (DOT) net> wrote:
Quote:
Because in this particular case, Visual C++ is being more standard
conforming than the version of g++ you are using. This is a g++
extension that is not part of the C++ language at all.

g++ -pedantic-errors says:

ISO C++ forbids variable-size array `a'

So g++ is well aware that this is an extension and informs you if you
wish to be informed...

Andre'

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ron Natalie
Guest





PostPosted: Sat Sep 20, 2003 9:58 am    Post subject: Re: compiling difference b/w G++ and VC++ Reply with quote


"André Pönitz" <poenitz (AT) gmx (DOT) net> wrote


Quote:

So g++ is well aware that this is an extension and informs you if you
wish to be informed...

And if you use a CURRENT version of G++, you don't even have to add
options. The pedantic warnings are on by default.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.