Defining and initializing arrays

J

John

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;

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
 
F

Frederick Gotham

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.
 
S

S S

John said:
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;

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?
 
R

Rolf Magnus

John said:
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++.
 
F

Frederick Gotham

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.
 
H

Howard

John said:
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;

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
 
A

amparikh

John said:
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;

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 ?
 
G

Gianni Mariani

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.
 
T

twomers

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.
 
R

Rolf Magnus

Frederick said:
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 ï¬rst 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.
 
H

Howard

twomers said:
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
 
F

Frederick Gotham

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

"This second edition cancels and replaces the ï¬rst 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.
 
J

Jerry Coffin

[ ... ]
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<double> x(i);
x[i-1] = 1123;

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top