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





Reply via email to