How is this code invalid?
j
my.email at gmail.com
Sun Dec 18 04:36:13 UTC 2022
On Saturday, 17 December 2022 at 00:23:32 UTC, thebluepandabear
wrote:
> I am reading the fantastic book about D by Ali Çehreli, and he
> gives the following example when he talks about variadic
> functions:
>
> ```D
> int[] numbersForLaterUse;
>
> void foo(int[] numbers...) {
> numbersForLaterUse = numbers;
> }
>
> struct S {
> string[] namesForLaterUse;
>
> void foo(string[] names...) {
> namesForLaterUse = names;
> }
> }
> ```
>
> He says that the code above is a bug because:
>
> "Both the free-standing function foo() and the member function
> S.foo() are in
> error because they store slices to automatically-generated
> temporary arrays that
> live on the program stack. Those arrays are valid only during
> the execution of the
> variadic functions."
>
> The thing is, when I run the code I get absolutely no error, so
> how is this exactly a 'bug' if the code runs properly? That's
> what I am confused about. What is the D compiler doing behind
> the scenes?
Ali is right. It is a bug. Do not be like the man of the moon who
copies brothers like a monkey then learning all that is bad from
his brothers he convinces himself it is ok. Be your own man and
determine if you have what it takes or not. We are here for your
support. I am sure there is a method in object that can help you
with this bug because I have used it before, but do not be a
Lemming on the wall. You will fall. Then you will not be able to
help the other behind you from falling too.
More information about the Digitalmars-d-learn
mailing list