Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #15207

Re: passing static 2D array to function - not fully working like I hoped for...

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: passing static 2D array to function - not fully working like I hoped for...
Date 2011-12-29 15:35 -0500
Organization A noiseless patient Spider
Message-ID <4EFCCF27.6040506@verizon.net> (permalink)
References <41a267c2-3e83-4c98-9061-34a7e390e6d9@k10g2000yqk.googlegroups.com> <9m3mkvFh9sU1@mid.uni-berlin.de> <ff48c721-ce6d-4764-a164-d049f1b9659a@n39g2000yqh.googlegroups.com>

Show all headers | View raw


On 12/29/2011 02:03 PM, someone wrote:
> On Dec 29, 6:34 pm, j...@toerring.de (Jens Thoms Toerring) wrote:
>> someone <newsbo...@gmail.com> wrote:
...
>>> Explanation:
>>> I have a static 2D array: bezierData[4][3], 4 points * xyz-coordinates
>>> and I pass it to FindBezierControlPointsND with this call:
>>>   FindBezierControlPointsND( &N_kontur, Xval, Yval, bezierData);
>>> and the function takes: GLfloat bezierData[][3] as input.
>>> All is ok - the code works, however what I really wanted was to omit
>>> the 4th input parameter (const int &P) so it looks like:
>>> void setBezierPoint(const double &x, const double &y, const double &z,
>>>                     GLfloat bezierCTRpts[][3]) // 4th input is GONE !
>>> {
>>>   bezierCTRpts[0][0] = (GLfloat)x;
>>>   bezierCTRpts[0][1] = (GLfloat)y;
>>>   bezierCTRpts[0][2] = (GLfloat)z;
>>> }

There's really no reason for using C++ references here. You're not
changing the thing they refer to. C++ references are just syntactic
sugar for something that could also be done in ordinary C using pointers
(don't get me wrong - I like some kinds of syntactic sugar, and C++
references can be useful). By using a reference, you avoid a copy, but
impose the cost of dereferencing the pointer - it's pretty much a wash,
unless the parameter has a big complicated type - which isn't the case
for double.

>> Why then not make it
>>
>> void setBezierPoint(const double &x, const double &y, const double &z,
>>                    GLfloat bezierCTRpts[3])
>> {
>>   bezierCTRpts[0] = (GLfloat)x;
>>   bezierCTRpts[1] = (GLfloat)y;
>>   bezierCTRpts[2] = (GLfloat)z;
>>
>> }
> 
> Thank you very much.
> 
>> Note that the casts to 'GLfloat' are rather likely unnecessary
>> - why did you put them in? I would recomend to use casts only
> 
> Actually I cast double -> float, so I make the intermediate
> calculations using double precision, but when it comes to actually
> storing the data, I save it as a float (less precision). Normally I
> would be doing everything with double precision...
> 
> Of some reason, floats seem to be widely used with opengl... Don' ask
> me why - I don't know. That's why I cast.

But it doesn't make any difference; the conversion specified by the cast
will occur automatically even if you don't write the cast.

> ... I could also cast before
> calling the function, of course...

With the current declaration, that would be a waste of time; the values
would be converted back to double before executing the function call. If
you changed the function's declaration to have it take float parameters,
the conversion would occur automatically as a result of calling the
function, whether or not you used a cast. It wouldn't make much
difference compared to the current code.

>> when they are really needed (and since you're doing C++ you
>> also should be using C++-style casts if you have a good reason
>> to use them.
> 
> I don't see why, if it works and apparently it does work out fine with
> C?

C++'s specialized casts aren't needed here, for precisely the same
reason that the C casts aren't needed. However, where a cast is needed,
C++'s specialized casts are better because a diagnostic message is
mandatory if the combination of the type of the argument and the result
type violates the requirements of the specialized cast. It's just a
matter of choosing the appropriate specialized cast for the conversion
you want performed - in this case, static_cast<>. The diagnostic makes
it easier to detect problems due to giving the wrong expression or the
wrong result type, or the wrong specialized cast.

If you're certain you'll never make any such mistake, then it doesn't
matter. I don't think it's possible to be sanely certain of such things.
However, it is possible to sanely believe it's too much additional work
to justify bothering with.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

passing static 2D array to function - not fully working like I hoped for... someone <newsboost@gmail.com> - 2011-12-29 09:11 -0800
  Re: passing static 2D array to function - not fully working like I hoped for... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-29 17:27 +0000
    Re: passing static 2D array to function - not fully working like I hoped for... someone <newsboost@gmail.com> - 2011-12-29 10:55 -0800
  Re: passing static 2D array to function - not fully working like I hoped for... jt@toerring.de (Jens Thoms Toerring) - 2011-12-29 17:34 +0000
    Re: passing static 2D array to function - not fully working like I hoped for... someone <newsboost@gmail.com> - 2011-12-29 11:03 -0800
      Re: passing static 2D array to function - not fully working like I hoped for... jt@toerring.de (Jens Thoms Toerring) - 2011-12-29 19:38 +0000
        Re: passing static 2D array to function - not fully working like I hoped for... "BartC" <bc@freeuk.com> - 2011-12-29 23:19 +0000
          Re: passing static 2D array to function - not fully working like I hoped for... "BartC" <bc@freeuk.com> - 2011-12-31 11:29 +0000
        Re: passing static 2D array to function - not fully working like I hoped for... 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-29 17:26 -0800
      Re: passing static 2D array to function - not fully working like I hoped for... Keith Thompson <kst-u@mib.org> - 2011-12-29 11:58 -0800
        Re: passing static 2D array to function - not fully working like I hoped for... Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 17:45 -0800
          Re: passing static 2D array to function - not fully working like I hoped for... Kaz Kylheku <kaz@kylheku.com> - 2012-01-26 02:08 +0000
      Re: passing static 2D array to function - not fully working like I hoped for... James Kuyper <jameskuyper@verizon.net> - 2011-12-29 15:35 -0500
        Re: passing static 2D array to function - not fully working like I hoped for... Keith Thompson <kst-u@mib.org> - 2011-12-29 13:25 -0800
          Re: passing static 2D array to function - not fully working like I hoped for... "io_x" <a@b.c.invalid> - 2011-12-31 16:32 +0100
            Re: passing static 2D array to function - not fully working like I hoped for... 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-31 15:10 -0800
              Re: passing static 2D array to function - not fully working like I hoped for... "osmium" <r124c4u102@comcast.net> - 2011-12-31 18:03 -0600
      Re: passing static 2D array to function - not fully working like I hoped for... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-29 20:37 +0000
        Re: passing static 2D array to function - not fully working like I hoped for... jgk@panix.com (Joe keane) - 2012-01-03 15:48 +0000
          Re: passing static 2D array to function - not fully working like I hoped for... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-03 16:47 +0000
          Re: passing static 2D array to function - not fully working like I hoped for... Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-01-05 22:31 +0200
      Re: passing static 2D array to function - not fully working like I hoped for... Nobody <nobody@nowhere.com> - 2011-12-31 09:23 +0000
        Re: passing static 2D array to function - not fully working like I hoped for... Keith Thompson <kst-u@mib.org> - 2011-12-31 11:04 -0800
          Re: passing static 2D array to function - not fully working like I hoped for... Nobody <nobody@nowhere.com> - 2012-01-01 08:20 +0000
  Re: passing static 2D array to function - not fully working like I hoped for... Barry Schwarz <schwarzb@dqel.com> - 2011-12-29 09:49 -0800
    Re: passing static 2D array to function - not fully working like I hoped for... someone <newsboost@gmail.com> - 2011-12-29 11:13 -0800
      Re: passing static 2D array to function - not fully working like I hoped for... Seebs <usenet-nospam@seebs.net> - 2011-12-29 19:22 +0000
        Re: passing static 2D array to function - not fully working like I hoped for... someone <newsboost@gmail.com> - 2011-12-29 13:29 -0800
          Re: passing static 2D array to function - not fully working like I hoped for... Seebs <usenet-nospam@seebs.net> - 2011-12-29 21:42 +0000
            Re: passing static 2D array to function - not fully working like I hoped for... someone <newsboost@gmail.com> - 2011-12-29 15:28 -0800
          Re: passing static 2D array to function - not fully working like I hoped for... Keith Thompson <kst-u@mib.org> - 2011-12-29 14:46 -0800
        Re: passing static 2D array to function - not fully working like I hoped for... James Kuyper <jameskuyper@verizon.net> - 2011-12-29 18:29 -0500
          Re: passing static 2D array to function - not fully working like I hoped for... Seebs <usenet-nospam@seebs.net> - 2011-12-30 00:03 +0000
            Re: passing static 2D array to function - not fully working like I hoped for... James Kuyper <jameskuyper@verizon.net> - 2011-12-29 21:30 -0500
        Re: passing static 2D array to function - not fully working like I hoped for... Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 17:47 -0800
  Re: passing static 2D array to function - not fully working like I hoped for... Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 17:39 -0800

csiph-web