Article: Finding memory bugs in D code with AddressSanitizer

Walter Bright newshound2 at digitalmars.com
Tue Dec 26 22:43:28 UTC 2017


I posted this on another thread. It succinctly points out what is the 
fundamental difference between C++ and D on memory safety:


C++:

     int foo(int* p) { return p[1]; }
     int bar(int i) { return foo(&i); }

     clang++ -c test.cpp -Wall


D:

     @safe:
     int foo(int* p) { return p[1]; }
     int bar(int i) {return foo(&i); }

     dmd -c test.d
     test.d(3): Error: safe function 'test.foo' cannot index pointer 'p'
     test.d(4): Error: cannot take address of parameter i in @safe function bar


I.e. in C++, writing memory safe code means using the right library functions. 
It is not checkable by the compiler. In D, it is checkable by the compiler.


More information about the Digitalmars-d-announce mailing list