Re: Better diagnostics for null classes dereferencing

2018-07-10 Thread Seb via Digitalmars-d-learn
On Tuesday, 10 July 2018 at 20:57:02 UTC, Steven Schveighoffer wrote: On 7/10/18 4:02 PM, Per Nordlöw wrote: On Tuesday, 10 July 2018 at 19:27:07 UTC, Steven Schveighoffer wrote: On 7/10/18 3:01 PM, Per Nordlöw wrote: [...] Yes, call this function on startup: import etc.linux : 

Re: Better diagnostics for null classes dereferencing

2018-07-10 Thread Neia Neutuladh via Digitalmars-d-learn
On Tuesday, 10 July 2018 at 22:53:25 UTC, kdevel wrote: extern (C) __gshared bool rt_trapExceptions; static this () { rt_trapExceptions = false; } This will catch exceptions raised in main and in static constructors that run after this one. However, if you put that code in

Re: Better diagnostics for null classes dereferencing

2018-07-10 Thread kdevel via Digitalmars-d-learn
On Tuesday, 10 July 2018 at 22:31:54 UTC, Adam D. Ruppe wrote: Turn off rtTrapExceptions though the command line switch PR is STILL NOT MERGED https://github.com/dlang/druntime/pull/2035 extern (C) __gshared bool rt_trapExceptions; static this () { rt_trapExceptions =

Re: Better diagnostics for null classes dereferencing

2018-07-10 Thread Adam D. Ruppe via Digitalmars-d-learn
On Tuesday, 10 July 2018 at 21:18:01 UTC, kdevel wrote: but how do I force the runtime to generate a coredump for real post-mortem analysis? Turn off rtTrapExceptions though the command line switch PR is STILL NOT MERGED https://github.com/dlang/druntime/pull/2035 come on, people.

Re: Better diagnostics for null classes dereferencing

2018-07-10 Thread Steven Schveighoffer via Digitalmars-d-learn
u'd have to have a special runtime, and change the constructor for RangeError to access a null pointer. -Steve

Re: Better diagnostics for null classes dereferencing

2018-07-10 Thread Per Nordlöw via Digitalmars-d-learn
On Tuesday, 10 July 2018 at 19:27:07 UTC, Steven Schveighoffer wrote: import etc.linux : registerMemoryErrorHandler; Needs to be: import etc.linux.memoryerror : registerMemoryErrorHandler;

Re: Better diagnostics for null classes dereferencing

2018-07-10 Thread kdevel via Digitalmars-d-learn
On Tuesday, 10 July 2018 at 21:09:23 UTC, Steven Schveighoffer wrote: [...] As far as the OS is concerned, a[2 .. $] is within the process memory limit. Of course, that's an out of bounds access, so the compiler or the bounds check *should* complain. It complains at runtime > ./dumpme2

Re: Better diagnostics for null classes dereferencing

2018-07-10 Thread Steven Schveighoffer via Digitalmars-d-learn
On 7/10/18 5:01 PM, kdevel wrote: On Tuesday, 10 July 2018 at 20:10:54 UTC, Adam D. Ruppe wrote: On Tuesday, 10 July 2018 at 19:01:22 UTC, Per Nordlöw wrote: [...] Run the program in a debugger, or run `ulimit -c unlimited` to enable core dumps [...] Works for null ptr deref but how do I

Re: Better diagnostics for null classes dereferencing

2018-07-10 Thread kdevel via Digitalmars-d-learn
On Tuesday, 10 July 2018 at 20:10:54 UTC, Adam D. Ruppe wrote: On Tuesday, 10 July 2018 at 19:01:22 UTC, Per Nordlöw wrote: [...] Run the program in a debugger, or run `ulimit -c unlimited` to enable core dumps [...] Works for null ptr deref but how do I enforce core dumps in this code

Re: Better diagnostics for null classes dereferencing

2018-07-10 Thread Steven Schveighoffer via Digitalmars-d-learn
On 7/10/18 4:02 PM, Per Nordlöw wrote: On Tuesday, 10 July 2018 at 19:27:07 UTC, Steven Schveighoffer wrote: On 7/10/18 3:01 PM, Per Nordlöw wrote: Is it possible to change run-time behaviour of null-class dereferencing, on Linux, so that it gives some other diagnostics than: Program exited

Re: Better diagnostics for null classes dereferencing

2018-07-10 Thread Seb via Digitalmars-d-learn
On Tuesday, 10 July 2018 at 19:01:22 UTC, Per Nordlöw wrote: Is it possible to change run-time behaviour of null-class dereferencing, on Linux, so that it gives some other diagnostics than: Program exited with code -11 Does DMD and LDC provide different alternatives here? On a Systemd

Re: Better diagnostics for null classes dereferencing

2018-07-10 Thread Adam D. Ruppe via Digitalmars-d-learn
On Tuesday, 10 July 2018 at 19:01:22 UTC, Per Nordlöw wrote: Is it possible to change run-time behaviour of null-class dereferencing, on Linux, so that it gives some other diagnostics than: Run the program in a debugger, or run `ulimit -c unlimited` to enable core dumps so you can run

Re: Better diagnostics for null classes dereferencing

2018-07-10 Thread Per Nordlöw via Digitalmars-d-learn
On Tuesday, 10 July 2018 at 19:27:07 UTC, Steven Schveighoffer wrote: On 7/10/18 3:01 PM, Per Nordlöw wrote: Is it possible to change run-time behaviour of null-class dereferencing, on Linux, so that it gives some other diagnostics than: Program exited with code -11 Does DMD and LDC provide

Re: Better diagnostics for null classes dereferencing

2018-07-10 Thread Steven Schveighoffer via Digitalmars-d-learn
On 7/10/18 3:01 PM, Per Nordlöw wrote: Is it possible to change run-time behaviour of null-class dereferencing, on Linux, so that it gives some other diagnostics than: Program exited with code -11 Does DMD and LDC provide different alternatives here? Yes, call this function on startup

Better diagnostics for null classes dereferencing

2018-07-10 Thread Per Nordlöw via Digitalmars-d-learn
Is it possible to change run-time behaviour of null-class dereferencing, on Linux, so that it gives some other diagnostics than: Program exited with code -11 Does DMD and LDC provide different alternatives here?

Re: class that is initialized becomes null when outside of function

2018-07-09 Thread Flaze07 via Digitalmars-d-learn
On Monday, 9 July 2018 at 09:38:30 UTC, ag0aep6g wrote: The `win` you're creating in `init` is a function-local variable. It ceases to exist when `init` returns. It's not `this.win`. In `run`, you're accessing `this.win`. It's still null because you never assigned anything there. So change

Re: class that is initialized becomes null when outside of function

2018-07-09 Thread ag0aep6g via Digitalmars-d-learn
On 07/09/2018 11:18 AM, Flaze07 wrote: class Game { [...]     RenderWindow win; [...]     void init()     { [...]     auto win = new RenderWindow( VideoMode( 600, 600 ), "snake" ); [...]     }     void run()     { [...]     writeln( w

Re: class that is initialized becomes null when outside of function

2018-07-09 Thread Flaze07 via Digitalmars-d-learn
On Monday, 9 July 2018 at 09:18:50 UTC, Flaze07 wrote: I am using DSFML module game; [...] oh, and I forgot, I had Game myGame; static this() { myGame = new Game; } below the Game class

class that is initialized becomes null when outside of function

2018-07-09 Thread Flaze07 via Digitalmars-d-learn
e / 2, Vector2f( win.size.x, win.size.y ) ); } void run() { auto clock = new Clock; writeln( win is null ); while( win.isOpen() ) { writeln("lol" ); } } } the problem here is, during init(), the writeln will output false, whic

Re: determining if array element is null

2018-06-05 Thread Alex via Digitalmars-d-learn
On Tuesday, 5 June 2018 at 14:52:28 UTC, Timoses wrote: Does `int[4] nums = void` work? Work for what? If you avoid initialization, then the variable(s) are not initialized. https://dlang.org/spec/declaration.html#void_init However, an int is not nullable and always contains a value.

Re: determining if array element is null

2018-06-05 Thread Timoses via Digitalmars-d-learn
On Saturday, 2 June 2018 at 18:10:38 UTC, eastanon wrote: Does D array implementation support an array of null values? int a[4] = null; But I ran into a type error while checking if a[i] is null foreach(i; 0..3){ if(i == null){ writeln("it is null"); } } } How do you set

Re: determining if array element is null

2018-06-02 Thread ag0aep6g via Digitalmars-d-learn
On 06/02/2018 08:35 PM, Neia Neutuladh wrote: 2. `int[4] a = null` treats the initialization as a copy from an array whose value is null. If you run just that line of code, it will produce an error at runtime: "object.Error@(0): Array lengths don't match for copy: 0 != 4" I

Re: determining if array element is null

2018-06-02 Thread Neia Neutuladh via Digitalmars-d-learn
On Saturday, 2 June 2018 at 18:10:38 UTC, eastanon wrote: Does D array implementation support an array of null values? int a[4] = null; But I ran into a type error while checking if a[i] is null foreach(i; 0..3){ if(i == null){ writeln("it is null"); } } } How do you set

determining if array element is null

2018-06-02 Thread eastanon via Digitalmars-d-learn
Does D array implementation support an array of null values? int a[4] = null; But I ran into a type error while checking if a[i] is null foreach(i; 0..3){ if(i == null){ writeln("it is null"); } } } How do you set fixed size array of null values and check if they are null?

Re: C API / const char *text / std.string.toStringz pointer is always NULL on C side

2018-05-19 Thread kinke via Digitalmars-d-learn
On Saturday, 19 May 2018 at 17:33:08 UTC, Robert M. Münch wrote: On 2018-05-18 14:42:17 +, Adam D. Ruppe said: A value struct return is actually done via a hidden pointer parameter (so the function can construct it in-place for the caller, a standard optimization), so it just shifted all

Re: C API / const char *text / std.string.toStringz pointer is always NULL on C side

2018-05-19 Thread Robert M. Münch via Digitalmars-d-learn
On 2018-05-18 14:42:17 +, Adam D. Ruppe said: On Friday, 18 May 2018 at 14:06:11 UTC, Robert M. Münch wrote: So, having a wrong return-type here, resulted in the const char *text parameter always being NULL. Not sure I understand the relation but looks strange to me... at least not very

Re: C API / const char *text / std.string.toStringz pointer is always NULL on C side

2018-05-18 Thread Adam D. Ruppe via Digitalmars-d-learn
On Friday, 18 May 2018 at 14:06:11 UTC, Robert M. Münch wrote: So, having a wrong return-type here, resulted in the const char *text parameter always being NULL. Not sure I understand the relation but looks strange to me... at least not very obvious. A value struct return is actually done via

Re: C API / const char *text / std.string.toStringz pointer is always NULL on C side

2018-05-18 Thread Robert M. Münch via Digitalmars-d-learn
? That was causing the problem. So, having a wrong return-type here, resulted in the const char *text parameter always being NULL. Not sure I understand the relation but looks strange to me... at least not very obvious. -- Robert M. Münch http://www.saphirion.com smarter | better | faster

Re: C API / const char *text / std.string.toStringz pointer is always NULL on C side

2018-05-16 Thread Steven Schveighoffer via Digitalmars-d-learn
it like this: result = myfunc(0, 0, std.string.toStringz("1"), stuff, true); The problem is, that on the DLL side *text is always NULL. I doesn't matter what I put as third argument. And even more strange, I have an other function with a const char* as first argument. There

Re: C API / const char *text / std.string.toStringz pointer is always NULL on C side

2018-05-16 Thread Robert M. Münch via Digitalmars-d-learn
, std.string.toStringz("1"), stuff, true); The problem is, that on the DLL side *text is always NULL. I doesn't matter what I put as third argument. And even more strange, I have an other function with a const char* as first argument. There everything is working... Any idea what cause

Re: C API / const char *text / std.string.toStringz pointer is always NULL on C side

2018-05-16 Thread Steven Schveighoffer via Digitalmars-d-learn
ingz isn't necessary, string literals always are null terminated, and implicitly cast to const char *. The problem is, that on the DLL side *text is always NULL. I doesn't matter what I put as third argument. And even more strange, I have an other function with a const char* as first argument. There

C API / const char *text / std.string.toStringz pointer is always NULL on C side

2018-05-16 Thread Robert M. Münch via Digitalmars-d-learn
I have an extern(C) function in a DLL with this signature: result* myfunc(double x, double y, const char *text, stuff *myStuff, bool flag); I call it like this: result = myfunc(0, 0, std.string.toStringz("1"), stuff, true); The problem is, that on the DLL side *text is alwa

Re: C API / const char *text / std.string.toStringz pointer is always NULL on C side

2018-05-16 Thread rikki cattermole via Digitalmars-d-learn
he problem is, that on the DLL side *text is always NULL. I doesn't matter what I put as third argument. And even more strange, I have an other function with a const char* as first argument. There everything is working... Any idea what cause this could be? This is really strange... Please post the

Re: toString contains null for struct with function/method

2018-04-09 Thread number via Digitalmars-d-learn
On Sunday, 8 April 2018 at 15:51:05 UTC, Paul Backus wrote: On Sunday, 8 April 2018 at 15:04:49 UTC, number wrote: writeln(s2);// S2(0, null) S2 is a nested struct [1], which means it has a hidden pointer field that's used to access its enclosing scope. If you change

Re: Checking if a function pointer is set or null

2018-04-08 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, April 09, 2018 00:25:08 solidstate1991 via Digitalmars-d-learn wrote: > Would the if(!(myFunctionPointer is null)){} work is I > intended? You can also do if(myFunctionPointer !is null) - Jonathan M Davis

Re: Checking if a function pointer is set or null

2018-04-08 Thread Uknown via Digitalmars-d-learn
On Monday, 9 April 2018 at 00:25:08 UTC, solidstate1991 wrote: Would the if(!(myFunctionPointer is null)){} work is I intended? Yes, that works as you expect https://run.dlang.io/is/ZTtm0P

Checking if a function pointer is set or null

2018-04-08 Thread solidstate1991 via Digitalmars-d-learn
Would the if(!(myFunctionPointer is null)){} work is I intended?

Re: toString contains null for struct with function/method

2018-04-08 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 8 April 2018 at 15:04:49 UTC, number wrote: the write() shows a 'null' if the struct has a function/method. why is that? ``` import std.stdio; void main() { struct S { int i; } S s; writeln(s);// S(0

toString contains null for struct with function/method

2018-04-08 Thread number via Digitalmars-d-learn
the write() shows a 'null' if the struct has a function/method. why is that? ``` import std.stdio; void main() { struct S { int i; } S s; writeln(s);// S(0) writeln(typeid(s).sizeof); // 8 struct S2

Re: Can't "is null" an interface?!?! Incompatible types???

2018-03-08 Thread Jacob Carlborg via Digitalmars-d-learn
On Thursday, 8 March 2018 at 08:04:54 UTC, Nick Sabalausky (Abscissa) wrote: Interesting. I was using vibe.d 'v0.8.3-rc.1' (which doesn't appear to work on run.dlang.io). But it does seem to work for me if I use 'v0.8.3-alpha.1'. I wonder what could have changed to result in this? It's a

Re: Can't "is null" an interface?!?! Incompatible types???

2018-03-08 Thread Nick Sabalausky (Abscissa) via Digitalmars-d-learn
On 03/08/2018 03:04 AM, Nick Sabalausky (Abscissa) wrote: Interesting. I was using vibe.d 'v0.8.3-rc.1' (which doesn't appear to work on run.dlang.io). But it does seem to work for me if I use 'v0.8.3-alpha.1'. I wonder what could have changed to result in this?

Re: Can't "is null" an interface?!?! Incompatible types???

2018-03-08 Thread Nick Sabalausky (Abscissa) via Digitalmars-d-learn
On 03/08/2018 01:13 AM, Nicholas Wilson wrote: That does seem odd. --- /+dub.sdl: dependency "vibe-d" version="~>0.8.3-alpha.1" +/ import vibe.core.net; import std.stdio; TCPConnection mySocket; void main() {     auto b = mySocket is null;     writeln(b); } --- wor

Re: Can't "is null" an interface?!?! Incompatible types???

2018-03-07 Thread Nicholas Wilson via Digitalmars-d-learn
On Thursday, 8 March 2018 at 04:48:08 UTC, Nick Sabalausky (Abscissa) wrote: - import vibe.core.net; TCPConnection mySocket; void main() { auto b = mySocket is null; } - That's giving me: - Error: incompatible types for (mySocket

Can't "is null" an interface?!?! Incompatible types???

2018-03-07 Thread Nick Sabalausky (Abscissa) via Digitalmars-d-learn
- import vibe.core.net; TCPConnection mySocket; void main() { auto b = mySocket is null; } - That's giving me: - Error: incompatible types for (mySocket) is (null): TCPConnection and typeof(null) - WTF

Re: Class instance becoming null after calling bindings to C code???

2017-11-17 Thread Kagamin via Digitalmars-d-learn
And https://github.com/Extrawurst/DerelictFmod/issues/1

Re: Class instance becoming null after calling bindings to C code???

2017-11-15 Thread Chirs Forest via Digitalmars-d-learn
*) a` instead to print the address of the object itself. writeln(); // null Though why it would ever say null is weird. Maybe one of the lengths you pass to a C function is wrong and it is writing too far and killing one of the hidden thread local pointer variables. The problem isn't

Re: Class instance becoming null after calling bindings to C code???

2017-11-15 Thread Adam D. Ruppe via Digitalmars-d-learn
. writeln(); // null Though why it would ever say null is weird. Maybe one of the lengths you pass to a C function is wrong and it is writing too far and killing one of the hidden thread local pointer variables.

Class instance becoming null after calling bindings to C code???

2017-11-15 Thread Chirs Forest via Digitalmars-d-learn
I'm using the derelict fmod bindings to handle sounds in my application and I'm running into a weird bug... If I put calls to fmod in a function inside a class, upon calling those functions the instance of that class will be null, or otherwise changed. I obviously get an access violation if I

Re: opCast fails when this is null.

2017-10-28 Thread Mike Wey via Digitalmars-d-learn
On 28-10-17 16:22, Nicholas Wilson wrote: On Saturday, 28 October 2017 at 14:19:01 UTC, Nicholas Wilson wrote: As Basile mentioned, this is compiler sticking checks in behind your back. The reason it works on new LDC is because #6982 was cherry picked to LDC (1.3?) before it was merged into

Re: opCast fails when this is null.

2017-10-28 Thread Nicholas Wilson via Digitalmars-d-learn
On Saturday, 28 October 2017 at 14:19:01 UTC, Nicholas Wilson wrote: As Basile mentioned, this is compiler sticking checks in behind your back. The reason it works on new LDC is because #6982 was cherry picked to LDC (1.3?) before it was merged into dmd (not sure what version, I though it was

Re: opCast fails when this is null.

2017-10-28 Thread Nicholas Wilson via Digitalmars-d-learn
this; } } void main() { A a = null; B b = null; auto c = cast(Object)a; auto d = cast(Object)b; // Fails with: core.exception.AssertError@test.d(8): null this } ``` How would you write an opCast that would handle this case correctly? Testing if this is null

Re: opCast fails when this is null.

2017-10-28 Thread Basile B. via Digitalmars-d-learn
this; } } void main() { A a = null; B b = null; auto c = cast(Object)a; auto d = cast(Object)b; // Fails with: core.exception.AssertError@test.d(8): null this } ``` How would you write an opCast that would handle this case correctly? Compiler change is required

opCast fails when this is null.

2017-10-28 Thread Mike Wey via Digitalmars-d-learn
The following code runs correctly when compiled with ldc (1.4.0) but fails with an assert error when compiled with dmd (2.076 and ldc 1.2.0) ``` class A { } class B { T opCast(T)() { return this; } } void main() { A a = null; B b

Re: Express "Class argument may not be null" ?

2017-08-09 Thread Steven Schveighoffer via Digitalmars-d-learn
, the in contract does actually enforce the requirement. And adds null pointer checks even when clearly not needed. Clearly not needed? I thought the point was to ensure the reference is not null? I meant code like this: `k->something(); foo(*k);` or `auto k = new Klass(); foo(*k);`. Anyway, the po

Re: Express "Class argument may not be null" ?

2017-08-09 Thread Johan via Digitalmars-d-learn
passing a pointer. Come on Steven, I thought you knew I am a compiler dev. The caller _is_ doing dereferencing. That's what the '*' means semantically and it matters. [1] However, the in contract does actually enforce the requirement. And adds null pointer checks even when clearly not needed

Re: Express "Class argument may not be null" ?

2017-08-09 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/8/17 4:00 PM, Andre Kostur wrote: On 2017-08-08 12:38 PM, Steven Schveighoffer wrote: On 8/8/17 2:56 PM, ag0aep6g wrote: On 08/08/2017 08:34 PM, Johan Engelen wrote: How would you express the function interface intent that a reference to a class may not be null? For a function "

Re: Express "Class argument may not be null" ?

2017-08-09 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/8/17 3:59 PM, Johan Engelen wrote: On Tuesday, 8 August 2017 at 19:38:19 UTC, Steven Schveighoffer wrote: Note that C++ also can do this, so I'm not sure the & is accomplishing the correct goal: void foo(Klass&); int main() { Klass *k = NULL; foo(*k); } In C++, it

Re: readText with added null-terminator that enables sentinel-based search

2017-08-09 Thread Marco Leise via Digitalmars-d-learn
Am Tue, 08 Aug 2017 20:48:39 + schrieb Nordlöw : > Has anybody written a wrapper around `std.file.readText` (or > similar) that appends a final zero-byte terminator in order to > realize sentinel-based search in textual parsers. What do you mean by similar? There are

readText with added null-terminator that enables sentinel-based search

2017-08-08 Thread Nordlöw via Digitalmars-d-learn
Has anybody written a wrapper around `std.file.readText` (or similar) that appends a final zero-byte terminator in order to realize sentinel-based search in textual parsers.

Re: Express "Class argument may not be null" ?

2017-08-08 Thread Adam D. Ruppe via Digitalmars-d-learn
I was about to say "use NotNull" but there still isn't one in std.typecons. ugh. But it is just a wrapper type that checks null in the contracts too, so you can do it at the function itself.

Re: Express "Class argument may not be null" ?

2017-08-08 Thread Andre Kostur via Digitalmars-d-learn
On 2017-08-08 12:38 PM, Steven Schveighoffer wrote: On 8/8/17 2:56 PM, ag0aep6g wrote: On 08/08/2017 08:34 PM, Johan Engelen wrote: How would you express the function interface intent that a reference to a class may not be null? For a function "void foo(Klass)", calling "foo(

Re: Express "Class argument may not be null" ?

2017-08-08 Thread Johan Engelen via Digitalmars-d-learn
On Tuesday, 8 August 2017 at 19:38:19 UTC, Steven Schveighoffer wrote: Note that C++ also can do this, so I'm not sure the & is accomplishing the correct goal: void foo(Klass&); int main() { Klass *k = NULL; foo(*k); } In C++, it is clear that the _caller_ is doing the deref

Re: Express "Class argument may not be null" ?

2017-08-08 Thread Johan Engelen via Digitalmars-d-learn
On Tuesday, 8 August 2017 at 18:57:48 UTC, Steven Schveighoffer wrote: On 8/8/17 2:34 PM, Johan Engelen wrote: Hi all, How would you express the function interface intent that a reference to a class may not be null? For a function "void foo(Klass)", calling "foo(null)"

Re: Express "Class argument may not be null" ?

2017-08-08 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/8/17 2:56 PM, ag0aep6g wrote: On 08/08/2017 08:34 PM, Johan Engelen wrote: How would you express the function interface intent that a reference to a class may not be null? For a function "void foo(Klass)", calling "foo(null)" is valid. How do I express that tha

Re: Express "Class argument may not be null" ?

2017-08-08 Thread ag0aep6g via Digitalmars-d-learn
On 08/08/2017 08:34 PM, Johan Engelen wrote: How would you express the function interface intent that a reference to a class may not be null? For a function "void foo(Klass)", calling "foo(null)" is valid. How do I express that that is invalid? (let's leave erroring

Re: Express "Class argument may not be null" ?

2017-08-08 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/8/17 2:34 PM, Johan Engelen wrote: Hi all, How would you express the function interface intent that a reference to a class may not be null? For a function "void foo(Klass)", calling "foo(null)" is valid. How do I express that that is invalid? (let's leave erroring

Express "Class argument may not be null" ?

2017-08-08 Thread Johan Engelen via Digitalmars-d-learn
Hi all, How would you express the function interface intent that a reference to a class may not be null? For a function "void foo(Klass)", calling "foo(null)" is valid. How do I express that that is invalid? (let's leave erroring with a compile error aside for now)

Re: Why would an initialised struct pointer field be null in the struct's destructor?

2017-05-21 Thread Stanislav Blinov via Digitalmars-d-learn
the destructor must be callable on .init so you might do like struct Foo { int* ptr; ~this() { // might be called with ptr is null cuz of .init // so wanna check if(ptr !is null) free(ptr); } } Ah, yes, exactly. The page is indeed the same one. P.S. Though it's fine to call

Re: Why would an initialised struct pointer field be null in the struct's destructor?

2017-05-21 Thread Adam D. Ruppe via Digitalmars-d-learn
Foo { int* ptr; ~this() { // might be called with ptr is null cuz of .init // so wanna check if(ptr !is null) free(ptr); } }

Re: Why would an initialised struct pointer field be null in the struct's destructor?

2017-05-21 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 22 May 2017 at 00:23:26 UTC, Adam D. Ruppe wrote: On Sunday, 21 May 2017 at 14:13:20 UTC, Stanislav Blinov wrote: Not if you either emplace() or blit Foo.init into all of the array elements. You especially need to be safe calling ~this on Foo.init. How so? .init is supposed to be

Re: Why would an initialised struct pointer field be null in the struct's destructor?

2017-05-21 Thread Adam D. Ruppe via Digitalmars-d-learn
On Sunday, 21 May 2017 at 14:13:20 UTC, Stanislav Blinov wrote: Not if you either emplace() or blit Foo.init into all of the array elements. You especially need to be safe calling ~this on Foo.init.

Re: Why would an initialised struct pointer field be null in the struct's destructor?

2017-05-21 Thread Stanislav Blinov via Digitalmars-d-learn
On Sunday, 21 May 2017 at 23:59:08 UTC, Guillaume Piolat wrote: On Sunday, 21 May 2017 at 12:48:10 UTC, Adam D. Ruppe wrote: Any struct should be able to have its destructor called Does this rule also applies to class objects? Yes. If your destructor does modify the state, you should expect

Re: Why would an initialised struct pointer field be null in the struct's destructor?

2017-05-21 Thread Guillaume Piolat via Digitalmars-d-learn
On Sunday, 21 May 2017 at 12:48:10 UTC, Adam D. Ruppe wrote: Any struct should be able to have its destructor called Does this rule also applies to class objects?

Re: Why would an initialised struct pointer field be null in the struct's destructor?

2017-05-21 Thread Stanislav Blinov via Digitalmars-d-learn
On Sunday, 21 May 2017 at 12:48:10 UTC, Adam D. Ruppe wrote: On Saturday, 20 May 2017 at 10:48:54 UTC, Gary Willoughby wrote: // Why is this._foo null here??? The others have answered why and what to do, but note that according to the spec, that any struct should be able

Re: Why would an initialised struct pointer field be null in the struct's destructor?

2017-05-21 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 20 May 2017 at 10:48:54 UTC, Gary Willoughby wrote: // Why is this._foo null here??? The others have answered why and what to do, but note that according to the spec, that any struct should be able to have its destructor called, so you should do a null check

Re: Why would an initialised struct pointer field be null in the struct's destructor?

2017-05-21 Thread Eduard Staniloiu via Digitalmars-d-learn
On Saturday, 20 May 2017 at 10:48:54 UTC, Gary Willoughby wrote: Looks like you would want to use emplace [0] here. public this(int n) { this._data = (cast(Foo*) calloc(n, Foo.sizeof))[0 .. n]; foreach(ref element; this._data) {

Re: Why would an initialised struct pointer field be null in the struct's destructor?

2017-05-20 Thread Gary Willoughby via Digitalmars-d-learn
On Saturday, 20 May 2017 at 12:25:39 UTC, Stanislav Blinov wrote: Oof. Dangerous stuff. :) Thanks for the heads up but I think I'm covering all cases in my main code.

Re: Why would an initialised struct pointer field be null in the struct's destructor?

2017-05-20 Thread Gary Willoughby via Digitalmars-d-learn
On Saturday, 20 May 2017 at 11:15:57 UTC, Moritz Maxeiner wrote: Because `element = tmp` destroys `element`, which you allocated filled with zeroes. The destructor will run for each `element`. Right, I get it because the destructors running on the struct that is being replaced. Doh!

Re: Why would an initialised struct pointer field be null in the struct's destructor?

2017-05-20 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 20 May 2017 at 10:48:54 UTC, Gary Willoughby wrote: In the following code, the `_foo` pointer (of the Foo struct) is null in the first call to the destructor. Why is this? I think it's got something to do with the foreach loop but I'm not sure. Any ideas? Oof. Dangerous stuff

Re: Why would an initialised struct pointer field be null in the struct's destructor?

2017-05-20 Thread Moritz Maxeiner via Digitalmars-d-learn
On Saturday, 20 May 2017 at 10:48:54 UTC, Gary Willoughby wrote: In the following code, the `_foo` pointer (of the Foo struct) is null in the first call to the destructor. Why is this? I think it's got something to do with the foreach loop but I'm not sure. Any ideas? struct Bar

Why would an initialised struct pointer field be null in the struct's destructor?

2017-05-20 Thread Gary Willoughby via Digitalmars-d-learn
In the following code, the `_foo` pointer (of the Foo struct) is null in the first call to the destructor. Why is this? I think it's got something to do with the foreach loop but I'm not sure. Any ideas? import std.stdio; import core.stdc.stdlib : malloc, calloc, free; struct Foo

Re: Null-Safe Dereference Operator

2017-03-13 Thread Adam D. Ruppe via Digitalmars-d-learn
with a null string if there wasn't one. I used to write: string header; if(auto e = document.querySelector("h1")) header = e.innerText; but now i can write: string header = document.optionSelector("h1").innerText; http://dpldocs.info/experimental-docs/arsd.dom.Element.optionS

Re: Null-Safe Dereference Operator

2017-03-13 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, March 14, 2017 00:51:02 Jolly James via Digitalmars-d-learn wrote: > Does anybody know, if D has a null-safe dereference operator like > C# does (?.) or something similar? It does not, though if you really wanted to, you could probably create template that did the same thing

Re: null this

2016-11-21 Thread Satoshi via Digitalmars-d-learn
On Monday, 21 November 2016 at 10:25:17 UTC, Satoshi wrote: Hello, how can calling method on local struct throw "null this"? struct is initialized by .init and have default values. struct Foo { Bar bar; static struct Bar { float x, y, z; } Bar getBar() {

Re: null this

2016-11-21 Thread rikki cattermole via Digitalmars-d-learn
On 21/11/2016 11:25 PM, Satoshi wrote: Hello, how can calling method on local struct throw "null this"? struct is initialized by .init and have default values. struct Foo { Bar bar; static struct Bar { float x, y, z; } Bar getBar() { return bar; } } then Foo foo

Re: null this

2016-11-21 Thread Satoshi via Digitalmars-d-learn
cast(ulong) but is not null. It won't work just in special cases what I cannot reproduce easily.

null this

2016-11-21 Thread Satoshi via Digitalmars-d-learn
Hello, how can calling method on local struct throw "null this"? struct is initialized by .init and have default values. struct Foo { Bar bar; static struct Bar { float x, y, z; } Bar getBar() { return bar; } } then Foo foo = Foo.init; auto b = foo.getBar(); // throw "null this"

Re: Stacktrace on Null Pointer Derefence

2016-09-22 Thread Adam D. Ruppe via Digitalmars-d-learn
On Thursday, 22 September 2016 at 19:51:31 UTC, Nordlöw wrote: A known bug? Yeah, it shows the line before instead of the line of. But it still shows basically where you are.

Re: Stacktrace on Null Pointer Derefence

2016-09-22 Thread ketmar via Digitalmars-d-learn
p.s. that is the reason it is not turned on by default, btw.

Re: Stacktrace on Null Pointer Derefence

2016-09-22 Thread ketmar via Digitalmars-d-learn
On Thursday, 22 September 2016 at 19:51:31 UTC, Nordlöw wrote: A known bug? prolly. segfault handler is highly non-standard hack, it may miss exact position or something. as is "it is not guaranteed to work, and if it will work, it is not guaranteed to work correctly".

Re: Stacktrace on Null Pointer Derefence

2016-09-22 Thread Nordlöw via Digitalmars-d-learn
On Thursday, 22 September 2016 at 00:46:19 UTC, ketmar wrote: { import etc.linux.memoryerror; registerMemoryErrorHandler(); } Thx! That at least triggered an exception. However the line number for the innermost function is wrong. For instance 1 void boom() 2 { 3 int* y = null; 4

Re: Stacktrace on Null Pointer Derefence

2016-09-21 Thread Jonathan Marler via Digitalmars-d-learn
On Wednesday, 21 September 2016 at 23:36:08 UTC, Nordlöw wrote: Doing a null deref such as int* y = null; *y = 42;// boom [...] Can you include compiler command line? I use -g -gs -debug to get stack traces on windows.

Re: Stacktrace on Null Pointer Derefence

2016-09-21 Thread ketmar via Digitalmars-d-learn
On Wednesday, 21 September 2016 at 23:36:08 UTC, Nordlöw wrote: Is there a way to get a stacktrace instead? { import etc.linux.memoryerror; registerMemoryErrorHandler(); } warning: this is hightly system-specific, and may work or not work depending of the moon phase, and may broke on any

Stacktrace on Null Pointer Derefence

2016-09-21 Thread Nordlöw via Digitalmars-d-learn
Doing a null deref such as int* y = null; *y = 42;// boom currently outputs /home/per/.emacs.d/auto-builds/dmd/Debug-Unittest-Require-Full-Coverage/home/per/foo[0x406474] /lib/x86_64-linux-gnu/libpthread.so.0(+0x113d0)[0x7f08cf25e3d0] /home/per/.emacs.d/auto-builds

Re: null as parametr

2016-07-31 Thread Andrew Godfrey via Digitalmars-d-learn
On Sunday, 31 July 2016 at 05:41:55 UTC, AntonSotov wrote: 2 Seb Thank you! is (T: typeof (null)) - very comfortable An example of Seb's warning: What happens if you have: string s = null; MyFunc(s); I'm guessing it doesn't do what you want. But it isn't clear what you want - null

Re: null as parametr

2016-07-30 Thread AntonSotov via Digitalmars-d-learn
2 Seb Thank you! is (T: typeof (null)) - very comfortable

Re: null as parametr

2016-07-30 Thread Seb via Digitalmars-d-learn
// WHAT HERE ? writeln("null"); } } int main(string[] args) { myFunc("abc"); myFunc(123); myFunc(null); return 0; } //-- How to transfer as parameter type? just have a look with pragma

null as parametr

2016-07-30 Thread AntonSotov via Digitalmars-d-learn
import std.stdio; void myFunc(T)(in T val) { static if(is(T == string)) { writeln("string: ", val); } static if(is(T : long)) { writeln("long: ", val); } static if // WHAT HERE ? writeln("null");

Re: Trouble checking for null-ness

2016-07-25 Thread Bahman Movaqar via Digitalmars-d-learn
On 07/25/2016 05:47 PM, Adam D. Ruppe wrote: > On Monday, 25 July 2016 at 13:09:22 UTC, Bahman Movaqar wrote: >> From what I could gather, it's not possible to check for `null` at >> runtime for reference based types. Am I right? > > No, it is only possible to check for nul

Re: Trouble checking for null-ness

2016-07-25 Thread Adam D. Ruppe via Digitalmars-d-learn
On Monday, 25 July 2016 at 13:09:22 UTC, Bahman Movaqar wrote: From what I could gather, it's not possible to check for `null` at runtime for reference based types. Am I right? No, it is only possible to check for null for reference based types. But map's result is not a reference based

<    1   2   3   4   5   6   7   8   9   >