Jump to page: 1 218  
Page
Thread overview
dereferencing null
Mar 02, 2012
Nathan M. Swan
Mar 02, 2012
Jonathan M Davis
Mar 02, 2012
Peter Alexander
Mar 02, 2012
Daniel Murphy
Mar 02, 2012
Peter Alexander
Mar 03, 2012
Daniel Murphy
Mar 02, 2012
Jacob Carlborg
Mar 02, 2012
Jonathan M Davis
Mar 02, 2012
Jacob Carlborg
Mar 02, 2012
deadalnix
Mar 02, 2012
Marco Leise
Mar 02, 2012
deadalnix
Mar 02, 2012
Timon Gehr
Mar 02, 2012
David Nadlinger
Mar 02, 2012
Nick Sabalausky
Mar 02, 2012
Martin Nowak
Mar 02, 2012
deadalnix
Mar 02, 2012
Jacob Carlborg
Mar 02, 2012
deadalnix
Mar 03, 2012
Jonathan M Davis
Mar 05, 2012
deadalnix
Mar 05, 2012
Jérôme M. Berger
Mar 03, 2012
Walter Bright
Mar 03, 2012
bearophile
Mar 03, 2012
Tove
Mar 03, 2012
James Miller
Mar 03, 2012
Walter Bright
Mar 03, 2012
deadalnix
Mar 03, 2012
Timon Gehr
Mar 05, 2012
deadalnix
Mar 05, 2012
Chad J
Mar 05, 2012
Adam D. Ruppe
Mar 05, 2012
H. S. Teoh
Mar 05, 2012
Chad J
Mar 05, 2012
Adam D. Ruppe
Mar 05, 2012
Chad J
Mar 05, 2012
Adam D. Ruppe
Mar 05, 2012
Nick Sabalausky
Mar 06, 2012
Jason House
Mar 06, 2012
Adam D. Ruppe
Mar 06, 2012
bearophile
Mar 06, 2012
Timon Gehr
Mar 06, 2012
foobar
Mar 06, 2012
Timon Gehr
Mar 07, 2012
Chad J
Mar 07, 2012
bearophile
Mar 07, 2012
Timon Gehr
Mar 07, 2012
Timon Gehr
Mar 08, 2012
Chad J
Mar 08, 2012
Timon Gehr
Mar 08, 2012
Chad J
Mar 06, 2012
bearophile
Mar 05, 2012
Walter Bright
Mar 05, 2012
Chad J
Mar 05, 2012
Walter Bright
Mar 05, 2012
Sandeep Datta
Mar 06, 2012
bearophile
Mar 06, 2012
Martin Nowak
Mar 05, 2012
Jacob Carlborg
Mar 05, 2012
Andrej Mitrovic
Mar 05, 2012
H. S. Teoh
Mar 05, 2012
Walter Bright
Mar 05, 2012
H. S. Teoh
Mar 05, 2012
Andrej Mitrovic
Mar 05, 2012
Walter Bright
Mar 05, 2012
Nick Sabalausky
Mar 05, 2012
Jacob Carlborg
Mar 06, 2012
Walter Bright
Mar 06, 2012
Chad J
Mar 05, 2012
H. S. Teoh
Mar 05, 2012
Nick Sabalausky
Mar 05, 2012
H. S. Teoh
Mar 05, 2012
Adam D. Ruppe
Mar 06, 2012
Michel Fortin
Mar 06, 2012
Jonathan M Davis
Mar 06, 2012
Jacob Carlborg
Mar 06, 2012
Jacob Carlborg
Mar 06, 2012
Chad J
Mar 06, 2012
Jacob Carlborg
Mar 06, 2012
Jacob Carlborg
Mar 06, 2012
James Miller
Mar 06, 2012
Jacob Carlborg
Mar 06, 2012
Michel Fortin
Mar 06, 2012
bearophile
Mar 06, 2012
foobar
Mar 06, 2012
Martin Nowak
Mar 06, 2012
Jonathan M Davis
Mar 06, 2012
Chad J
Mar 06, 2012
Jonathan M Davis
Mar 06, 2012
Chad J
Mar 06, 2012
Mantis
Mar 07, 2012
Chad J
Mar 07, 2012
Walter Bright
Mar 07, 2012
Sean Kelly
Mar 07, 2012
Walter Bright
Mar 07, 2012
Walter Bright
Mar 07, 2012
Sean Kelly
Mar 07, 2012
H. S. Teoh
Mar 07, 2012
Chad J
Mar 07, 2012
Chad J
Mar 08, 2012
Chad J
Mar 08, 2012
Jonathan M Davis
Mar 08, 2012
Chad J
Mar 08, 2012
Jonathan M Davis
Mar 08, 2012
Chad J
Mar 08, 2012
Jonathan M Davis
Mar 08, 2012
Chad J
Mar 07, 2012
Chad J
Mar 07, 2012
H. S. Teoh
Mar 07, 2012
Jonathan M Davis
Mar 08, 2012
Chad J
Mar 05, 2012
Jacob Carlborg
Mar 05, 2012
David Nadlinger
Mar 05, 2012
Martin Nowak
Mar 05, 2012
Jonathan M Davis
Mar 03, 2012
Sandeep Datta
Mar 03, 2012
Walter Bright
Mar 03, 2012
Vladimir Panteleev
Mar 04, 2012
Sandeep Datta
Mar 04, 2012
Adam D. Ruppe
Mar 04, 2012
Sandeep Datta
Mar 04, 2012
Adam D. Ruppe
Mar 04, 2012
H. S. Teoh
Mar 04, 2012
Walter Bright
Mar 04, 2012
H. S. Teoh
Mar 04, 2012
Walter Bright
Mar 04, 2012
Sandeep Datta
Mar 04, 2012
Sandeep Datta
Mar 05, 2012
Nick Sabalausky
Mar 06, 2012
Walter Bright
Mar 06, 2012
Don Clugston
Mar 06, 2012
Sean Kelly
Mar 08, 2012
Don Clugston
Mar 08, 2012
Andrej Mitrovic
Mar 08, 2012
H. S. Teoh
Mar 09, 2012
Chad J
Mar 08, 2012
Andrej Mitrovic
Mar 08, 2012
Andrej Mitrovic
Mar 08, 2012
Andrej Mitrovic
Mar 08, 2012
H. S. Teoh
Mar 08, 2012
Sean Kelly
Mar 08, 2012
Walter Bright
Mar 08, 2012
David Nadlinger
Mar 09, 2012
bearophile
Mar 09, 2012
Adam D. Ruppe
Mar 09, 2012
bearophile
Mar 09, 2012
Timon Gehr
Mar 09, 2012
bearophile
Mar 06, 2012
Martin Nowak
Mar 04, 2012
Martin Nowak
Mar 04, 2012
Sandeep Datta
Mar 05, 2012
Nathan M. Swan
Mar 05, 2012
Jesse Phillips
Mar 06, 2012
Nathan M. Swan
Mar 06, 2012
Jonathan M Davis
Mar 06, 2012
Nathan M. Swan
Mar 02, 2012
Marco Leise
Re: dereferencing null - what about something like an check-scope?
Mar 08, 2012
dennis luehring
Mar 08, 2012
David Eagen
March 02, 2012
Am I correct that trying to use an Object null results in undefined behavior?

    Object o = null;
    o.opCmp(new Object); // segmentation fault on my OSX machine

This seems a bit non-D-ish to me, as other bugs like this throw Errors (e.g. RangeError).

It would be nice if it would throw a NullPointerError or something like that, because I spent a long time trying to find a bug that crashed the program before writeln-debugging statements could be flushed.

NMS
March 02, 2012
On Friday, March 02, 2012 05:37:46 Nathan M. Swan wrote:
> Am I correct that trying to use an Object null results in undefined behavior?
> 
>      Object o = null;
>      o.opCmp(new Object); // segmentation fault on my OSX machine
> 
> This seems a bit non-D-ish to me, as other bugs like this throw
> Errors (e.g. RangeError).
> 
> It would be nice if it would throw a NullPointerError or something like that, because I spent a long time trying to find a bug that crashed the program before writeln-debugging statements could be flushed.

It's defined. The operating system protects you. You get a segfault on *nix and an access violation on Windows. Walter's take on it is that there is no point in checking for what the operating system is already checking for - especially when it adds additional overhead. Plenty of folks disagree, but that's the way it is.

If you really care about checking for it, then just assert:

assert(obj !is null);

or

assert(obj);

(the second one will also call the object's invariant).

- Jonathan M Davis
March 02, 2012
On Friday, 2 March 2012 at 04:53:02 UTC, Jonathan M Davis wrote:
> On Friday, March 02, 2012 05:37:46 Nathan M. Swan wrote:
>> Am I correct that trying to use an Object null results in
>> undefined behavior?
>> 
>>      Object o = null;
>>      o.opCmp(new Object); // segmentation fault on my OSX machine
>> 
>> This seems a bit non-D-ish to me, as other bugs like this throw
>> Errors (e.g. RangeError).
>> 
>> It would be nice if it would throw a NullPointerError or
>> something like that, because I spent a long time trying to find a
>> bug that crashed the program before writeln-debugging statements
>> could be flushed.
>
> It's defined. The operating system protects you. You get a segfault on *nix and
> an access violation on Windows.

False.

-----------------------
import std.stdio;

class Foo
{
    final void bar()
    {
        writeln("I'm null!");
    }
}

void main()
{
    Foo foo;
    foo.bar();
}
-----------------------

% dmd test.d -O -release -inline
% ./test
I'm null!
%

-----------------------

You only get an error if there is a memory access involved (vtable, member data etc.)

March 02, 2012
"Peter Alexander" <peter.alexander.au@gmail.com> wrote in message news:vicaibqyaerogseqsjbe@forum.dlang.org...
>>
>> It's defined. The operating system protects you. You get a segfault on
>> *nix and
>> an access violation on Windows.
>
> False.
>
> [snip]
>
> You only get an error if there is a memory access involved (vtable, member data etc.)
>

It _is_ defined, you get an access violation whenever there's a dereference. Yes, you can call some types of member functions without any dereferences, but this is alse well defined and sometimes quite useful.


March 02, 2012
On 2012-03-02 10:22, Peter Alexander wrote:
> On Friday, 2 March 2012 at 04:53:02 UTC, Jonathan M Davis wrote:
>> On Friday, March 02, 2012 05:37:46 Nathan M. Swan wrote:
>>> Am I correct that trying to use an Object null results in
>>> undefined behavior?
>>>
>>> Object o = null;
>>> o.opCmp(new Object); // segmentation fault on my OSX machine
>>>
>>> This seems a bit non-D-ish to me, as other bugs like this throw
>>> Errors (e.g. RangeError).
>>>
>>> It would be nice if it would throw a NullPointerError or
>>> something like that, because I spent a long time trying to find a
>>> bug that crashed the program before writeln-debugging statements
>>> could be flushed.
>>
>> It's defined. The operating system protects you. You get a segfault on
>> *nix and
>> an access violation on Windows.
>
> False.
>
> -----------------------
> import std.stdio;
>
> class Foo
> {
> final void bar()
> {
> writeln("I'm null!");
> }
> }
>
> void main()
> {
> Foo foo;
> foo.bar();
> }
> -----------------------
>
> % dmd test.d -O -release -inline
> % ./test
> I'm null!
> %
>
> -----------------------
>
> You only get an error if there is a memory access involved (vtable,
> member data etc.)
>

I never thought about that.

-- 
/Jacob Carlborg
March 02, 2012
On Friday, March 02, 2012 11:56:52 Jacob Carlborg wrote:
> I never thought about that.

It's the same in C++. I was quite surprised when I first ran into it. But as Daniel points out, the behavior is still defined, just less expected. If you actually use this (or any member variable, since that would use this) inside of that member function though, you'll get a segfault just like if it had been dereferenced before calling the function like it would be with a virtual function.

- Jonathan M Davis
March 02, 2012
On 03/02/2012 10:22 AM, Peter Alexander wrote:
> You only get an error if there is a memory access involved (vtable,
> member data etc.)
>

In non-release mode you get an assertion failure.
March 02, 2012
On Friday, 2 March 2012 at 09:22:28 UTC, Peter Alexander wrote:
> You only get an error if there is a memory access involved (vtable, member data etc.)

By the way, my favorite application of that in C++ is debug helper member functions (think: using DMD's toChar() in GDB), which don't crash when invoked on a null pointer by checking if (this == 0) before accessing member variables.

David
March 02, 2012
On 2012-03-02 12:05, Jonathan M Davis wrote:
> On Friday, March 02, 2012 11:56:52 Jacob Carlborg wrote:
>> I never thought about that.
>
> It's the same in C++. I was quite surprised when I first ran into it. But as
> Daniel points out, the behavior is still defined, just less expected. If you
> actually use this (or any member variable, since that would use this) inside
> of that member function though, you'll get a segfault just like if it had been
> dereferenced before calling the function like it would be with a virtual
> function.
>
> - Jonathan M Davis

Yeah, that makes sense. A final method, not accessing "this", would be just as a static method. Which is like a free function scoped in a class.

-- 
/Jacob Carlborg
March 02, 2012
Le 02/03/2012 05:51, Jonathan M Davis a écrit :
> On Friday, March 02, 2012 05:37:46 Nathan M. Swan wrote:
>> Am I correct that trying to use an Object null results in
>> undefined behavior?
>>
>>       Object o = null;
>>       o.opCmp(new Object); // segmentation fault on my OSX machine
>>
>> This seems a bit non-D-ish to me, as other bugs like this throw
>> Errors (e.g. RangeError).
>>
>> It would be nice if it would throw a NullPointerError or
>> something like that, because I spent a long time trying to find a
>> bug that crashed the program before writeln-debugging statements
>> could be flushed.
>
> It's defined. The operating system protects you. You get a segfault on *nix and
> an access violation on Windows. Walter's take on it is that there is no point
> in checking for what the operating system is already checking for - especially
> when it adds additional overhead. Plenty of folks disagree, but that's the way
> it is.
>

The assertion that it has overhead isn't true.You'll find solutions without overhead (using libsigsegv in druntime for example).

BTW, object should be non nullable by default, if you ask me. The drawback of null is way bigger than any benefit.
« First   ‹ Prev
1 2 3 4 5 6 7 8 9 10 11