Is this a valid C++ program that will not crash on any machine?
#include <iostream>
using namespace std;
int main( void ) {
int i;
cin >i;
double X[i];
X[i-1] = 1123;
cout << X[i-1] << endl;
return 0;
}
What does the standard say about defining and initializing arrays of
length
non-const? Is it illegal? Or is that legal in C++?
Thanks,
--j 13 2259
John posted:
What does the standard say about defining and initializing arrays of
length
non-const? Is it illegal? Or is that legal in C++?
According to the current C++ Standard, the length of an array must be a
compile-time constant.
Many compilers provide an extension whereby the programmer can define an
array using a runtime value.
If you're writing portable code, use "new" or "vector" or "malloc" instead.
--
Frederick Gotham
John wrote:
Is this a valid C++ program that will not crash on any machine?
#include <iostream>
using namespace std;
int main( void ) {
int i;
cin >i;
double X[i];
X[i-1] = 1123;
cout << X[i-1] << endl;
return 0;
}
What does the standard say about defining and initializing arrays of
length
non-const? Is it illegal? Or is that legal in C++?
Did you try it even on one machine before posting?
Thanks,
--j
S S posted:
Did you try it even on one machine before posting?
Most compilers will compile it no problem -- if they're not in Standard C++
mode, that is.
--
Frederick Gotham
John wrote:
What does the standard say about defining and initializing arrays of
length non-const? Is it illegal? Or is that legal in C++?
It would be legal in C, but it's illegal in C++.
"John" <we**********@y ahoo.comwrote in message
news:11******** **************@ m7g2000cwm.goog legroups.com...
Is this a valid C++ program that will not crash on any machine?
Well, I've seen a few machines that had a tendency to crash, regardless of
what you ran on them. :-) Of course, that's not what you're really
asking...
#include <iostream>
using namespace std;
int main( void ) {
int i;
cin >i;
double X[i];
X[i-1] = 1123;
cout << X[i-1] << endl;
return 0;
}
What does the standard say about defining and initializing arrays of
length
non-const? Is it illegal? Or is that legal in C++?
The standard says that the array size has to be a compile-time constant.
The above code should not compile (and if it does, then that's a compiler
extension, and is non-standard).
If you need to specify the size at run-time, you can define X as a double*,
and dynamically allocate using new[] (and later delete[] to clean up).
Better still, use std::vector!
-Howard
John wrote:
Is this a valid C++ program that will not crash on any machine?
#include <iostream>
using namespace std;
int main( void ) {
int i;
cin >i;
double X[i];
X[i-1] = 1123;
cout << X[i-1] << endl;
return 0;
}
What does the standard say about defining and initializing arrays of
length
non-const? Is it illegal? Or is that legal in C++?
Thanks,
--j
It needs to be a constant. Think about it for a second. How can the
compiler possibly guess as to how much memory it needs to reserve ?
Rolf Magnus posted:
John wrote:
>What does the standard say about defining and initializing arrays of length non-const? Is it illegal? Or is that legal in C++?
It would be legal in C, but it's illegal in C++.
Only in C99, which has yet to take the place of C89.
--
Frederick Gotham am******@gmail. com wrote:
....
It needs to be a constant. Think about it for a second. How can the
compiler possibly guess as to how much memory it needs to reserve ?
Some compilers actually implement that feature as an extension so your
question obviously has an answer. The C99 standard actually allows this
and it is one way in which an array can be dynamically allocated.
If you need to specify the size at run-time, you can define X as a double*,
and dynamically allocate using new[] (and later delete[] to clean up).
Better still, use std::vector!
-Howard
Don't mix up new and delete syntax!
int num;
cout<< "Enter a number: ";
cin >num;
int *i = new int[num];
// do something
// if the pointer i was not an dynamically allocated array, you can use
this
// delete i;
// however it IS an array (probably), so you have to do this
delete []i;
Just thought I'd clarify in case the OP didn't know about the syntax,
odd as it is. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: zeroSpamNotISaytype |
last post by:
This question has just occurred to me - but if I declare an array of
numeric variables(eg int goats, double *wasps=new double), are
the values of it's elements automatically set to zero? If so, is there
any way to save program time by stopping this happening? (and if yes,
I can remove a good few unnecessary loops)
Thanks,
James M.
|
by: Mantorok Redgormor |
last post by:
Is this legal?
int foo = { 0 };
gcc gives:
foo.c: In function `main':
foo.c:5: warning: missing braces around initializer
foo.c:5: warning: (near initialization for `foo')
foo.c:5: warning: unused variable `foo'
|
by: mdh |
last post by:
I am still early on with K&R, but every now and again, I try to assign
to an array like this:
char arr="I am a string"; /* works*/
but
char arr; /* MAX == some number */
char arr="I am a string"; / * Does not work */
|
by: selam |
last post by:
I have this assignment using c++ please help me in writing a code.
Suppose you have a main ( ) with three local arrays, all the same size and type (say float). The first two are already initialized to values. Write a function called add arrays ( ) that accepts the addresses of the three arrays as arguments; add the contents of the first two...
|
by: Peter Oliphant |
last post by:
I'm using VC++ 2005 Express in /cli mode, and I'm trying to initialize an
array of integers. Something like:
array<int int_array ;
int_array = gcnew array<int>(3) ;
int_array = { 0x3, 0x9, 0x7 } ; // error here
How is this done? Thanx in advance...
| |
by: javelin |
last post by:
I'm defining several arrays in the following format:
$farmanimals = array('farmanimal1' ='horse', 'farmanimal2' ='cow',
'farmanimal3' ='chicken')
$housepets = array('housepet1' ='dog', 'housepet2' ='cat',
'housepet3' ='bird')
$zooanimals = array('zooanimal1' ='monkey', 'zooanimal2' ='lion',
'zooanimal3' ='zebra', 'zooanimal4' ='giraffe')...
|
by: sanjay.vasudevan |
last post by:
Why are the following declarations invalid in C?
int f();
int f();
It would be great if anyone could also explain the design decision
for such a language restricton.
Regards,
Sanjay
|
by: =?Utf-8?B?SGVucmlrIFNjaG1pZA==?= |
last post by:
Hi,
consider the attached code.
Serializing the multi-dimensional array takes about 36s
vs. 0.36s for the single-dimensional array.
Initializing the multi-dimensional array takes about 4s
vs. 0.3s for the single-dimensional array.
(I know initializing is not necessary in this simple example,
|
by: Edward Jensen |
last post by:
Hi,
I have the following static arrays of different size in a class:
in header:
static double w2, x2;
static double w3, x3;
static double w4, x4;
in GaussLegendre.cpp:
|
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...
|
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...
| |
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. ...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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...
|
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...
|
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...
|
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...
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |