POV-Ray : Newsgroups : povray.off-topic : 99 lines of C++ for an unbiased ray tracer : Re: 99 lines of C++ for an unbiased ray tracer Server Time
5 Sep 2024 01:23:08 EDT (-0400)
  Re: 99 lines of C++ for an unbiased ray tracer  
From: Warp
Date: 14 Jan 2010 15:03:58
Message: <4b4f78ae@news.povray.org>
Orchid XP v8 <voi### [at] devnull> wrote:
> >> 44:  double n=sizeof(spheres)/sizeof(Sphere);
> >>
> >> Well... that's... one way to figure out how big an array is. :-.
> > 
> > That's the usual way to figure out how big an array is in C and C++.

> It is surprising to me that this actually works. I was under the 
> impression that C does not actually distinguish between pointers and 
> arrays (and integers and booleans and...)

  There is a big difference between a pointer and an array in C (and hence
in C++).

  Basically each array is a type of its own, defined by the element type
and the number of elements. What makes an array distinctly a type of its
own is that sizeof() always returns the number of bytes that a type
requires when allocated in memory, and an array is no different: sizeof()
for an array returns the amount of bytes taken by the entire array. This
is drastically different from a sizeof() of a pointer, which will always
return 4 or 8 (depending on whether you are compiling a 32-bit or 64-bit
program).

  Since sizeof() of an array returns the total amount of bytes taken by
an array, if you divide it by the amount of bytes of one element, you get
the total number of elements.

  For example if in C++ you instantiate a template using an array as a
template parameter, a distinct instanstiation will be made for each specified
type and size of array.

  Of course what makes this confusing is that an array implicitly casts to
a pointer of the element type. In other words, wherever a pointer of the
element type is required, you can give an array of that type instead (there
will be an implicity cast to the pointer type).

  Also the syntax of indexing an array and indexing memory using a pointer
is identical, adding to the confusion.

  It can sometimes be difficult to distinguish between an array and a
const pointer (a pointer which cannot be changed to point somewhere else).
sizeof() is one thing which makes the distinction.

> >> Also, where THE HELL is "Xi" defined? I can see it *used* in several 
> >> places, but I can't find a definitions.
> > 
> > Line 48. It's an argument to the function.

> No, it's an argument in the radiance() function, I meant where is it 
> defined in main().

  Line 82.

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.