Passing dynamic arrays

Bruno Medeiros brunodomedeiros+spam at com.gmail
Fri Nov 26 14:59:43 PST 2010


On 26/11/2010 19:16, Andrei Alexandrescu wrote:
>> "
>> One easy way out would be to always reallocate a upon appending to it
>> [...]
>> Although that behavior is easiest to implement, it
>> has serious efficiency problems. For example, oftentimes arrays are
>> iteratively grown in a loop:
>>
>> int[] a;
>> foreach (i; 0 .. 100) {
>> a ~= i;
>> }
>>
>> "
>>
>> Hum, "oftentimes"? I wonder if such code is really that common (and what
>> languages are we talking about here?)
>
> It would be difficult to challenge the assumption that appends in a loop
> are common.

Well, there was actually no assumption yet, I wanted first of all to 
know what languages you had in mind, because I'm wasn't sure I 
understood you correctly. C and C++ don't even have (dynamic) arrays. 
Java, Javascript (and C# as well, I think) have them but there is no 
append operation, arrays cannot be resized. So trivially that idiom is 
not common in these languages. :)
I don't know about Python, Ruby, Erlang, Haskell, Perl, PHP.
Or perhaps you were just being very liberal in your meaning of "arrays", 
and were also thinking of constructs like C++'s std::vector ?

-- 
Bruno Medeiros - Software Engineer


More information about the Digitalmars-d mailing list