|
|
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
>> 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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |