Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 30, 2009 Re: Null references redux | ||||
---|---|---|---|---|
| ||||
If nonnull class references are added to D, then it can be good to add nonnull struct pointers too, to avoid similar bugs. (In C# for a struct you define a "default" one, that gets used by the compiler as the default one when the struct reference is null). Bye, bearophile |
September 30, 2009 Re: Null references redux | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Wed, 30 Sep 2009 08:53:57 -0400, bearophile <bearophileHUGS@lycos.com> wrote:
>If nonnull class references are added to D, then it can be good to add nonnull struct pointers too, to avoid similar bugs.
>
>(In C# for a struct you define a "default" one, that gets used by the compiler as the default one when the struct reference is null).
>
>Bye,
>bearophile
Don't get confused by 'new' in struct initializers. Structs in C# are value types. You can box them, use pointers to them in 'unsafe' context but you can't directly allocate them on heap. So there is no nullable references to structs. When you do:
struct S
{
public int x;
}
S s = new S(); // x is default initialized to 0
the struct is still allocated on stack and passed by value.
S s;
s.x = 1; //error, struct is not initialized
BTW, C# does have null-initialization problems regardless of flow control. One example:
struct S
{
public Object obj;
}
S s = new S();
s.obj.ToString(); // null-reference exception;
|
September 30, 2009 Re: Null references redux | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samukha | On Wed, 30 Sep 2009 18:26:20 +0300, Max Samukha <spambox@d-coding.com> wrote:
>On Wed, 30 Sep 2009 08:53:57 -0400, bearophile <bearophileHUGS@lycos.com> wrote:
>
>>If nonnull class references are added to D, then it can be good to add nonnull struct pointers too, to avoid similar bugs.
>>
>>(In C# for a struct you define a "default" one, that gets used by the compiler as the default one when the struct reference is null).
>>
>>Bye,
>>bearophile
>
>Don't get confused by 'new' in struct initializers. Structs in C# are value types. You can box them, use pointers to them in 'unsafe' context but you can't directly allocate them on heap. So there is no nullable references to structs. When you do:
>
>struct S
>{
> public int x;
>}
>
>S s = new S(); // x is default initialized to 0
Ok. I have rechecked this one and it appears that you don't have to initialize a struct if it is a POD (Microsoft names such structs 'unmanaged', of course) or you don't access fields that are references. For example:
struct S
{
public int x;
public Object obj;
}
S s;
s.x = 1; // ok, we can get away without initialization for now
s.obj.ToString(); // compile-time error
|
September 30, 2009 Re: Null references redux | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samukha | Max Samukha:
> Don't get confused by 'new' in struct initializers. Structs in C# are value types.
Yes, you are right.
But in D structs can be allocated on the heap too, so I think having optional nonnull struct pointers can be useful. The syntax and usage is similar to normal struct pointers.
Bye,
bearophile
|
September 30, 2009 Re: Null references redux | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samukha | On Wed, 30 Sep 2009 18:26:20 +0300, Max Samukha <spambox@d-coding.com> wrote:
>
>BTW, C# does have null-initialization problems regardless of flow control.
I'll probably never learn to proof-read my opuses. It should have been "flow analysis".
|
September 30, 2009 Re: Null references redux | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Wed, Sep 30, 2009 at 12:44 PM, bearophile <bearophileHUGS@lycos.com> wrote:
> Max Samukha:
>
>> Don't get confused by 'new' in struct initializers. Structs in C# are value types.
>
> Yes, you are right.
>
> But in D structs can be allocated on the heap too, so I think having optional nonnull struct pointers can be useful. The syntax and usage is similar to normal struct pointers.
I don't know why a struct pointer would be different than any other pointer. That is, you'd have S* and S*? as well as int* and int*?.
|
September 30, 2009 Re: Null references redux | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | On Wed, 30 Sep 2009 23:08:40 +0400, Jarrett Billingsley <jarrett.billingsley@gmail.com> wrote: > On Wed, Sep 30, 2009 at 12:44 PM, bearophile <bearophileHUGS@lycos.com> wrote: >> Max Samukha: >> >>> Don't get confused by 'new' in struct initializers. Structs in C# are >>> value types. >> >> Yes, you are right. >> >> But in D structs can be allocated on the heap too, so I think having optional nonnull struct pointers can be useful. The syntax and usage is similar to normal struct pointers. > > I don't know why a struct pointer would be different than any other > pointer. That is, you'd have S* and S*? as well as int* and int*?. Note that C stdlib (and other libraries/bindings) will need to be updated to reflect changes, e.g. extern(C) void*? malloc(size_t size); // may return null! which is great because it will provide additional safety. I've seen quite a lot of code that don't test returned value against null (which is a mistake, I believe). |
September 30, 2009 Re: Null references redux | ||||
---|---|---|---|---|
| ||||
Posted in reply to Denis Koroskin | On Wed, Sep 30, 2009 at 3:30 PM, Denis Koroskin <2korden@gmail.com> wrote:
> Note that C stdlib (and other libraries/bindings) will need to be updated to
> reflect changes, e.g.
>
> extern(C) void*? malloc(size_t size); // may return null!
>
> which is great because it will provide additional safety. I've seen quite a lot of code that don't test returned value against null (which is a mistake, I believe).
Wonderful. Don't you love self-documenting code that forces you to use it correctly? :P
|
October 01, 2009 Re: Null references redux | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 30/09/2009 18:44, bearophile wrote:
> Max Samukha:
>
>> Don't get confused by 'new' in struct initializers. Structs in C# are
>> value types.
>
> Yes, you are right.
>
> But in D structs can be allocated on the heap too, so I think having optional nonnull struct pointers can be useful. The syntax and usage is similar to normal struct pointers.
>
> Bye,
> bearophile
why not just use references with structs?
struct S { ... }
S* sPtr = new S;
S sRef = *sPtr; // non-null ref
there is no need for non-null pointers.
|
October 01, 2009 Re: Null references redux | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yigal Chripun | Yigal Chripun wrote:
> On 30/09/2009 18:44, bearophile wrote:
>> Max Samukha:
>>
>>> Don't get confused by 'new' in struct initializers. Structs in C# are
>>> value types.
>>
>> Yes, you are right.
>>
>> But in D structs can be allocated on the heap too, so I think having optional nonnull struct pointers can be useful. The syntax and usage is similar to normal struct pointers.
>>
>> Bye,
>> bearophile
>
> why not just use references with structs?
>
> struct S { ... }
> S* sPtr = new S;
> S sRef = *sPtr; // non-null ref
>
> there is no need for non-null pointers.
Because sRef wouldn't be a reference but a copy.
|
Copyright © 1999-2021 by the D Language Foundation