Dumb question about complex arrays in c++

  • Context: C/C++ 
  • Thread starter Thread starter Dr Transport
  • Start date Start date
  • Tags Tags
    Arrays C++ Complex
Click For Summary

Discussion Overview

The discussion revolves around defining and initializing complex arrays in C++, particularly for someone transitioning from FORTRAN. It covers single complex numbers, multi-dimensional arrays, and the challenges associated with dynamic memory allocation.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant expresses confusion about defining and initializing complex arrays in C++ after having experience with FORTRAN.
  • Another participant suggests using a pointer to create a dynamic array of Complex objects with the syntax: Complex *array = new Complex[n];.
  • A correction is made regarding the syntax, emphasizing the need for a pointer with the correct spelling of "Complex".
  • A query is raised about creating a 2D array of complex numbers, with an initial suggestion that may not be correct.
  • One participant warns that dynamic memory allocation for multi-dimensional arrays is complex and suggests using an array of arrays approach.
  • Another participant notes that for simple data types, such as ints or doubles, memory allocation does not require special handling beyond using the new operator.
  • A suggestion is made to use a vector of vectors as a simpler alternative for multi-dimensional arrays.
  • A participant indicates they have figured out the solution with hints from the community and expresses a desire to convert old FORTRAN codes to C++.

Areas of Agreement / Disagreement

Participants present multiple approaches to defining complex arrays, with some disagreement on the best method for multi-dimensional arrays. The discussion remains unresolved regarding the optimal approach for dynamic memory allocation in this context.

Contextual Notes

Participants highlight the complexity of dynamic memory allocation for multi-dimensional arrays, indicating that it may lead to complicated code, especially in higher dimensions. There is also a mention of the simplicity of using vectors for certain applications.

Messages
2,603
Reaction score
785
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 ·
2
Replies
31
Views
3K
Replies
1
Views
2K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 13 ·
Replies
13
Views
3K
Replies
69
Views
11K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K