Re: Modify Static Data Okay?

From: Chris Torek (nospam_at_torek.net)
Date: 11/14/04


Date: 14 Nov 2004 18:13:22 GMT

In article <HpFld.10556$14.5913@read1.cgocable.net> Method Man <a@b.c> wrote:
>I always considered string literals as constant objects. Why are they
>considered non-const if they can not be modified? Is it because they're
>stored in a different area of memory than const objects?

String-literal-created arrays are in principle read-only, yes.
(Whether they are in fact read-only on any given implementation is
up to the implementation. Making them physically read-only usually
requires some kind of hardware level protection, whether it be in
the form of ROM, or page-protection, or similar; so systems without
such protection, or on which protections have been disabled for
some reason, the arrays will generally be write-able anyway.)

Now, given that "const" in C means, more or less, "read-only", I
suspect you mean to ask:

    The array created by the string literal "hello" has type
    "array 6 of char" instead of "array 6 of const char". Why?

The answer is, essentially, "history". Early C (before the original
1989 ANSI standard) had no "const" keyword, and hence no const-
qualified types. This meant that string-literal-created arrays
were *always* used with non-qualified pointers:

    char *p = "hello";

The X3J11 (ANSI C) committee folks did not want to break existing
code, but did want to require "const" in cases like this:

    const char s[6] = { 'h', 'e', 'l', 'l', 'o', '\0' };
    const char *p = s;

The only way to reconcile these two desires was to make string
literals produce arrays of type "array N of char" instead of "array
N of const char".

(I always thought -- and in fact still do think -- that they should
instead have given up on the second desire, and not made "const" a
type qualifier at all, but rather merely a storage-class modifier.
This would avoid the "const char *const *argv" problem:

    char **argv;
    ...
    const char *const *p = argv; /* must draw diagnostic in C */

This particular problem is fixed in a different way in C++, and C
could adopt the C++ rules; but note that C++ already makes the
arrays produced by string literals have type "const char [N]", too.
C and C++ are sufficiently different languages that it is often
not a good idea to move concepts between them casually.)

-- 
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: forget about it   http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.


Relevant Pages

  • Re: pointer to const string
    ... Making string literals const would have broken every single pre-ANSI ...
    (comp.lang.c)
  • Re: pointer to const string
    ... Making string literals const would have broken every single pre-ANSI ...
    (comp.lang.c)
  • Re: How to learn C ?
    ... string types are merely just collections of characters sitting next to each other in memory. ... whereas you can modify arrays. ... If they are talking about strings and string literals, then no, they ...
    (comp.lang.c)
  • Re: Read-only functionality without const
    ... How does the string 'hello' in first method lie in read-only memory ... Only 'const' provides the 'Read-only' functionality in C. ... The problem is that const was added to the language too late for it to be easy to make string literals const. ...
    (comp.lang.c)
  • Re: Is this correct?
    ... String literals may not be modified, ... literals should be const. ... char const * const p = "some string"; ...
    (comp.lang.c)