public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: How to pass 2D variable-sized arrays in C++?
@ 2002-09-26 14:29 Jason Mancini
  0 siblings, 0 replies; 9+ messages in thread
From: Jason Mancini @ 2002-09-26 14:29 UTC (permalink / raw)
  To: gcc-help


Hello,
Thank you for the thorough explanation!  That is a sol'n I
thought of, but I wasn't sure it could be applied to runtime
dynamic-sized arrays.
Thanks,
Jason Mancini

-------------------------------

Claudio Bley wrote:

The second problem is that it might not do what you expect when you
want to use it in the usual notation f[i][j] which is actually
equivalent to (*(*(f+i)+j)).

The correct solution is to use it like so:

void print_mn (int m, int n, float *f)
{
	for (int i = 0; i < m; ++i) {
		for (int j = 0; j < n; ++j) {
			cout << f[i*n+j] << '\t';
		}
		cout << endl;
	}
}

float[4][5] f;
print_mn (4, 5, &f[0][0]);


_________________________________________________________________
Join the worldÂ’s largest e-mail service with MSN Hotmail. 
http://www.hotmail.com

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to pass 2D variable-sized arrays in C++?
  2002-10-01  6:21       ` John Love-Jensen
@ 2002-10-01  6:45         ` Claudio Bley
  0 siblings, 0 replies; 9+ messages in thread
From: Claudio Bley @ 2002-10-01  6:45 UTC (permalink / raw)
  To: John Love-Jensen; +Cc: gcc-help

>>>>> "John" == John Love-Jensen <eljay@adobe.com> writes:

    >> OK, but what's your point here? We were talking about
    >> dynamically sized 2D arrays where the dimension of the array
    >> passed to `foo' is not known at compile time.

    John> In that case, I refer back to my first answer: typedef
    John> vector<vector<float> > myarray;

    John> void foo(myarray& a) ...

Of course, I'd prefer that too - but I guess you don't always have the
choice.

-- 
Claudio Bley                                 ASCII ribbon campaign (")
Debian GNU/Linux advocate                     - against HTML email  X 
http://www.cs.uni-magdeburg.de/~bley/                     & vCards / \

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to pass 2D variable-sized arrays in C++?
  2002-10-01  6:04     ` Claudio Bley
@ 2002-10-01  6:21       ` John Love-Jensen
  2002-10-01  6:45         ` Claudio Bley
  0 siblings, 1 reply; 9+ messages in thread
From: John Love-Jensen @ 2002-10-01  6:21 UTC (permalink / raw)
  To: Claudio Bley; +Cc: gcc-help

> OK, but what's your point here? We were talking about dynamically
> sized 2D arrays where the dimension of the array passed to `foo' is
> not known at compile time.

In that case, I refer back to my first answer:
typedef vector<vector<float> > myarray;

void foo(myarray& a) ...

--Eljay

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to pass 2D variable-sized arrays in C++?
  2002-10-01  4:59   ` John Love-Jensen
@ 2002-10-01  6:04     ` Claudio Bley
  2002-10-01  6:21       ` John Love-Jensen
  0 siblings, 1 reply; 9+ messages in thread
From: Claudio Bley @ 2002-10-01  6:04 UTC (permalink / raw)
  To: John Love-Jensen; +Cc: gcc-help


Hi,

>>>>> "John" == John Love-Jensen <eljay@adobe.com> writes:

    John> Hi Jason, Try this out for size:

OK, but what's your point here? We were talking about dynamically
sized 2D arrays where the dimension of the array passed to `foo' is
not known at compile time.

-- 
Claudio Bley                                 ASCII ribbon campaign (")
Debian GNU/Linux advocate                     - against HTML email  X 
http://www.cs.uni-magdeburg.de/~bley/                     & vCards / \

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to pass 2D variable-sized arrays in C++?
  2002-09-30 18:46 ` Claudio Bley
@ 2002-10-01  4:59   ` John Love-Jensen
  2002-10-01  6:04     ` Claudio Bley
  0 siblings, 1 reply; 9+ messages in thread
From: John Love-Jensen @ 2002-10-01  4:59 UTC (permalink / raw)
  To: Jason Mancini; +Cc: gcc-help

Hi Jason,

Try this out for size:

- - - - - - - - - -
#include <iostream>
using namespace std;

typedef float myarray[3][7];

void foo(myarray& a)
{
    for(int i = 0; i < 3; ++i)
        for(int j = 0; j < 7; ++j)
            cout << a[i][j] << (j == 6 ? "\n" : " ");
}

int main()
{
    myarray a;
    a[0][0] = 0.0f;
    a[0][1] = 0.1f;
    a[0][2] = 0.2f;
    a[0][3] = 0.3f;
    a[0][4] = 0.4f;
    a[0][5] = 0.5f;
    a[0][6] = 0.6f;
    a[1][0] = 1.0f;
    a[1][1] = 1.1f;
    a[1][2] = 1.2f;
    a[1][3] = 1.3f;
    a[1][4] = 1.4f;
    a[1][5] = 1.5f;
    a[1][6] = 1.6f;
    a[2][0] = 2.0f;
    a[2][1] = 2.1f;
    a[2][2] = 2.2f;
    a[2][3] = 2.3f;
    a[2][4] = 2.4f;
    a[2][5] = 2.5f;
    a[2][6] = 2.6f;
    foo(a);
}
- - - - - - - - - -

--Eljay

^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: How to pass 2D variable-sized arrays in C++?
  2002-09-30 14:48 Jason Mancini
@ 2002-09-30 18:46 ` Claudio Bley
  2002-10-01  4:59   ` John Love-Jensen
  0 siblings, 1 reply; 9+ messages in thread
From: Claudio Bley @ 2002-09-30 18:46 UTC (permalink / raw)
  To: Jason Mancini; +Cc: gcc-help

>>>>> "Jason" == Jason Mancini <jayrusman@hotmail.com> writes:

    Jason> Hi again, After many tries, I propose that this is the
    Jason> "correct solution".  The compiler should hide the address
    Jason> calculation for me -- this is why we have compilers after
    Jason> all!  Thanks, Jason Mancini


    Jason> void func(int c, int r, float *fa) { 
    Jason>    float (*f)[c] = (float (*)[c])fa;
    Jason>    ... f[xr][xc] ... }

Yeah, this is cool. I didn't know this is possible. The drawback is
that it might not work with any other compiler out there except
gcc. (I can confirm it doesn't work with Intel's C++ compiler for
example)

I myself wouldn't use such esoteric features (if it really is one -
maybe Intel's compiler is just faulty?). I would like to see some
reference to the standard spec where this is defined.

Cheers
-- 
Claudio Bley                                 ASCII ribbon campaign (")
Debian GNU/Linux advocate                     - against HTML email  X 
http://www.cs.uni-magdeburg.de/~bley/                     & vCards / \

^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: How to pass 2D variable-sized arrays in C++?
@ 2002-09-30 14:48 Jason Mancini
  2002-09-30 18:46 ` Claudio Bley
  0 siblings, 1 reply; 9+ messages in thread
From: Jason Mancini @ 2002-09-30 14:48 UTC (permalink / raw)
  To: gcc-help


Hi again,
After many tries, I propose that this is the "correct solution".
The compiler should hide the address calculation for me -- this
is why we have compilers after all!
Thanks,
Jason Mancini


void func(int c, int r, float *fa)
{
  float (*f)[c] = (float (*)[c])fa;
  ... f[xr][xc] ...
}

main()
{
  int c = 5, r = 4;
  float f[r][c];
  func(c, r, &f[0][0]);
}


-------------------------------

Claudio Bley wrote:

The second problem is that it might not do what you expect when you
want to use it in the usual notation f[i][j] which is actually
equivalent to (*(*(f+i)+j)).

The correct solution is to use it like so:

void print_mn (int m, int n, float *f)
{
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
cout << f[i*n+j] << '\t';
}
cout << endl;
}
}

float[4][5] f;
print_mn (4, 5, &f[0][0]);



_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com

^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: How to pass 2D variable-sized arrays in C++?
@ 2002-09-26  8:33 Quang Nguyen (Ngo)
  0 siblings, 0 replies; 9+ messages in thread
From: Quang Nguyen (Ngo) @ 2002-09-26  8:33 UTC (permalink / raw)
  To: 'Jason Mancini', gcc-help

Hi Jason,

Try this:

void func(int c, int r, float f[][])
        {
        printf("hello\n");
        }

main()
        {
        int c = 5, r = 7;
        float f[r][c];

        func(c, r, f);
        }

--
Quang



-----Original Message-----
From: Jason Mancini [mailto:jayrusman@hotmail.com] 
Sent: Wednesday, September 25, 2002 7:37 PM
To: gcc-help@gcc.gnu.org
Subject: How to pass 2D variable-sized arrays in C++?



Hello,
How does one type the 2D variable size array "f" below?
I've tried nasty casts that all get rejected, cheating
with void, etc, etc.  Very stumped!  Sure, I could back
up to a real float** and manually new[] and pass float**,
but that takes all the fun out!

Thanks,
Jason


void function(int c, int r, ??? f )
{
}

main() {
  int c(5), r(7);
  float f[r][c];
  function(c, r, f);
};


--


_________________________________________________________________
Join the world's largest e-mail service with MSN Hotmail. 
http://www.hotmail.com


________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs SkyScan
service. For more information on a proactive anti-virus service working
around the clock, around the globe, visit http://www.messagelabs.com
________________________________________________________________________

________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs SkyScan
service. For more information on a proactive anti-virus service working
around the clock, around the globe, visit http://www.messagelabs.com
________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* How to pass 2D variable-sized arrays in C++?
@ 2002-09-25 19:36 Jason Mancini
  0 siblings, 0 replies; 9+ messages in thread
From: Jason Mancini @ 2002-09-25 19:36 UTC (permalink / raw)
  To: gcc-help


Hello,
How does one type the 2D variable size array "f" below?
I've tried nasty casts that all get rejected, cheating
with void, etc, etc.  Very stumped!  Sure, I could back
up to a real float** and manually new[] and pass float**,
but that takes all the fun out!

Thanks,
Jason


void function(int c, int r, ??? f )
{
}

main() {
  int c(5), r(7);
  float f[r][c];
  function(c, r, f);
};


--


_________________________________________________________________
Join the worldÂ’s largest e-mail service with MSN Hotmail. 
http://www.hotmail.com

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2002-10-01 13:45 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-26 14:29 How to pass 2D variable-sized arrays in C++? Jason Mancini
  -- strict thread matches above, loose matches on Subject: below --
2002-09-30 14:48 Jason Mancini
2002-09-30 18:46 ` Claudio Bley
2002-10-01  4:59   ` John Love-Jensen
2002-10-01  6:04     ` Claudio Bley
2002-10-01  6:21       ` John Love-Jensen
2002-10-01  6:45         ` Claudio Bley
2002-09-26  8:33 Quang Nguyen (Ngo)
2002-09-25 19:36 Jason Mancini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).