www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Is this statement in http://dlang.org/dmd-windows.html still true?

reply Pierre Rouleau <prouleau001 gmail.com> writes:
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
next sibling parent reply "Bernard Helyer" <b.helyer gmail.com> writes:
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
parent Pierre Rouleau <prouleau001 gmail.com> writes:
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
prev sibling next sibling parent "Bernard Helyer" <b.helyer gmail.com> writes:
That being said, it does no harm (i.e. it isn't _wrong_).
Jun 25 2012
prev sibling next sibling parent reply "Jesse Phillips" <jessekphillips+D gmail.com> writes:
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
parent reply Pierre Rouleau <prouleau001 gmail.com> writes:
On 12-06-25 11:02 PM, Jesse Phillips wrote:
 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.

OK, understood. The last two sentences of my original post were misleading, allow me to correct that.
 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
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
parent Pierre Rouleau <prouleau001 gmail.com> writes:
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.

 Ali

Thanks Ali, I completely agree. - Pierre Rouleau
Jul 02 2012
prev sibling next sibling parent "Mehrdad" <wfunction hotmail.com> writes:
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
prev sibling next sibling parent "Jesse Phillips" <jessekphillips+D gmail.com> writes:
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:
 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.

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.
Jun 25 2012
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
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
parent Pierre Rouleau <prouleau001 gmail.com> writes:
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 posix

Thanks for clarifying this Jacob, -- Pierre Rouleau
Jul 01 2012