Eric wrote:
Dear All:
In the process of reading the FAQ, I have some questions here is the
question from 1.13(in the new edition maybe 1.32).
quot:
================================================== ================
What is the difference between these initializations?
char a[] = "string literal";
char *p = "string literal";
My program crashes if I try to assign a new value to p[i].
================================================== ===============
Here I know the difference of the two initialize, what I want know is
why the ROM data can not modify?
Because "ROM" means "Read-Only Memory," that is,
memory for which only reads and not writes are valid.
If you cannot write to the memory containing an object,
you cannot modify that object.
Who assign the "ROM" area for our program?
Who load the program to memory and how load const to the "ROM" area?
Is the OS protect the "ROM" area?
All of this is implementation-specific, and varies
from one implementation to the next. Here are a few
possibilities:
- There is no ROM at all, and all objects can be
written even if writing to them is a bad idea.
- The O/S can control the read/write permissions
for different sections of memory, often called
"pages" of memory. While loading the program, the
O/S makes all pages writeable so it can store the
initial contents in them. Then before calling the
main() function, the O/S changes some of the pages
to read-only status.
- There is no O/S, or only a minimal O/S. The
program is loaded "at the factory," and some
items are allocated to memory chips that cannot
be written after manufacture. (Or perhaps they
can be rewritten or "re-flashed" afterwards, but
not by way of ordinary memory reads and writes.)
The C programmer should not be concerned with such
details, because the C programmer tries to write code
that will run on many different machines, and the details
will differ from one machine to the next. Here's what
the C programmer should think: "It is always possible
and safe to change the contents of a[], but it might
be impossible to change the contents of *p and is not
safe to do so even if it happens to be possible."
Note that "the C programmer" has a different point
of view than that of "the C implementor," who must deal
with the picky details of his own machine.
--
Eric Sosman
es*****@ieee-dot-org.invalid