473,669 Members | 2,449 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 2345
* 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******** *************@n ewsc.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.hc ltech.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******** *************@n ewsc.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
5176
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
2579
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 than f(n) so that x0 + f(n) == x0. For example, x0 could be 2**300*(1.1234...) while f(n) is 2**100*(1.4256...). If x0 + f(n) == x0 I still sometimes want the addition of f(n) to x0 to have some effect on x0. A good solution seems to be to update...
4
7811
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, 32, 35, 39, 42, 45, 48, 50, 53, 55, 57, 59, 61, 64, 65, 67, 69, 71, 73, 75, 76, 78, 80, 81, 83, 84, 86, 87, 89, 90, 91, 93, 94, 96, 97, 98, 99, 101, 102,
6
10211
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 = xnl; XmlNodeType nodeType = xn.NodeType;
1
1702
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 interface (for daily report generation). I have looked high and low for some samples and/or some help on what references to use, how to even create a report object etc, but to no avail. Can anybody please help me (links to references would be just...
0
1727
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 exception at FillDatatable dr.AcceptChanges, and also the XTraGridControl will not response when click on the column header to do sorting and filtering. What i'm trying to do is very simple, i just want to retrieve a very large table from web service...
6
11880
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, but they are written in C not C++. I know that i am asking too much, but i really need to write the program and send it tomorrow(22.01). I will be very gratefull to those who help me. Here are the codes that I mentioned above: /* This is a...
17
2055
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 a times, I have a stack overflow error due to huge amount of data processing involved, and for this, I go to Options--Linker and increase the MaxStackSize, which sorts out the problem. All of a sudden, I have started to get trouble with one of...
1
1855
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 Dim Tcin As Single, Tc21 As Single, Tc22 As Single, Tc23 As Single, Tc24 As Single, Tc25 As Single Dim Tc26 As Single, Tc27 As Single, Tc28 As Single, Tc29 As Single Dim Th32 As Single, Th33 As Single, Th34 As Single, Th35 As Single, Th36 As...
0
8465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8383
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8894
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8587
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7407
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5682
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2792
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.