Thread overview
Detect uninitialized class var access
Sep 24, 2022
rassoc
Sep 24, 2022
Adam D Ruppe
Sep 24, 2022
rassoc
Sep 25, 2022
Tejas
Sep 25, 2022
Paul Backus
Sep 25, 2022
Salih Dincer
Sep 26, 2022
wjoe
Sep 26, 2022
rikki cattermole
Sep 26, 2022
wjoe
September 24, 2022
Recently I refactored some old code of mine, now utilizing classes instead structs, and I got hit by an uninitialized variable access pattern similar to the simplified example below.

How can we easily spot this? What other switches and safeguards are there?

> dmd -dip1000 -debug -g -w

just crashes with `Error: program killed by signal 11` on linux and nothing else.

```d
import std;

@safe:

class Foo { int x; }

void main() {
    Foo f; // = new Foo;
    writeln(f.x);
}
```
September 24, 2022
On Saturday, 24 September 2022 at 13:17:19 UTC, rassoc wrote:
> just crashes with `Error: program killed by signal 11` on linux and nothing else.

gdb --args ./your_program

and then it will tell you all the details you want to know about when this happens.

strangly sometimes adding the -O option will cause dmd to catch simple cases at compile time but gdb is the general solution to track these down.
September 24, 2022
On 9/24/22 15:28, Adam D Ruppe via Digitalmars-d-learn wrote:
> gdb --args ./your_program
> 
> and then it will tell you all the details you want to know about when this happens.

Thank you for your input, Adam.  Real shame that there's no built-in compiler solution and probably never will be according to [1]. :(

D's competition got stuff like this right:

```
> crystal build foo.cr
In foo.cr:6:6

 6 | puts f.x
          ^
Error: read before assignment to local variable 'f'
```

[1] https://issues.dlang.org/show_bug.cgi?id=4595
September 25, 2022

On Saturday, 24 September 2022 at 13:17:19 UTC, rassoc wrote:

>

Recently I refactored some old code of mine, now utilizing classes instead structs, and I got hit by an uninitialized variable access pattern similar to the simplified example below.

I think changing the structure will make things harder (I mean in general). I know most people will object; but classes are useless. Structs are enough for you for the rest of your life 😀

SDB@79

September 25, 2022

On Saturday, 24 September 2022 at 23:04:00 UTC, rassoc wrote:

>

On 9/24/22 15:28, Adam D Ruppe via Digitalmars-d-learn wrote:

>

gdb --args ./your_program

and then it will tell you all the details you want to know about when this happens.

Thank you for your input, Adam. Real shame that there's no built-in compiler solution and probably never will be according to [1]. :(

D's competition got stuff like this right:

> crystal build foo.cr
In foo.cr:6:6

 6 | puts f.x
          ^
Error: read before assignment to local variable 'f'

[1] https://issues.dlang.org/show_bug.cgi?id=4595

AFAIK diagnostics like that are not possible because Walter doesn't want to implement full blown dataflow analysis in the compiler since it will slow down the compilation speed of dmd

September 25, 2022

On Sunday, 25 September 2022 at 03:04:45 UTC, Tejas wrote:

>

On Saturday, 24 September 2022 at 23:04:00 UTC, rassoc wrote:

>

On 9/24/22 15:28, Adam D Ruppe via Digitalmars-d-learn wrote:

>

gdb --args ./your_program

and then it will tell you all the details you want to know about when this happens.

Thank you for your input, Adam. Real shame that there's no built-in compiler solution and probably never will be according to [1]. :(

[1] https://issues.dlang.org/show_bug.cgi?id=4595

AFAIK diagnostics like that are not possible because Walter doesn't want to implement full blown dataflow analysis in the compiler since it will slow down the compilation speed of dmd

Implementing such a diagnostic is equivalent, in the general case, to solving the halting problem. So even with dataflow analysis, there would always be cases where the compiler either fails to detect a potential null dereference (false negative), or detects one that can't actually happen at runtime (false positive).

September 26, 2022

On Sunday, 25 September 2022 at 02:10:00 UTC, Salih Dincer wrote:

>

On Saturday, 24 September 2022 at 13:17:19 UTC, rassoc wrote:

>

Recently I refactored some old code of mine, now utilizing classes instead structs, and I got hit by an uninitialized variable access pattern similar to the simplified example below.

I think changing the structure will make things harder (I mean in general). I know most people will object; but classes are useless. Structs are enough for you for the rest of your life 😀

SDB@79

How would you do this without classes:

interface ForwardRange
{
  // ...
}

class Foo: ForwardRange
{
   // ...
}

class Bar: ForwardRange
{
  // ...
}

struct Baz
{
   ForwardRange r;

   this(ForwardRange r_)
   {
     r = r_;
   }
}
September 27, 2022
Currently in D you would be forced to create a vtable struct manually.

But if we had something like signatures you could do this:

```d
struct Foo {
	//...
}

struct Bar {
	InputRange input;
}

void doIt() {
	Bar bar;
	Foo* foo = new Foo;
	bar.input = foo;
}
```

Classes are not the only way to do OOP :)

September 26, 2022
On Monday, 26 September 2022 at 16:51:11 UTC, rikki cattermole wrote:
> Currently in D you would be forced to create a vtable struct manually.

Or I could just use classes.
The cure shouldn't be worse than the disease.

> But if we had something like signatures you could do this:
>
> ```d
> struct Foo {
> 	//...
> }
>
> struct Bar {
> 	InputRange input;
> }
>
> void doIt() {
> 	Bar bar;
> 	Foo* foo = new Foo;
> 	bar.input = foo;
> }
> ```

This would be nice but since we don't it doesn't really matter. unfortunately.