www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - core dumped when run this simple D program.

reply Fox <linuxl4 sohu.com> writes:
import std.stdio;

class myClas_1 {
	int a;
}

void main() {
	myClas_1 c1;
	writeln(c1.a);
}


===================
what's wrong? dmd and gdc both can compile these code, but can't 
run either.
DMD64 D Compiler v2.109.1
gcc 14.2.1 20240824 (GCC)
fedora 41
thanks!
Aug 25
parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
Classes in D are heap objects, you have to allocate them.

``myClas_1 c1 = new myClas_1;``

The default is null.
Aug 26
parent Fox <linuxl4 sohu.com> writes:
On Monday, 26 August 2024 at 07:17:58 UTC, Richard (Rikki) Andrew 
Cattermole wrote:
 Classes in D are heap objects, you have to allocate them.

 ``myClas_1 c1 = new myClas_1;``

 The default is null.
thank you very much!
Aug 26