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
4 Sep 2024 21:20:07 EDT (-0400)
  Re: 99 lines of C++ for an unbiased ray tracer  
From: Darren New
Date: 15 Jan 2010 14:14:38
Message: <4b50be9e$1@news.povray.org>
Warp wrote:
> Darren New <dne### [at] sanrrcom> wrote:
>> Well, no, I disagree. How do you find the size of an array at runtime? 
> 
>   It's a constant integral value, hence known at runtime.

If you ask for it, the compiler will give you some value that's unassociated 
with the array that happens to contain the size of the array, sure.

>> That's exactly why you have to pass it around along with the pointer.
> 
>   Now you are confusing dynamically allocated arrays with static arrays.
> They are not the same thing.

Not at all. My point is that in many languages, you can ask an array for its 
size, regardless of where you use it. Because of the fact that arrays are 
not first class in C, you cannot talk about an array except where the name 
of the array is in scope. So, in order for a subroutine to know how big is 
the array you've passed in, you have to pass in the size separately.

strcpy() has no way of doing what strncpy() does, and you need an extra 
argument to strncpy() that may or may not be the size of the destination 
array. Hence, there's no way to get the size of an array at runtime.

Certainly C knows the size of a dynamically allocated array at runtime 
*better* than it knows the size of a statically allocated array at runtime.

>> Hmmm... If you put an array in a struct, can you ask 
>> "sizeof(myrecord.thearray)" and get an appropriate size?
> 
>   Yes, actually.
> 
>> I suppose you 
>> could, but still I wouldn't count that as such.
> 
>   "As such"? What? Runtime? sizeof() always works at compile time.

That's what I'm saying. sizeof() works at compile time. Hence, there's no 
way to ask what the size of an array is at runtime, whether it's static or 
not. The only way to get the size of an array is if the *declaration* of the 
array is in scope.

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


Post a reply to this message

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