C/C++ Dumb question about complex arrays in c++

AI Thread Summary
The discussion centers on defining and initializing complex arrays in C++, particularly for someone transitioning from FORTRAN. The initial focus is on creating a one-dimensional array of complex numbers using dynamic memory allocation with the syntax "Complex *array = new Complex[n];". A participant clarifies the correct syntax, emphasizing the need for a pointer. The conversation then shifts to two-dimensional arrays, with a suggestion that creating them involves allocating an array of arrays, which can complicate the code, especially for higher dimensions. It is noted that for simple data types like integers or doubles, straightforward allocation suffices. An alternative approach is proposed: using a vector of vectors, which simplifies the process. The participant expresses optimism about adapting their old FORTRAN codes to C++ with the help of community insights.
Messages
2,600
Reaction score
791
In trying to learn C++ (I'm an old FORTRAN guy), I am unable to figure out how to define and initialize a complex array. Single complex numbers are straight forward, but going further has me perplexed some.

Thanks in advance.
 
Technology news on Phys.org
I assume you have a Complex class.

Complex *array = new Complex [n];

I think that should work. "new" is a keyword, "array" is the variable name for the array, and "n" would be an integer variable, of course you can put in your own number if you want, e.g. replace "n" with "5".

EDITED based on chroot's comment.
 
Last edited:
Not quite AKG. You forgot the star:

Compex *array = new Complex[n];

- Warren
 
Thanks, how about a 2d array?

complex **array = new complex [n][n]; ?

and maybe an n-d array, I use tensors in 3-d and 4-d in my old fortran codes.
 
Using dynamic memory allocation for multi dimensional arrays isn't as trivial as you think. You actually have to create an array of arrays. You'll first need to allocate a single dimensional array. Then using a loop allocate an array within each cell. You can get some pretty nasty code especially with higher dimesions. Believe it or not, this is one thing you probable want to stick with fortran to do.
 
You only need to do as dduardo suggested if you are using non-trivial data types. If you're just making a multi-dimensional array of ints or doubles, etc., you don't need to do anything special besides allocating the memory with the new operator.

- Warren
 
I was afraid of that. I'll have to mess around with it...

Thanks

dt
 
Could always make a vector of vectors; that's relatively painless.
 
I think I got it figured out, as Hurkyl and dduardo, either make a vector of vectors or an array of arrays. After I got a couple of hints from the community, it actually came very quickly.

On to other things about C++...maybe I'll succeed in converthing those old codes I have to further some old projects that I can't seem get working on other machines and operating systems.
 

Similar threads

Replies
31
Views
3K
Replies
20
Views
3K
Replies
22
Views
4K
Replies
13
Views
3K
Replies
23
Views
2K
Replies
13
Views
4K
Replies
5
Views
3K
Back
Top