Hello I tried the example from the d homepage but it segfaults using both DMD and GDC (in the binary versions that are linked from the homepage). My host is a AMD Athlon64 running in i386 chroot using 'Debian sid'. Other D programs run fine. $ cat segfault.d import std.stdio; int main(char[][] args) { char[] s1 = "hello world"; char[] s2 = "goodbye "; s2[8..13] = s1[6..11]; // s2 is "goodbye world" return 0; } $ ltrace ./segfault ... calloc(1, 68) = 0x8061150 mmap(0, 0x100000, 3, 34, -1) = 0xf7d06000 calloc(2050, 4) = 0x8061198 calloc(2050, 4) = 0x80631a8 calloc(2050, 4) = 0x80651b8 malloc(256) = 0x80671c8 memset(0x80671c8, '013円', 256) = 0x80671c8 realloc(NULL, 4) = 0x80672d0 memmove(0x80672d4, 0x80672d0, 0, 0, 0x8061008) = 0x80672d4 memset(0x80671c8, '\n', 16) = 0x80671c8 pthread_mutex_lock(0x80610a8, 0x8061008, 0x1f35850, 0x8061008, 0x805ec58) = 0 calloc(2050, 4) = 0x80672e0 pthread_mutex_unlock(0x80610a8, 0x80672d8, 8200, 0x8061150, 0xf7d06ff0) = 0 pthread_mutex_lock(0x80610a8, 0x8061008, 0x80672d8, 0x8061008, 0x805f814) = 0 pthread_mutex_unlock(0x80610a8, 0x80610a8, 0x8061008, 0x80672d8, 0x8061008) = 0 pthread_attr_init(0xf7d07f20, 0x8061008, 0xffcd551c, 0x805959c, 0) = 0 pthread_mutex_init(0xf7d07f40, 0, 0x8061008, 0xffcd551c, 0x805959c) = 0 pthread_cond_init(0xf7d07f58, 0, 0x8061008, 0xffcd551c, 0x805959c) = 0 pthread_self(0x805ec58, 0x805b1c9, 0x805d2ec, 0x8061008, 1) = 0xf7e066c0 sigfillset(0xffcd553c) = 0 sigaction(10, 0xffcd5538, NULL) = 0 sigaction(12, 0xffcd5538, NULL) = 0 sem_init(0x8060ac8, 0, 0, 0x805b1c9, 0x805d2ec) = 0 malloc(8) = 0x80692f0 memset(0xf7d08f80, '000円', 124) = 0xf7d08f80 memset(0xf7d08f00, '000円', 124) = 0xf7d08f00 strlen("./segfault") = 10 memcpy(0x805a0bc, "world", 5 <unfinished ...> --- SIGSEGV (Segmentation fault) --- +++ killed by SIGSEGV +++
String constants are read-only on Linux, you need a .dup in your code (COW).
String literals are read only, hence attempting to write to them will produce undefined behavior.
So many examples from http://digitalmars.com/d/cppstrings.html do not work at all in Linux? Or not at all with the latest language specs? This is sad. I hope the rest of the advertised features from the example pages do at least work without unexpected segfaults... bye, -christian-
I've changed the component to "www.digitalmars.com", since the samples are definitely wrong. http://digitalmars.com/d/cppstrings.html #Copying a String #Filling a String These two need .dup added to prevent modifying read-only strings. #Conversions to C Strings The second sample needs foo(s1.ptr);
Hello Do you consider this a bug in the Linux compiler or just one in the documentation? As it is possible to modify a string made out of character constants like char[] s3 = ['H', 'e', 'l', 'l', 'o']; but not char[] s3 = "Hello"; I would argue that this is a bug. The language description on http://www.digitalmars.com/d/arrays.html does not mention the existance of "read-only" arrays neither. If they was supposed to exists there should be at least a boolean .readonly attribute so that functions that accepts char[] and even check their length can decide if they may or may not be able to modify the provided char[] buffer. So at least document this in the language reference, too, as the easy string handling through arrays with builtin length attribute will surely be one of the things C programmers attracts... bye, -christian- bye, -christian-
Fixed DMD 1.005
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル