POV-Ray : Newsgroups : povray.off-topic : 99 lines of C++ for an unbiased ray tracer Server Time
4 Sep 2024 23:24:31 EDT (-0400)
  99 lines of C++ for an unbiased ray tracer (Message 36 to 45 of 65)  
<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>
From: Orchid XP v8
Subject: Re: 99 lines of C++ for an unbiased ray tracer
Date: 14 Jan 2010 16:41:40
Message: <4b4f8f94@news.povray.org>
>>>   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.
> 
>> This is plain. What surprises me is that sizeof() can give you an 
>> accurate size in the first place.
> 
>   Why wouldn't it? The compiler has to know the size of the array if it
> wants to allocate it in memory.

All that stuff about "C does not know the size of an array at run-time, 
and does not check array bounds". But then, this is from the same books 
that claim that pointers *are* arrays... Certainly if you asked for 
sizeof() on a pointer, you wouldn't get the size of the array it points to.

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Warp
Subject: Re: 99 lines of C++ for an unbiased ray tracer
Date: 14 Jan 2010 17:05:11
Message: <4b4f9516@news.povray.org>
Orchid XP v8 <voi### [at] devnull> wrote:
> >>>   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.
> > 
> >> This is plain. What surprises me is that sizeof() can give you an 
> >> accurate size in the first place.
> > 
> >   Why wouldn't it? The compiler has to know the size of the array if it
> > wants to allocate it in memory.

> All that stuff about "C does not know the size of an array at run-time, 
> and does not check array bounds".

  That talks about dynamically allocated arrays, not statically allocated
ones. They are different.

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: 99 lines of C++ for an unbiased ray tracer
Date: 14 Jan 2010 17:06:27
Message: <4b4f9563@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Warp wrote:
> >   Also the syntax of indexing an array and indexing memory using a pointer
> > is identical, adding to the confusion.

> Plus, you can't pass an array to a function, at least in C.

  Not directly, but you can do it indirectly if you enclose the array inside
a struct (in that case the array (inside the struct) is truly passed by
value rather than by-pointer).

-- 
                                                          - Warp


Post a reply to this message

From: Orchid XP v8
Subject: Re: 99 lines of C++ for an unbiased ray tracer
Date: 14 Jan 2010 17:12:06
Message: <4b4f96b6$1@news.povray.org>
>> Plus, you can't pass an array to a function, at least in C.
> 
>   Not directly, but you can do it indirectly if you enclose the array inside
> a struct (in that case the array (inside the struct) is truly passed by
> value rather than by-pointer).

...you really have been doing this for a long time, haven't you?

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Warp
Subject: Re: 99 lines of C++ for an unbiased ray tracer
Date: 14 Jan 2010 17:14:25
Message: <4b4f9741@news.povray.org>
Orchid XP v8 <voi### [at] devnull> wrote:
> >> Plus, you can't pass an array to a function, at least in C.
> > 
> >   Not directly, but you can do it indirectly if you enclose the array inside
> > a struct (in that case the array (inside the struct) is truly passed by
> > value rather than by-pointer).

> ...you really have been doing this for a long time, haven't you?

  Well, it's rather basic C.

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: 99 lines of C++ for an unbiased ray tracer
Date: 14 Jan 2010 17:51:23
Message: <4b4f9feb$1@news.povray.org>
Warp wrote:
> Orchid XP v8 <voi### [at] devnull> wrote:
>>>>>   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.
>>>> This is plain. What surprises me is that sizeof() can give you an 
>>>> accurate size in the first place.
>>>   Why wouldn't it? The compiler has to know the size of the array if it
>>> wants to allocate it in memory.
> 
>> All that stuff about "C does not know the size of an array at run-time, 
>> and does not check array bounds".
> 
>   That talks about dynamically allocated arrays, not statically allocated
> ones. They are different.

He's right, tho. C doesn't know the size of an array at run time. sizeof() 
is a compile-time operator.

-- 
Darren New, San Diego CA, USA (PST)
   Forget "focus follows mouse." When do
   I get "focus follows gaze"?


Post a reply to this message

From: Darren New
Subject: Re: 99 lines of C++ for an unbiased ray tracer
Date: 14 Jan 2010 17:51:59
Message: <4b4fa00f$1@news.povray.org>
Warp wrote:
> Darren New <dne### [at] sanrrcom> wrote:
>> Warp wrote:
>>>   Also the syntax of indexing an array and indexing memory using a pointer
>>> is identical, adding to the confusion.
> 
>> Plus, you can't pass an array to a function, at least in C.
> 
>   Not directly, but you can do it indirectly if you enclose the array inside
> a struct (in that case the array (inside the struct) is truly passed by
> value rather than by-pointer).

And in the original versions of C, you couldn't pass a struct by value 
either. :-)

But yah, that's a good point I hadn't considered.

-- 
Darren New, San Diego CA, USA (PST)
   Forget "focus follows mouse." When do
   I get "focus follows gaze"?


Post a reply to this message

From: Warp
Subject: Re: 99 lines of C++ for an unbiased ray tracer
Date: 14 Jan 2010 18:18:49
Message: <4b4fa659@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> He's right, tho. C doesn't know the size of an array at run time. sizeof() 
> is a compile-time operator.

  If it knows it at compile time, it knows it at runtime. It's not like
it forgets somewhere in between... :P

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: 99 lines of C++ for an unbiased ray tracer
Date: 14 Jan 2010 18:19:41
Message: <4b4fa68c@news.povray.org>
Out of curiosity I changed the location of the light to see that it really
renders it properly:

http://warp.povusers.org/images/smallpt.jpg

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: 99 lines of C++ for an unbiased ray tracer
Date: 14 Jan 2010 23:50:25
Message: <4b4ff411@news.povray.org>
Warp wrote:
> Darren New <dne### [at] sanrrcom> wrote:
>> He's right, tho. C doesn't know the size of an array at run time. sizeof() 
>> is a compile-time operator.
> 
>   If it knows it at compile time, it knows it at runtime. It's not like
> it forgets somewhere in between... :P

Well, no, I disagree. How do you find the size of an array at runtime? 
That's exactly why you have to pass it around along with the pointer.

OK, I'll grant you that the generated code has that available, in the sense 
that the loader avoids overwriting that space with other stuff, but it's not 
available to the C-level program in any way at runtime.

You can take sizeof(x) at compile time, and store that somewhere that 
runtime can find it, but there's nothing like Ada's myvariable'size 
expression that would tell you at runtime what the size is for an arbitrary 
array. There's no way to apply sizeof() to a non-array-name and find out the 
size of an array (i.e., no way to say "what's the size of the array this 
pointer points to").

Hmmm... If you put an array in a struct, can you ask 
"sizeof(myrecord.thearray)" and get an appropriate size? I suppose you 
could, but still I wouldn't count that as such.

-- 
Darren New, San Diego CA, USA (PST)
   Forget "focus follows mouse." When do
   I get "focus follows gaze"?


Post a reply to this message

<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>

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