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


Groups > comp.lang.c > #15027

Re: rms from rosettacode

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: rms from rosettacode
Date 2011-12-27 10:04 -0500
Organization A noiseless patient Spider
Message-ID <4EF9DE90.8000206@verizon.net> (permalink)
References <4ef99d34$0$1379$4fafbaef@reader2.news.tin.it> <jdcbai$7uf$1@dont-email.me> <4ef9b395$0$1378$4fafbaef@reader2.news.tin.it> <jdcck6$edo$1@dont-email.me> <4ef9bbc2$0$1388$4fafbaef@reader2.news.tin.it>

Show all headers | View raw


On 12/27/2011 07:36 AM, superpollo wrote:
> James Kuyper ha scritto:
>> On 12/27/2011 07:01 AM, superpollo wrote:
>>> James Kuyper ha scritto:
>>>> On 12/27/2011 05:25 AM, superpollo wrote:
>>>>> http://rosettacode.org/wiki/Averages/Root_mean_square#C
>>>>>
>>>>>
>>>>> ...
>>>>> [~/superpollo]$ cat rms_rc.c
>>>>> #include <stdio.h>
>>>>> #include <math.h>
>>>>>
>>>>> double rms(double *v, int n)
>>>>> {
>>>>>      int i;
>>>>>      double sum = 0.0;
>>>>>      for (i = 0; i < n; i++)
>>>>>          sum += v[i] * v[i];
>>>>>      return sqrt(sum / n);
>>>>> }
>>>>>
>>>>> int main()
>>>>> {
>>>>>      double v[] = { 1., 2., 3., 4., 5., 6., 7., 8., 9., 10. };
>>>>>      printf("%f\n", rms(v, sizeof(v) / sizeof(double)));
>>>>>
>>>>>      return 0;
>>>>> }
>>>>> [~/superpollo]$ cc -lm rms_rc.c && ./a.out
>>>>> 6.204837
>>>>> ...
>>>>>
>>>>> that works fine, but i am quite unhappy with having to say to the rms 
>>>>> function how big the array is... after all, i guess, shouldn't the 
>>>>> function be able to figure it out herself?
>>>> How?
>>> just in the same way as "main" does. no?
>>
>> main() doesn't figure out the length itself; it's told the length by the
>> routine that calls main, by two different mechanisms: argc contains the
>> count of the arguments; since you're objecting to having to pass 'n' to
>> rms(), you should object, on the same ground, to the fact that the
>> implementation has to pass argc to main(). main() also uses the sentinel
>> value method I mentioned in the part of my message which you snipped:
>> argv[argc] is required to be a null pointer.
> 
> i really cannot se how the "main()" arguments are related to the 
> question, since "v[]" is declared INSIDE "main()".

I assumed that you were comparing the 'v' argument of rms() to the argv
argument often (but not necessarily) passed to main(). That would be a
valid comparison, and argument 'n' of rms() corresponds to the argc
argument of main(). I didn't realize that you're referring to the 'v'
defined inside of main(), because that v is irrelevant to your original
question - the fact that you think it's relevant is a symptom of your
misunderstanding of what's going on in this program.

The 'v' identifier in main() has block scope - it can only be referred
to inside the body of main(). The 'v' identifier in rms() has block
scope too; it can only be referred to in the parameter list or body of
rms(). Despite the fact that they have the same name, they identify
different objects, of different types. The 'v' in main() identifies an
array of 10 doubles. The 'v' in rms() identifies a pointer to double. I
general, those will have two very different sizes.

> on a related note: how can "sizeof" figure out the size of "v[]"?

When you use sizeof(v) inside of main, it gives you the size of the
array of 10 doubles named v. The compiler knows how big a double is, and
it knows that v[] contains 10 of them, so sizeof(v) just gives
10*sizeof(double), on many implementations, sizeof(double) is 8 - on
such implementations, sizeof(v) in main() would therefore be 80.

When you use sizeof(v) inside of rms, it will give you the size of the
pointer to double named 'v'. On many implementations, pointers are
generally 32-bit, and bytes are 8 bit, in which case sizeof(v) in rms()
would be 4.

> is 
> there some sentinel implicitly at the end? if yes, why not passing it 
> around to "rms()", so ther the finction can figure out the size herself? 
> it does not seem to create such a big overhead, for instance.

The C language leaves such details under your control; "sizeof
expression" only gives you the size of the object referred to by the
expression; if that object is a pointer to the first element of an
array, that will be the size of the pointer; in general, it won't be the
size of the array, and in the unlikely event that it is also the size of
the array, that's just a coincidence.


In a part of my message that you chose not to quote, I talked about n
having a value of sizeof(v)/sizeof(double), which corresponds to
sizeof(double*)/sizeof(double), an expression that has a value of 0 on
many systems. I must not have been fully awake yet when I wrote that.
sizeof(v) gives sizeof(double*) only in rms(); the only place where your
code divides sizeof() values is in main(). Please ignore that part of my
comments.

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


Thread

rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-27 11:25 +0100
  Re: rms from rosettacode James Kuyper <jameskuyper@verizon.net> - 2011-12-27 06:48 -0500
    Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-27 13:01 +0100
      Re: rms from rosettacode James Kuyper <jameskuyper@verizon.net> - 2011-12-27 07:10 -0500
        Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-27 13:36 +0100
          Re: rms from rosettacode James Kuyper <jameskuyper@verizon.net> - 2011-12-27 10:04 -0500
    Re: rms from rosettacode Keith Thompson <kst-u@mib.org> - 2011-12-27 14:42 -0800
      Re: rms from rosettacode James Kuyper <jameskuyper@verizon.net> - 2011-12-27 18:46 -0500
  Re: rms from rosettacode 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-27 09:49 -0800
  Re: rms from rosettacode "BartC" <bc@freeuk.com> - 2011-12-27 18:14 +0000
    Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-27 19:33 +0100
      Re: rms from rosettacode jt@toerring.de (Jens Thoms Toerring) - 2011-12-27 19:04 +0000
        Re: rms from rosettacode Keith Thompson <kst-u@mib.org> - 2011-12-27 14:45 -0800
        Re: rms from rosettacode blp@cs.stanford.edu (Ben Pfaff) - 2011-12-27 14:57 -0800
      Re: rms from rosettacode "BartC" <bc@freeuk.com> - 2011-12-27 19:06 +0000
        Re: rms from rosettacode Bull in a China Blue Shop <chine.bleu@yahoo.com> - 2011-12-27 16:16 -0800
      Re: rms from rosettacode James Kuyper <jameskuyper@verizon.net> - 2011-12-27 14:11 -0500
      Re: rms from rosettacode Seebs <usenet-nospam@seebs.net> - 2011-12-27 20:13 +0000
      Re: rms from rosettacode Johann Klammer <klammerr@NOSPAM.aon.at> - 2011-12-28 00:23 +0100
      Re: rms from rosettacode 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-27 15:54 -0800
        Re: rms from rosettacode Keith Thompson <kst-u@mib.org> - 2011-12-27 22:53 -0800
          Re: rms from rosettacode Seebs <usenet-nospam@seebs.net> - 2011-12-28 06:29 +0000
          Re: rms from rosettacode 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-28 02:45 -0800
  Re: rms from rosettacode Kaz Kylheku <kaz@kylheku.com> - 2011-12-27 19:53 +0000
    Re: rms from rosettacode Seebs <usenet-nospam@seebs.net> - 2011-12-27 20:13 +0000
    Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-27 21:31 +0100
      Re: rms from rosettacode Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-27 20:49 -0500
        Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-28 11:42 +0100
          Re: rms from rosettacode Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-29 08:18 -0800
        Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-28 11:54 +0100
          Re: rms from rosettacode Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-12-29 04:12 +0200
            Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-29 14:38 +0100
        Re: rms from rosettacode Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-28 02:26 -0800
          Re: rms from rosettacode Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-28 21:35 -0500
            Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-29 14:10 +0100
        Re: rms from rosettacode James Kuyper <jameskuyper@verizon.net> - 2011-12-28 08:03 -0500
          Re: rms from rosettacode "BartC" <bc@freeuk.com> - 2011-12-28 13:32 +0000
            Re: rms from rosettacode Dr Nick <3-nospam@temporary-address.org.uk> - 2011-12-28 13:41 +0000
            Re: rms from rosettacode Kaz Kylheku <kaz@kylheku.com> - 2011-12-28 19:29 +0000
            Re: rms from rosettacode Keith Thompson <kst-u@mib.org> - 2011-12-28 13:47 -0800
            Re: rms from rosettacode Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-12-29 04:19 +0200
      Re: rms from rosettacode Kaz Kylheku <kaz@kylheku.com> - 2011-12-28 19:11 +0000

csiph-web