FAQ question (1.13)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric

    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~
  • Eric Sosman

    #2
    Re: FAQ question (1.13)

    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
    esosman@ieee-dot-org.invalid

    Comment

    • Jack Klein

      #3
      Re: FAQ question (1.13)

      On Fri, 06 Jun 2008 10:05:02 +0800, Eric <friendfish@gma il.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.l earn.c-c++

      Comment

      • Dan

        #4
        Re: FAQ question (1.13)

        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.


        Comment

        • Antoninus Twink

          #5
          Re: FAQ question (1.13)

          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.

          Comment

          • Flash Gordon

            #6
            Re: FAQ question (1.13)

            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

            Comment

            Working...