digitalmars.D - immutable
- Robert (17/17) Jan 26 2011 Hello,
- Steven Schveighoffer (8/26) Jan 26 2011 It shouldn't. I don't know where the bug is. Please file a bug with a ...
- Trass3r (1/3) Jan 26 2011 Does it also segfault with string buf?
- Steven Schveighoffer (3/6) Jan 26 2011 No. Now I'm confused :)
- Trass3r (3/8) Jan 26 2011 On Linux strings are put into some read-only space.
- Trass3r (1/1) Jan 26 2011 (readln only uses ~= on buf, it doesn't change the original string)
- Steven Schveighoffer (5/6) Jan 26 2011 What? Why does it take a ref argument then? If it doesn't overwrite th...
- Trass3r (3/6) Jan 26 2011 No I meant it doesn't alter buf's original content, i.e. "hello"
- Steven Schveighoffer (10/17) Jan 26 2011 Then why not return the newly-created buffer? Why alter the buffer via ...
- spir (8/23) Jan 26 2011 Yes, that's how I think readln's API should be. Is a buf version for rep...
- Andrei Alexandrescu (3/29) Jan 26 2011 There is an overload of readln that looks like that.
- Steven Schveighoffer (5/37) Jan 26 2011 Then is my hypothesis correct that readln is *supposed* to overwrite the...
- Ellery Newcomer (32/66) Jan 26 2011 I'd guess this is the problem:
- Nick Sabalausky (4/7) Jan 26 2011 Automatically converts s from string to wstring and iterates over the
- Robert (5/5) Jan 26 2011 I didn't expect the code to run, hence my question. I tried it on OSX fi...
- Steven Schveighoffer (5/10) Jan 26 2011 The code shouldn't even compile. But the segfault is OS dependent (as y...
- Trass3r (2/3) Jan 26 2011 Rats, I've filed one too ;)
- Robert (1/1) Jan 26 2011 Hopefully they will give it double the attention then ;)
- Tomek =?ISO-8859-2?Q?Sowi=F1ski?= (5/9) Jan 27 2011 We found it over a year ago :)
- Trass3r (2/2) Jan 26 2011 This is a serious bug.
Hello, I have just recently started programming in D (a very pleasant experience so far I must say), but when experimenting with the the immutable attribute I discovered that the following code does not generate a compile time nor a runtime error: //Decalare immutable string immutable char[] buf = "hello"; //Print the value of buf writefln("buf = %s",buf); //Change buf by using standard input stdin.readln(buf); //Print buf again writefln("buf = %s",buf); This is a bit confusing to be because I had assumed that immutable data really would be immutable (without casting). Why does the code above work? Cheers Nilew
Jan 26 2011
On Wed, 26 Jan 2011 15:32:58 -0500, Robert <robert.welin hotmail.com> wrote:Hello, I have just recently started programming in D (a very pleasant experience so far I must say), but when experimenting with the the immutable attribute I discovered that the following code does not generate a compile time nor a runtime error: //Decalare immutable string immutable char[] buf = "hello"; //Print the value of buf writefln("buf = %s",buf); //Change buf by using standard input stdin.readln(buf); //Print buf again writefln("buf = %s",buf); This is a bit confusing to be because I had assumed that immutable data really would be immutable (without casting). Why does the code above work?It shouldn't. I don't know where the bug is. Please file a bug with a complete program (with a main() function) here: http://d.puremagic.com/issues/enter_bug.cgi BTW, this has a segfault in Linux, so it's definitely trying to overwrite immutable data. -Steve
Jan 26 2011
BTW, this has a segfault in Linux, so it's definitely trying to overwrite immutable data.Does it also segfault with string buf?
Jan 26 2011
On Wed, 26 Jan 2011 15:47:23 -0500, Trass3r <un known.com> wrote:No. Now I'm confused :) -SteveBTW, this has a segfault in Linux, so it's definitely trying to overwrite immutable data.Does it also segfault with string buf?
Jan 26 2011
On Linux strings are put into some read-only space. I guess an immutable(char[]) also puts the pointer and length into that storage.No. Now I'm confused :)BTW, this has a segfault in Linux, so it's definitely trying to overwrite immutable data.Does it also segfault with string buf?
Jan 26 2011
(readln only uses ~= on buf, it doesn't change the original string)
Jan 26 2011
On Wed, 26 Jan 2011 15:52:25 -0500, Trass3r <un known.com> wrote:(readln only uses ~= on buf, it doesn't change the original string)What? Why does it take a ref argument then? If it doesn't overwrite the buffer passed in, there is no point in giving it a buffer. readln needs a serious redesign it looks like. -Steve
Jan 26 2011
No I meant it doesn't alter buf's original content, i.e. "hello" Of course it modifies the array itself via ~= and thus takes it as a ref. Though it should use 'out' instead I think.(readln only uses ~= on buf, it doesn't change the original string)What? Why does it take a ref argument then? If it doesn't overwrite the buffer passed in, there is no point in giving it a buffer.
Jan 26 2011
On Wed, 26 Jan 2011 16:13:41 -0500, Trass3r <un known.com> wrote:Then why not return the newly-created buffer? Why alter the buffer via a parameter? It makes no sense to me. Better API: char[] readln(); or if you want different char types: C[] readln(C = char)(); I think the original intent was for the code to overwrite the buffer, but it doesn't take into account the append improvements circa 2.041. -SteveNo I meant it doesn't alter buf's original content, i.e. "hello" Of course it modifies the array itself via ~= and thus takes it as a ref. Though it should use 'out' instead I think.(readln only uses ~= on buf, it doesn't change the original string)What? Why does it take a ref argument then? If it doesn't overwrite the buffer passed in, there is no point in giving it a buffer.
Jan 26 2011
On 01/26/2011 10:21 PM, Steven Schveighoffer wrote:On Wed, 26 Jan 2011 16:13:41 -0500, Trass3r <un known.com> wrote:Yes, that's how I think readln's API should be. Is a buf version for repeated use? (I guess no, since input comes from a user?) Denis -- _________________ vita es estrany spir.wikidot.comThen why not return the newly-created buffer? Why alter the buffer via a parameter? It makes no sense to me. Better API: char[] readln(); or if you want different char types: C[] readln(C = char)();No I meant it doesn't alter buf's original content, i.e. "hello" Of course it modifies the array itself via ~= and thus takes it as a ref. Though it should use 'out' instead I think.(readln only uses ~= on buf, it doesn't change the original string)What? Why does it take a ref argument then? If it doesn't overwrite the buffer passed in, there is no point in giving it a buffer.
Jan 26 2011
On 1/26/11 6:24 PM, spir wrote:On 01/26/2011 10:21 PM, Steven Schveighoffer wrote:There is an overload of readln that looks like that. AndreiOn Wed, 26 Jan 2011 16:13:41 -0500, Trass3r <un known.com> wrote:Yes, that's how I think readln's API should be. Is a buf version for repeated use? (I guess no, since input comes from a user?) DenisThen why not return the newly-created buffer? Why alter the buffer via a parameter? It makes no sense to me. Better API: char[] readln(); or if you want different char types: C[] readln(C = char)();No I meant it doesn't alter buf's original content, i.e. "hello" Of course it modifies the array itself via ~= and thus takes it as a ref. Though it should use 'out' instead I think.(readln only uses ~= on buf, it doesn't change the original string)What? Why does it take a ref argument then? If it doesn't overwrite the buffer passed in, there is no point in giving it a buffer.
Jan 26 2011
On Wed, 26 Jan 2011 19:46:43 -0500, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:On 1/26/11 6:24 PM, spir wrote:Then is my hypothesis correct that readln is *supposed* to overwrite the buffer? It currently doesn't. -SteveOn 01/26/2011 10:21 PM, Steven Schveighoffer wrote:There is an overload of readln that looks like that.On Wed, 26 Jan 2011 16:13:41 -0500, Trass3r <un known.com> wrote:Yes, that's how I think readln's API should be. Is a buf version for repeated use? (I guess no, since input comes from a user?) DenisThen why not return the newly-created buffer? Why alter the buffer via a parameter? It makes no sense to me. Better API: char[] readln(); or if you want different char types: C[] readln(C = char)();No I meant it doesn't alter buf's original content, i.e. "hello" Of course it modifies the array itself via ~= and thus takes it as a ref. Though it should use 'out' instead I think.(readln only uses ~= on buf, it doesn't change the original string)What? Why does it take a ref argument then? If it doesn't overwrite the buffer passed in, there is no point in giving it a buffer.
Jan 26 2011
On 01/26/2011 02:40 PM, Steven Schveighoffer wrote:On Wed, 26 Jan 2011 15:32:58 -0500, Robert <robert.welin hotmail.com> wrote:I'd guess this is the problem: void badurk(C,E)(ref C[] x, E y){ x ~= y; } void main(string[] args){ immutable char[] buf = "hello"; static assert(is(typeof(buf) == immutable(char[]))); badurk(buf,'a'); //compiler: la la, this is okay! } OT: this function confuses me: size_t readln(C)(ref C[] buf, dchar terminator = '\n') if (isSomeChar!C) { static if (is(C == char)) { enforce(p && p.handle, "Attempt to read from an unopened file."); return readlnImpl(p.handle, buf, terminator); } else { // TODO: optimize this string s = readln(terminator); if (!s.length) return 0; buf.length = 0; foreach (wchar c; s) //// <----- (?!) { buf ~= c; } return buf.length; } }Hello, I have just recently started programming in D (a very pleasant experience so far I must say), but when experimenting with the the immutable attribute I discovered that the following code does not generate a compile time nor a runtime error: //Decalare immutable string immutable char[] buf = "hello"; //Print the value of buf writefln("buf = %s",buf); //Change buf by using standard input stdin.readln(buf); //Print buf again writefln("buf = %s",buf); This is a bit confusing to be because I had assumed that immutable data really would be immutable (without casting). Why does the code above work?It shouldn't. I don't know where the bug is. Please file a bug with a complete program (with a main() function) here: http://d.puremagic.com/issues/enter_bug.cgi BTW, this has a segfault in Linux, so it's definitely trying to overwrite immutable data. -Steve
Jan 26 2011
"Ellery Newcomer" <ellery-newcomer utulsa.edu> wrote in message news:ihq1pm$2d2v$1 digitalmars.com...OT: this function confuses me: string s = readln(terminator); foreach (wchar c; s) //// <----- (?!)Automatically converts s from string to wstring and iterates over the wchars. It should be dchar, though, not wchar.
Jan 26 2011
I didn't expect the code to run, hence my question. I tried it on OSX first which might have been the reason. Ran it on Ubuntu too and got the expected segmentation fault. But thank you for the answer, I have filed the bug. Robert
Jan 26 2011
On Wed, 26 Jan 2011 16:12:58 -0500, Robert <robert.welin hotmail.com> wrote:I didn't expect the code to run, hence my question. I tried it on OSX first which might have been the reason. Ran it on Ubuntu too and got the expected segmentation fault.The code shouldn't even compile. But the segfault is OS dependent (as you discovered). Windows also will not segfault. -Steve
Jan 26 2011
But thank you for the answer, I have filed the bug.Rats, I've filed one too ;) http://d.puremagic.com/issues/show_bug.cgi?id=5492
Jan 26 2011
Hopefully they will give it double the attention then ;)
Jan 26 2011
Trass3r napisa=B3:We found it over a year ago :) http://d.puremagic.com/issues/show_bug.cgi?id=3D3534 --=20 TomekBut thank you for the answer, I have filed the bug.=20 Rats, I've filed one too ;) http://d.puremagic.com/issues/show_bug.cgi?id=3D5492
Jan 27 2011
This is a serious bug. http://d.puremagic.com/issues/show_bug.cgi?id=5492
Jan 26 2011