By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,641 Members | 2,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,641 IT Pros & Developers. It's quick & easy.

Defining and initializing arrays

P: n/a
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

Sep 20 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
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
Sep 20 '06 #2

P: n/a
S S

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
Sep 20 '06 #3

P: n/a
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
Sep 20 '06 #4

P: n/a
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++.

Sep 20 '06 #5

P: n/a

"John" <we**********@yahoo.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.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
Sep 20 '06 #6

P: n/a

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 ?

Sep 20 '06 #7

P: n/a
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
Sep 20 '06 #8

P: n/a
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.
Sep 20 '06 #9

P: n/a
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.

Sep 20 '06 #10

P: n/a
Frederick Gotham wrote:
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.
It did when it was released. There is no C89 anymore. In standard speak:

"This second edition cancels and replaces the first edition, ISO/IEC
9899:1990, as amended and corrected by ISO/IEC 9899/COR1:1994, ISO/IEC
9899/AMD1:1995, and ISO/IEC 9899/COR2:1996."

It's a shame that so many compilers still don't even try to be standard
compliant, considering that C89 ceased to be a standard 7 years ago. When I
talk about "C" without any further notice, I talk about C99.
Sep 21 '06 #11

P: n/a

"twomers" <tw*****@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
>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!
Huh? I didn't!
>
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;
Probably? Looks like an array to me.
>
Just thought I'd clarify in case the OP didn't know about the syntax,
odd as it is.
Seems more confusing to me, now. I explicitly described using new[] and
delete[]. Why introduce the other syntax?

-Howard

Sep 21 '06 #12

P: n/a
Rolf Magnus posted:
>Only in C99, which has yet to take the place of C89.

It did when it was released. There is no C89 anymore. In standard speak:

"This second edition cancels and replaces the first edition, ISO/IEC
9899:1990, as amended and corrected by ISO/IEC 9899/COR1:1994, ISO/IEC
9899/AMD1:1995, and ISO/IEC 9899/COR2:1996."

Ask any decent C programmer which Standard they're working off.

It's a shame that so many compilers still don't even try to be standard
compliant, considering that C89 ceased to be a standard 7 years ago.

It is very much still a Standard and has a great many followers.

When I talk about "C" without any further notice, I talk about C99.

Over on comp.lang.c, C refers to C89.

--

Frederick Gotham
Sep 21 '06 #13

P: n/a
In article <11**********************@m7g2000cwm.googlegroups. com>,
we**********@yahoo.com says...

[ ... ]
What does the standard say about defining and initializing arrays of
length non-const? Is it illegal? Or is that legal in C++?
It is not allowed in C++. Rather than directly using new[] and delete[]
as has been mentioned elsethread, you normally want to use std::vector:

#include <iostream>
#include <vector>

int main() {
int i;
cin >i;
std::vector<doublex(i);
x[i-1] = 1123;

std::cout << x[i-1] << std::endl;
return 0;
}

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 24 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.