473,508 Members | 2,207 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

int x[N]

Hi.
Is the following code alright?

#include <iostream>
using namespace std;

int main(){
int N;
cin>>N;
int x[N];
cout<<x;
}

A friend told me this not alrigh, stack v.s. heap allocation, yada yada. We
are still arguing about this, so I turn to you, whom has more knowledge
about this. Is alright to do or not?
With GCC it is alright, not with visual studio.
What is your opinion on this?
Oct 18 '05 #1
7 2331
* Gunnar G:

Is the following code alright?

#include <iostream>
using namespace std;

int main(){
int N;
Don't use all-uppercase names except for macros, where you should use them.

cin>>N;
int x[N];
Supported in C99, not in C++: tell g++ to be conforming and it'll report an
error.

cout<<x;
Outputs the address of x.
}


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 18 '05 #2

"Gunnar G" <de****@comhem.se> wrote in message
news:E9*********************@newsc.telia.net...
Hi.
Is the following code alright?

#include <iostream>
using namespace std;

int main(){
int N;
cin>>N;
Strictly speaking, this should not compile: int x[N];

Are you sure this(below) is what you want to do? cout<<x;


Regards,
Sumit.
--
Sumit Rajan <su****@msdc.hcltech.com>
Oct 18 '05 #3
Gunnar G wrote:
Is the following code alright?
Not in Standard C++.
#include <iostream>
using namespace std;

int main(){
int N;
cin>>N;
int x[N];
cout<<x;
}

A friend told me this not alrigh, stack v.s. heap allocation, yada yada. We
are still arguing about this, so I turn to you, whom has more knowledge
about this. Is alright to do or not?
With GCC it is alright, not with visual studio.
G++ (not GCC) has it as a language extension. VC++ doesn't.
What is your opinion on this?


My opinion is simple: do it if you don't care to be portable. If you know
that it's an extension and not a Standard C++ feature, and can understand
it when it stops compiling under another compiler, what's the big deal?

V
Oct 18 '05 #4

"Gunnar G" <de****@comhem.se> wrote in message
news:E9*********************@newsc.telia.net...
Hi.
Is the following code alright?
No.

#include <iostream>
using namespace std;

int main(){
int N;
cin>>N;
int x[N];
array size have to be constant expression, but
vector<int> x(N);
works ok
cout<<x;
}

A friend told me this not alrigh, stack v.s. heap allocation, yada yada.
We
are still arguing about this, so I turn to you, whom has more knowledge
about this. Is alright to do or not?
It's not alright, use vector.
With GCC it is alright, not with visual studio.
What is your opinion on this?

GCC imported everything that C99 has (and some more), into C++ . :)
Try compiling with -ansi for portable programs

Greetings, Bane.
Oct 18 '05 #5
> Not in Standard C++.
Then I'll stay away from it for the sake of portability.
Thank you for your answers.
Oct 18 '05 #6
In article <E9*********************@newsc.telia.net>,
Gunnar G <de****@comhem.se> wrote:
Is the following code alright?

#include <iostream>
using namespace std;

int main(){
int N;
cin>>N;
int x[N];
cout<<x;
}

A friend told me this not alrigh, stack v.s. heap allocation, yada yada. We
are still arguing about this, so I turn to you, whom has more knowledge
about this. Is alright to do or not?
With GCC it is alright, not with visual studio.
What is your opinion on this?


I don't have an opinion, but I know that Standard C++ doesn't
yet allow it. Intead you'll need maybe to use new int[M]
or vector<int>, with other appropriate changes as necessary.
If you compiler g++ with -Wall -ansi -pedantic you should get
more noise out of it.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 18 '05 #7
Gunnar G wrote:
Is the following code alright?

#include <iostream>
using namespace std;

int main(){
int N;
cin>>N;
int x[N];
cout<<x;
}

A friend told me this not alright, stack v.s. heap allocation, yada yada.
We are still arguing about this
so I turn to you, who have more knowledge about this.
Is it alright to do or not?
With GCC it is alright, not with visual studio.
What is your opinion on this? cat main.cc #include <iostream>

int main(int argc, char* argv[]) {
size_t n;
std::cin >> n;
int x[n];

for (size_t j = 0; j < n; ++j)
x[j] = j;
for (size_t j = 0; j < n; ++j)
std::cout << x[j] << std::endl;
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc main.cc: In function `int main(int, char**)':
main.cc:6: error: ISO C++ forbids variable-size array `x' ./main ./main: Command not found. g++ -Wall -o main main.cc
./main

3
0
1
2

The new C++ standard
will almost certainly adopt C99 variable-size arrays.
The GNU C++ compiler anticipates the new standard
and implements variable-sized arrays as an extension.
So they are certainly "alright"
and you certainly *should* use them
if you can be sure that every target platform will support them.
Oct 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5167
by: The Clansman | last post by:
Hi, how do I get the path of an XML node? I have: Dim xmlNo As XmlNode = xml.SelectSingleNode("//Category") I want to get the path of the xmlNo: i.e.
7
2564
by: Daniel Vallstrom | last post by:
I am having trouble with floating point addition because of the limited accuracy of floating types. Consider e.g. x0 += f(n) where x0 and f are of some floating type. Sometimes x0 is much larger...
4
7807
by: Case - | last post by:
I came across the following integer sqrt code. What do you think about this in terms of efficiency and C std? Thanks for listening, Kees static const int sqrttable = { 0, 16, 22, 27, ...
6
10207
by: Hans Kamp | last post by:
My program fails reading XML attributes. A fragment of the code is: private void showXmlNodeAtTreeNode(XmlNodeList xnl, TreeNode tn) { int i; for (i = 0; i < xnl.Count; i++) { XmlNode xn =...
1
1692
by: Matt M | last post by:
Hey... I'm trying to run a crystal report (8.5) from a c# windows forms application class. I can't use the report viewer as the application will be running these reports from a command line...
0
1723
by: JC Voon | last post by:
Hi All: I'm new in Threading and Web Services, can someone please verify my code, i'm not sure whether this is the correct way, althought it is partially work, but some time it will raise...
6
11871
by: Cappo | last post by:
Hello, I just started programming in C++ and i have a problem with a task.I need to write a program which illustrates the Bisection method in C++. I have some codes, which use the bisection code,...
17
2037
by: vj | last post by:
Hi All, I am using C++Builder V5.0 to run my C programs. I am not an expert in C. Normally, I run the C program with these steps: 'Compile', 'Make Project', 'Build Project' and then 'Run'. Many...
1
1841
by: lugo | last post by:
Private Sub Command1_Click() Dim Thin As Single, Th12 As Single, Th13 As Single, Th14 As Single, Th15 As Single Dim Th16 As Single, Th17 As Single, Th18 As Single, Th19 As Single, Th110 As Single...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7132
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7401
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7063
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5640
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4720
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
432
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.