Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #88207 > unrolled thread
| Started by | Lynn McGuire <lynnmcguire5@gmail.com> |
|---|---|
| First post | 2022-12-21 14:37 -0600 |
| Last post | 2022-12-22 22:23 -0600 |
| Articles | 12 — 6 participants |
Back to article view | Back to comp.lang.c++
best way to pass a multiple dimension variable to a method Lynn McGuire <lynnmcguire5@gmail.com> - 2022-12-21 14:37 -0600
Re: best way to pass a multiple dimension variable to a method Richard Damon <Richard@Damon-Family.org> - 2022-12-21 17:10 -0500
Re: best way to pass a multiple dimension variable to a method Lynn McGuire <lynnmcguire5@gmail.com> - 2022-12-21 16:18 -0600
Re: best way to pass a multiple dimension variable to a method Öö Tiib <ootiib@hot.ee> - 2022-12-21 18:03 -0800
Re: best way to pass a multiple dimension variable to a method David Brown <david.brown@hesbynett.no> - 2022-12-22 15:03 +0100
Re: best way to pass a multiple dimension variable to a method Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-12-22 03:02 +0000
Re: best way to pass a multiple dimension variable to a method Lynn McGuire <lynnmcguire5@gmail.com> - 2022-12-22 02:01 -0600
Re: best way to pass a multiple dimension variable to a method Öö Tiib <ootiib@hot.ee> - 2022-12-22 00:25 -0800
Re: best way to pass a multiple dimension variable to a method Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-12-22 12:13 +0000
Re: best way to pass a multiple dimension variable to a method Paavo Helde <eesnimi@osa.pri.ee> - 2022-12-22 09:07 +0200
Re: best way to pass a multiple dimension variable to a method Lynn McGuire <lynnmcguire5@gmail.com> - 2022-12-22 22:10 -0600
Re: best way to pass a multiple dimension variable to a method Lynn McGuire <lynnmcguire5@gmail.com> - 2022-12-22 22:23 -0600
| From | Lynn McGuire <lynnmcguire5@gmail.com> |
|---|---|
| Date | 2022-12-21 14:37 -0600 |
| Subject | best way to pass a multiple dimension variable to a method |
| Message-ID | <tnvqqq$1466q$1@dont-email.me> |
What is the best way to pass a multiple dimension variable to a method in C++ ? For instance, in C you could do the following: double xyz [5] [2] [14]; ... amethod (xyz, ...); Thanks, Lynn
[toc] | [next] | [standalone]
| From | Richard Damon <Richard@Damon-Family.org> |
|---|---|
| Date | 2022-12-21 17:10 -0500 |
| Message-ID | <%uLoL.89757$iU59.47205@fx14.iad> |
| In reply to | #88207 |
On 12/21/22 3:37 PM, Lynn McGuire wrote: > What is the best way to pass a multiple dimension variable to a method > in C++ ? > > For instance, in C you could do the following: > > double xyz [5] [2] [14]; > ... > amethod (xyz, ...); > > Thanks, > Lynn > You can do the same thing in C++ with a primative array, or you can define a container that acts like a multidimensional object, or you can use something like a vector of vectors (or array object of array objects)
[toc] | [prev] | [next] | [standalone]
| From | Lynn McGuire <lynnmcguire5@gmail.com> |
|---|---|
| Date | 2022-12-21 16:18 -0600 |
| Message-ID | <to00no$14ojn$1@dont-email.me> |
| In reply to | #88207 |
On 12/21/2022 2:37 PM, Lynn McGuire wrote:
> What is the best way to pass a multiple dimension variable to a method
> in C++ ?
>
> For instance, in C you could do the following:
>
> double xyz [5] [2] [14];
> ...
> amethod (xyz, ...);
>
> Thanks,
> Lynn
I am looking at
https://www.educba.com/3d-arrays-in-c-plus-plus/
and
https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function
Thanks,
Lynn
[toc] | [prev] | [next] | [standalone]
| From | Öö Tiib <ootiib@hot.ee> |
|---|---|
| Date | 2022-12-21 18:03 -0800 |
| Message-ID | <32309bc9-1ff9-48c9-b404-849b285644e3n@googlegroups.com> |
| In reply to | #88209 |
On Thursday, 22 December 2022 at 00:18:46 UTC+2, Lynn McGuire wrote: > On 12/21/2022 2:37 PM, Lynn McGuire wrote: > > What is the best way to pass a multiple dimension variable to a method > > in C++ ? > > > > For instance, in C you could do the following: > > > > double xyz [5] [2] [14]; > > ... > > amethod (xyz, ...); > > > > Thanks, > > Lynn > I am looking at > https://www.educba.com/3d-arrays-in-c-plus-plus/ > and > > https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function If the xyz is raw fixed dimension array then passing by reference is best as it is not losing any dimension information. void amethod(double (&xyz)[5][2][14], ...) About "multiple dimension variable" it depends on properties and usage. What dimensions you want to be dynamic if any and with what algorithms you want to process it. With little object of 140 doubles the usage does not matter but if it is far bigger than 500 doubles then cache-friendliness of data layout for processing can show noticeable differences.
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2022-12-22 15:03 +0100 |
| Message-ID | <to1o46$1csmp$1@dont-email.me> |
| In reply to | #88210 |
On 22/12/2022 03:03, Öö Tiib wrote: > On Thursday, 22 December 2022 at 00:18:46 UTC+2, Lynn McGuire wrote: >> On 12/21/2022 2:37 PM, Lynn McGuire wrote: >>> What is the best way to pass a multiple dimension variable to a method >>> in C++ ? >>> >>> For instance, in C you could do the following: >>> >>> double xyz [5] [2] [14]; >>> ... >>> amethod (xyz, ...); >>> >>> Thanks, >>> Lynn >> I am looking at >> https://www.educba.com/3d-arrays-in-c-plus-plus/ >> and >> >> https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function > > If the xyz is raw fixed dimension array then passing by reference is best as it > is not losing any dimension information. > void amethod(double (&xyz)[5][2][14], ...) > I would suggest that any time you have fixed size arrays, you are better off using std::array<>. Then the size is clearly fixed in the type, and there are no circumstances in which it "disappears" or the array decays into a pointer. (As Ben pointed out in another thread, it's not always easy to tell when this happens.) And you have container-style methods for the array if you want them, while all the time keeping the efficiency of a plain C array. It can be a little ugly to use std::array's of std::arrays, so a wrapping class might help. Or a template using : template <class T, size_t x, size_t y, size_t z> using array3d = std::array<std::array<std::array<T, z>, y>, x>; array3d<double, 5, 2, 14> xyz;
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2022-12-22 03:02 +0000 |
| Message-ID | <873598ta2w.fsf@bsb.me.uk> |
| In reply to | #88209 |
Lynn McGuire <lynnmcguire5@gmail.com> writes: > On 12/21/2022 2:37 PM, Lynn McGuire wrote: >> What is the best way to pass a multiple dimension variable to a method in C++ ? >> For instance, in C you could do the following: >> double xyz [5] [2] [14]; >> ... >> amethod (xyz, ...); Best is always going to depend on lost of as yet unspecified factors. Are the sizes (other than the "top-level" one) always know at compile time? Are they always the same? > I am looking at > https://www.educba.com/3d-arrays-in-c-plus-plus/ That hardly scratches the surface! > and > > https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function C has variably modified types which means you can pass run-time array sizes to a C function. C++ does not have this feature, but then that page includes some C++ and does not use variably modified types (as far as I could see). You probably need to say more about your constraints to get better advice. -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | Lynn McGuire <lynnmcguire5@gmail.com> |
|---|---|
| Date | 2022-12-22 02:01 -0600 |
| Message-ID | <to12t4$1j12$1@gioia.aioe.org> |
| In reply to | #88211 |
On 12/21/2022 9:02 PM, Ben Bacarisse wrote: > Lynn McGuire <lynnmcguire5@gmail.com> writes: > >> On 12/21/2022 2:37 PM, Lynn McGuire wrote: >>> What is the best way to pass a multiple dimension variable to a method in C++ ? >>> For instance, in C you could do the following: >>> double xyz [5] [2] [14]; >>> ... >>> amethod (xyz, ...); > > Best is always going to depend on lost of as yet unspecified factors. > Are the sizes (other than the "top-level" one) always know at compile > time? Are they always the same? > >> I am looking at >> https://www.educba.com/3d-arrays-in-c-plus-plus/ > > That hardly scratches the surface! > >> and >> >> https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function > > C has variably modified types which means you can pass run-time array > sizes to a C function. C++ does not have this feature, but then that > page includes some C++ and does not use variably modified types (as far > as I could see). > > You probably need to say more about your constraints to get better advice. I don't know if the array sizes are always known at compile time. I suspect that they are but I am far from sure. I am moving a 750,000 line F77 / C calculation engine to C++ using a modified version of F2C. F2C converts all multiple dimension arrays to single dimension arrays. Sometimes I convert them back. Still a few unknowns to work out. Thanks, Lynn
[toc] | [prev] | [next] | [standalone]
| From | Öö Tiib <ootiib@hot.ee> |
|---|---|
| Date | 2022-12-22 00:25 -0800 |
| Message-ID | <38f263fe-510c-423d-a2be-e05a9c20f489n@googlegroups.com> |
| In reply to | #88215 |
On Thursday, 22 December 2022 at 10:02:02 UTC+2, Lynn McGuire wrote: > On 12/21/2022 9:02 PM, Ben Bacarisse wrote: > > Lynn McGuire <lynnmc...@gmail.com> writes: > > > >> On 12/21/2022 2:37 PM, Lynn McGuire wrote: > >>> What is the best way to pass a multiple dimension variable to a method in C++ ? > >>> For instance, in C you could do the following: > >>> double xyz [5] [2] [14]; > >>> ... > >>> amethod (xyz, ...); > > > > Best is always going to depend on lost of as yet unspecified factors. > > Are the sizes (other than the "top-level" one) always know at compile > > time? Are they always the same? > > > >> I am looking at > >> https://www.educba.com/3d-arrays-in-c-plus-plus/ > > > > That hardly scratches the surface! > > > >> and > >> > >> https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function > > > > C has variably modified types which means you can pass run-time array > > sizes to a C function. C++ does not have this feature, but then that > > page includes some C++ and does not use variably modified types (as far > > as I could see). > > > > You probably need to say more about your constraints to get better advice. > I don't know if the array sizes are always known at compile time. I > suspect that they are but I am far from sure. > > I am moving a 750,000 line F77 / C calculation engine to C++ using a > modified version of F2C. F2C converts all multiple dimension arrays to > single dimension arrays. Sometimes I convert them back. Still a few > unknowns to work out. On such cases where we are transpiling and rearranging handling of arrays it is worth to write some tests to verify correctness of result and also to profile its efficiency. Issue is that Fortran keeps arrays always in column-major order C keeps arrays always in row-major order. Code of either can be optimised to take that into account. In C++ it is possible to design classes agnostic about that ordering. We can see that in several linear algebra libraries. Matrices choose ordering based on what was most efficient to get from algorithm and then choose algorithms based on ordering what is most efficient to process and that all is hidden behind scenes so user should not care.
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2022-12-22 12:13 +0000 |
| Message-ID | <87r0wrskjd.fsf@bsb.me.uk> |
| In reply to | #88215 |
Lynn McGuire <lynnmcguire5@gmail.com> writes: > On 12/21/2022 9:02 PM, Ben Bacarisse wrote: >> Lynn McGuire <lynnmcguire5@gmail.com> writes: >> >>> On 12/21/2022 2:37 PM, Lynn McGuire wrote: >>>> What is the best way to pass a multiple dimension variable to a method in C++ ? >>>> For instance, in C you could do the following: >>>> double xyz [5] [2] [14]; >>>> ... >>>> amethod (xyz, ...); >> Best is always going to depend on lost of as yet unspecified factors. >> Are the sizes (other than the "top-level" one) always know at compile >> time? Are they always the same? >> >>> I am looking at >>> https://www.educba.com/3d-arrays-in-c-plus-plus/ >> That hardly scratches the surface! >> >>> and >>> >>> https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function >> C has variably modified types which means you can pass run-time array >> sizes to a C function. C++ does not have this feature, but then that >> page includes some C++ and does not use variably modified types (as far >> as I could see). >> You probably need to say more about your constraints to get better advice. > > I don't know if the array sizes are always known at compile time. I > suspect that they are but I am far from sure. > > I am moving a 750,000 line F77 / C calculation engine to C++ using a > modified version of F2C. F2C converts all multiple dimension arrays > to single dimension arrays. Sometimes I convert them back. Still a > few unknowns to work out. You may better off just passing a pointer to the correct amount of space and doing the indexing "by hand". Do you ever need slices like column of row arrays? That might be another reason to do the index arithmetic yourself. -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | Paavo Helde <eesnimi@osa.pri.ee> |
|---|---|
| Date | 2022-12-22 09:07 +0200 |
| Message-ID | <to0vo9$1abr5$1@dont-email.me> |
| In reply to | #88207 |
21.12.2022 22:37 Lynn McGuire kirjutas: > What is the best way to pass a multiple dimension variable to a method > in C++ ? > > For instance, in C you could do the following: > > double xyz [5] [2] [14]; > ... > amethod (xyz, ...); > This C approach only really works as long as the dimensions are known at compile time and the arrays are small (larger arrays won't fit on stack). In C++, suggesting a custom class containing a plain std::vector for data storage and separate dimension info, providing some kind of 3D interface on top of that. Details depend on whether you prefer simple nice interfaces over speed, what are your typical dimensions, and what are your typical operations. In case you are interested in nice interfaces, one can provide nice operator[] overloads with proxies, so that you can access elements of your array via nice arr[x][y][z], or technically simpler arr.Elem(x, y, z). In case you are interested in performance, one needs to avoid arithmetic or multiple indirections when accessing single elements, and one needs to take care about memory locality. This in general means that the 3D abstraction becomes leaky; the algorithm working on the data would need to take out pointers to the whole linear array, or to the most tightly packed linear stretches, and iterate over them by itself. A single pixel access function like arr[x][y][z] or arr.Elem(x, y, z) would be pretty useless. No doubt there are already a myriad of multidimensional array libraries out there, but as the right solution depends on your needs, data, and usage, finding a suitable library might be more complicated than writing your own class suited to your needs.
[toc] | [prev] | [next] | [standalone]
| From | Lynn McGuire <lynnmcguire5@gmail.com> |
|---|---|
| Date | 2022-12-22 22:10 -0600 |
| Message-ID | <to39n5$1kh70$1@dont-email.me> |
| In reply to | #88207 |
On 12/21/2022 2:37 PM, Lynn McGuire wrote: > What is the best way to pass a multiple dimension variable to a method > in C++ ? > > For instance, in C you could do the following: > > double xyz [5] [2] [14]; > ... > amethod (xyz, ...); > > Thanks, > Lynn Thanks to all ! I am slowly puzzling my way through this. Thanks, Lynn
[toc] | [prev] | [next] | [standalone]
| From | Lynn McGuire <lynnmcguire5@gmail.com> |
|---|---|
| Date | 2022-12-22 22:23 -0600 |
| Message-ID | <to3af5$1kh70$2@dont-email.me> |
| In reply to | #88207 |
On 12/21/2022 2:37 PM, Lynn McGuire wrote: > What is the best way to pass a multiple dimension variable to a method > in C++ ? > > For instance, in C you could do the following: > > double xyz [5] [2] [14]; > ... > amethod (xyz, ...); > > Thanks, > Lynn BTW, I am also looking at this: https://stackoverflow.com/questions/13326021/how-to-pass-a-3d-array-as-a-parameter-to-function-c-also-do-global-variables Thanks, Lynn
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web