473,387 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

FAQ question (1.13)

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?
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?

Thanks a lot~
Jun 27 '08 #1
5 1465
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
Jun 27 '08 #2
On Fri, 06 Jun 2008 10:05:02 +0800, Eric <fr********@gmail.comwrote
in comp.lang.c:
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?
What's the ROM data?
Who assign the "ROM" area for our program?
What is the ROM data area? C does not define such a thing. On some
implementations, usually embedded systems, there might be ROM,
although it is more likely flash these days.
Who load the program to memory and how load const to the "ROM" area?
The C standard does not specify how a program gets loaded into memory,
if it needs to be loaded into memory, and how its execution starts.
This depends on the operating system, if any, or perhaps what they
call the BSP these days.
Is the OS protect the "ROM" area?
What OS? Which OS?
Thanks a lot~
The FAQ does not use the term ROM, it merely says "may be stored in
read-only memory". Note the use of "may".

What the C standard says if basically what the FAQ says.

A string literal in C is an unnamed array of characters. Its members
have the type char, and not the type const char. Attempting to modify
a string literal causes undefined behavior. Undefined behavior means
that C does not know or care what happens.

If you want to know how certain low-level things are done on a
specific compiler/platform combination, you need to ask in a group for
that specific combination. The language does not specify these
details, they are up to the implementation, and there are many
different methods used by different implementations.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jun 27 '08 #3
Dan
Here I know the difference of the two initialize, what I want know is
why the ROM data can not modify?
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?

Thanks a lot~
Usually char * one would be used on embedded microcontroller systems, to
avoid the string from taking up RAM space, instead the string is only stored
in ROM, which can't be wrote to. Obviously you cannot write to ROM. The OS
may protect any memory it sees fit from writing, which is usually done to
protect the program and the system in general in a multitasking enviroment.
if you need to modify the string, there is nothing stopping you allocating
new memory, copying in the string, and reassigning the same pointer to that
string. As the pointer may change its value.
Jun 27 '08 #4
On 6 Jun 2008 at 2:05, Eric wrote:
Here I know the difference of the two initialize, what I want know is
why the ROM data can not modify?
Who assign the "ROM" area for our program?
Who load the program to memory and how load const to the "ROM" area?
I believe you may have been confused by the terminology. Often when
people speak of ROM, they're talking about hardware that physically
cannot be written to. The FAQ answer is talking about an area of memory
that is designated read-only in software (usually by the OS). Even if
there is a hardware switch to mark a piece of memory as read-only, it
will be the OS that is responsible for setting that switch.

How exactly programs are loaded into memory and how they are started
varies with different OSes, but the basics are usually the same. There
are basically three parts to a program: uninitialized data, which goes
in the bss area; initialized data, which goes in the data area; and the
program instructions themselves, which go in the text area.

Within the data part, some executable formats (e.g. ELF) make a
distinction between read-only data (like string literals and perhaps
variables declared as const) and writable data. For example, in ELF
there are separate .data and .rodata sections. When you start up a
program, eventually a kernel function gets called (e.g. if you start a
program at your shell in Linux, probably the shell calls one of the
exec*() functions, which in turn invokes the kernel function
load_elf_binary()). This kernel function will put the .bss data from
your program into the bss segment of your program's address space, and
similarly for data and text. It will mark the text area as executable,
and it doesn't mark the read-only part of the data area as writable.

Jun 27 '08 #5
Antoninus Twink wrote, On 06/06/08 12:02:
On 6 Jun 2008 at 2:05, Eric wrote:
>Here I know the difference of the two initialize, what I want know is
why the ROM data can not modify?
Who assign the "ROM" area for our program?
Who load the program to memory and how load const to the "ROM" area?

I believe you may have been confused by the terminology. Often when
people speak of ROM, they're talking about hardware that physically
cannot be written to. The FAQ answer is talking about an area of memory
that is designated read-only in software (usually by the OS). Even if
there is a hardware switch to mark a piece of memory as read-only, it
will be the OS that is responsible for setting that switch.
Even when you need need to use ultra-violet light to erase the old data
and a 21V supply to write a new value and neither of those is present?
The FAQ is also talking about memory like that (which I've used) and all
the other variants of read-only memory *including* the types you are
talking about.
How exactly programs are loaded into memory and how they are started
varies with different OSes, but the basics are usually the same.
There are a *lot* of systems where the way the data and program gets in
to read-only memory is by being programmed there by some other device,
and this is an important consideration for *why* it is as it is.
There
are basically three parts to a program: uninitialized data, which goes
in the bss area; initialized data, which goes in the data area; and the
program instructions themselves, which go in the text area.
<snip>

A common but not universal model.
similarly for data and text. It will mark the text area as executable,
and it doesn't mark the read-only part of the data area as writable.
All very implementation specific.
--
Flash Gordon
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.