digitalmars.D - Is this statement in http://dlang.org/dmd-windows.html still true?
- Pierre Rouleau (15/15) Jun 25 2012 Hi all,
- Bernard Helyer (6/8) Jun 25 2012 Even then you have to actively try and break the type system
- Bernard Helyer (1/1) Jun 25 2012 That being said, it does no harm (i.e. it isn't _wrong_).
- Pierre Rouleau (5/6) Jun 25 2012 Understood, but it might be a good thing to qualify it. I am looking at...
- Jesse Phillips (9/11) Jun 25 2012 You have read this completely wrong.
- Mehrdad (5/8) Jun 25 2012 Isn't it compiler-specific behavior?
- Jesse Phillips (8/16) Jun 25 2012 I suppose this is the case, I was thinking the string literals
- Pierre Rouleau (33/43) Jul 01 2012 OK, understood. The last two sentences of my original post were
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (9/12) Jul 02 2012 "never a good idea" would be too permissive and welcoming. The
- Pierre Rouleau (4/17) Jul 02 2012 Thanks Ali, I completely agree.
- Jacob Carlborg (7/19) Jun 26 2012 This is still true for D1 which doesn't have immutable or const. Where
- Pierre Rouleau (4/9) Jul 01 2012 Thanks for clarifying this Jacob,
Hi all, inside http://dlang.org/dmd-windows.html#environment down in the section titled Differences between Windows and Linux versions, the statement is: "String literals are read-only under Linux. Attempting to write to them will cause a segment violation." This looks like an old and obsolete statement related to early implementations. Isn't the compiler preventing writing to write to string literals? Or is this statement there to want you not take the address of a string literal and attempt to write it via a pointer? In both case, I think the statement should be modified (removed or clarified). Am I reading this correctly? Thanks -- Pierre Rouleau
Jun 25 2012
It's out of date.Or is this statement there to want you not take the address of a string literal and attempt to write it via a pointer?Even then you have to actively try and break the type system auto p = cast(char*) "foo".ptr; And if you remove the brakes from your car, you shouldn't be terribly confused when you plow into a wall at 100 km/h. The statement should probably be removed or modified.
Jun 25 2012
That being said, it does no harm (i.e. it isn't _wrong_).
Jun 25 2012
On 12-06-25 9:06 PM, Bernard Helyer wrote:That being said, it does no harm (i.e. it isn't _wrong_).Understood, but it might be a good thing to qualify it. I am looking at D again (D2.0) and would like to start pushing it for my group at work. Unqualified statements like this might scare away some people to the detriment of D's popularity.
Jun 25 2012
On Tuesday, 26 June 2012 at 00:56:48 UTC, Pierre Rouleau wrote:"String literals are read-only under Linux. Attempting to write to them will cause a segment violation."You have read this completely wrong. It is still true today. Linux places string literals in Read-Only Memory, Windows does not. This is OS specific behavior and does not relate to the language in the least. The example is given to show why language support for immutability is important. It allows the language to define what is legal for the stored data.
Jun 25 2012
On Tuesday, 26 June 2012 at 03:02:45 UTC, Jesse Phillips wrote:Linux places string literals in Read-Only Memory, Windows does not. This is OS specific behavior and does not relate to the language in the least.Isn't it compiler-specific behavior? Visual C++ does this on Windows. After all, it's up to the compiler to place it in a read-only segment rather than a writable segment.
Jun 25 2012
On Tuesday, 26 June 2012 at 03:44:17 UTC, Mehrdad wrote:On Tuesday, 26 June 2012 at 03:02:45 UTC, Jesse Phillips wrote:I suppose this is the case, I was thinking the string literals were loaded as the program itself is loaded, and this was in a location not read-only. Guess I've made some poor associations/assumptions. Aside from that I still think the original issue with the statement is not and has not been valid. But if they can be RO in Windows it would seem a useless addition to specify Linux.Linux places string literals in Read-Only Memory, Windows does not. This is OS specific behavior and does not relate to the language in the least.Isn't it compiler-specific behavior? Visual C++ does this on Windows. After all, it's up to the compiler to place it in a read-only segment rather than a writable segment.
Jun 25 2012
On 12-06-25 11:02 PM, Jesse Phillips wrote:On Tuesday, 26 June 2012 at 00:56:48 UTC, Pierre Rouleau wrote:OK, understood. The last two sentences of my original post were misleading, allow me to correct that."String literals are read-only under Linux. Attempting to write to them will cause a segment violation."You have read this completely wrong.It is still true today. Linux places string literals in Read-Only Memory, Windows does not. This is OS specific behavior and does not relate to the language in the least. The example is given to show why language support for immutability is important. It allows the language to define what is legal for the stored data.Would it not be a good idea to clarify the statement on the web page by giving the explanation you just provided, making that topic a little bit more explicit than what it currently is? Given the fact that there is several angle this concept can be looked at: OS support implementation, language definition (D1.0, D2.0), use of string literal assigned to a variable that identifies a character as immutable, etc... Would the following rewrite of the above statement maintain the original intent while conveying a little bit more information? "It is never a good idea to write into a string literal. For instance, under Linux (or other Posix OS) string literals are stored in read-only memory and attempting to write to them will cause a segment violation. Some other OS (or compiler implementation) may not store string literals in read-only memory, the same code may seem to run OK on them. D data immutability provides protection against such invalid behavior. In D, string literals are declared as arrays of immutable characters. The compiler will not allow any code that attempts to write into immutable data, protecting you against attempting to write to string literals." Again, my point is to highlight the strengths of the D language and explicitly state where it shines instead of leaving it to interpretation. My second point was that it was not clear to me that the web-page original statement was identifying that string literals are implemented as array of immutable data. And finally, my last point is that I was not sure if this page referred to D 1.0 or D2.0. I am not a D expert yet, but from what I remembered D1.0 did not have support for immutable data. However, looking back at it it seems to refer to dmd2 only. Thanks for your reply and help. -- Pierre Rouleau
Jul 01 2012
On 07/01/2012 06:42 PM, Pierre Rouleau wrote:Would the following rewrite of the above statement maintain the original intent while conveying a little bit more information? "It is never a good idea to write into a string literal."never a good idea" would be too permissive and welcoming. The documentation should simply say that writing into a string literal is undefined behavior (exactly like in other popular system languages like C and C++). The current documentation gets into unnecessary detail of what may happen and why under different environments. Well, yes. :) It is undefined behavior. Ali
Jul 02 2012
On 12-07-02 4:58 AM, Ali Çehreli wrote:On 07/01/2012 06:42 PM, Pierre Rouleau wrote: > Would the following rewrite of the above statement maintain the original > intent while conveying a little bit more information? > > "It is never a good idea to write into a string literal. "never a good idea" would be too permissive and welcoming. The documentation should simply say that writing into a string literal is undefined behavior (exactly like in other popular system languages like C and C++). The current documentation gets into unnecessary detail of what may happen and why under different environments. Well, yes. :) It is undefined behavior. AliThanks Ali, I completely agree. - Pierre Rouleau
Jul 02 2012
On 2012-06-26 02:56, Pierre Rouleau wrote:Hi all, inside http://dlang.org/dmd-windows.html#environment down in the section titled Differences between Windows and Linux versions, the statement is: "String literals are read-only under Linux. Attempting to write to them will cause a segment violation." This looks like an old and obsolete statement related to early implementations. Isn't the compiler preventing writing to write to string literals? Or is this statement there to want you not take the address of a string literal and attempt to write it via a pointer? In both case, I think the statement should be modified (removed or clarified). Am I reading this correctly?This is still true for D1 which doesn't have immutable or const. Where this is legal: char[] str = "asd"; str[0] = 'b'; // ok on windows, segfault on posix -- /Jacob Carlborg
Jun 26 2012
On 12-06-26 6:34 AM, Jacob Carlborg wrote:On 2012-06-26 02:56, Pierre Rouleau wrote:This is still true for D1 which doesn't have immutable or const. Where this is legal: char[] str = "asd"; str[0] = 'b'; // ok on windows, segfault on posixThanks for clarifying this Jacob, -- Pierre Rouleau
Jul 01 2012