www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - crash D1 compiler

reply Zorran <zorran tut.by> writes:
This code crash D1 compiler (v1.039)

============
import std.stdio;

void main()
{
	string ss="sample";
	printf("%s", cast(char*)(ss+"\0") );
}
===========
Feb 01 2009
next sibling parent Extrawurst <spam extrawurst.org> writes:
Zorran wrote:
 This code crash D1 compiler (v1.039)
 
 ============
 import std.stdio;
 
 void main()
 {
 	string ss="sample";
 	printf("%s", cast(char*)(ss+"\0") );
 }
 ===========

do u mean it actually crashes dmd at compile time or do u mean your application carshes at runtime? try: void foo(char[] ss) { printf("my string is: %.*s\n", ss); } documented under: http://www.digitalmars.com/d/2.0/interfaceToC.html ("Calling printf()")
Feb 01 2009
prev sibling parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Sun, 01 Feb 2009 13:01:09 +0100, Zorran <zorran tut.by> wrote:

 This code crash D1 compiler (v1.039)

 ============
 import std.stdio;

 void main()
 {
 	string ss="sample";
 	printf("%s", cast(char*)(ss+"\0") );
 }
 ===========

Indeed it does. Now of course, ss + "\0" makes no sense, but the compiler still should not crash. To correctly concatenate two strings, use the ~ operator. Also, to convert a D string to a C string (char *), use toStringz, which automagically adds the terminating null. Your program would then look like this: void main() { string ss = "sample"; printf("%s", toStringz(ss)); } -- Simen
Feb 01 2009
parent Don <nospam nospam.com> writes:
Simen Kjaeraas wrote:
 On Sun, 01 Feb 2009 13:01:09 +0100, Zorran <zorran tut.by> wrote:
 
 This code crash D1 compiler (v1.039)

 ============
 import std.stdio;

 void main()
 {
     string ss="sample";
     printf("%s", cast(char*)(ss+"\0") );
 }
 ===========

Indeed it does. Now of course, ss + "\0" makes no sense, but the compiler still should not crash. To correctly concatenate two strings, use the ~ operator. Also, to convert a D string to a C string (char *), use toStringz, which automagically adds the terminating null. Your program would then look like this: void main() { string ss = "sample"; printf("%s", toStringz(ss)); } -- Simen

Feb 01 2009