Read-only string

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

    Read-only string

    why the following string, 'str' is read-only?

    char *str = "test string";

    anyhow 'str' needs to be in memory. do you think, making 'str' red-only
    would gain performance? or, it is right?

  • Vladimir Oka

    #2
    Re: Read-only string


    v4vijayakumar wrote:[color=blue]
    > why the following string, 'str' is read-only?
    >
    > char *str = "test string";
    >
    > anyhow 'str' needs to be in memory. do you think, making 'str' red-only
    > would gain performance? or, it is right?[/color]

    Character pointer `str` is /not/ read-only. You are free to assign any
    other character pointer value to it. What /is/ read-only is the string
    literal somewhere in memory to which `str` points.

    One reason for this is that it enables embedded applications to put the
    string literal into (physically) read-only memory. This may or may not
    gain performance, but certainly saves RAM which in these systems tends
    to command a price premium.

    Comment

    • Nils O. Selåsdal

      #3
      Re: Read-only string

      v4vijayakumar wrote:[color=blue]
      > why the following string, 'str' is read-only?[/color]

      Because the C standard says so.
      (well, it says it's undefined behavior to modify a string
      literal)[color=blue]
      > char *str = "test string";
      >
      > anyhow 'str' needs to be in memory. do you think, making 'str' red-only[/color]
      str is not the same thing as "test string", str can be modified :-)
      [color=blue]
      > would gain performance? or, it is right?[/color]

      Comment

      • Marc Boyer

        #4
        Re: Read-only string

        Le 21-06-2006, v4vijayakumar <v4vijayakumar@ yahoo.com> a écrit :[color=blue]
        > why the following string, 'str' is read-only?
        >
        > char *str = "test string";[/color]

        Because that is the way the langage is defined ?
        You could wonder why the langage is designed this way.
        I was not there, but my few knoledges in compilation let
        me assume that, for some compiler, it was easier to do
        so (it allow to store it in read-only memory).

        Marc Boyer

        Comment

        • v4vijayakumar

          #5
          Re: Read-only string



          Vladimir Oka wrote:[color=blue]
          > v4vijayakumar wrote:[color=green]
          > > why the following string, 'str' is read-only?
          > >
          > > char *str = "test string";
          > >
          > > anyhow 'str' needs to be in memory. do you think, making 'str' red-only
          > > would gain performance? or, it is right?[/color]
          >
          > Character pointer `str` is /not/ read-only. You are free to assign any
          > other character pointer value to it. What /is/ read-only is the string
          > literal somewhere in memory to which `str` points.
          >
          > One reason for this is that it enables embedded applications to put the
          > string literal into (physically) read-only memory. This may or may not
          > gain performance, but certainly saves RAM which in these systems tends
          > to command a price premium.[/color]


          saving RAM?! anyhow still, you have to have these 12 bytes in RAM.

          Comment

          • v4vijayakumar

            #6
            Re: Read-only string



            Marc Boyer wrote:[color=blue]
            > Le 21-06-2006, v4vijayakumar <v4vijayakumar@ yahoo.com> a écrit :[color=green]
            > > why the following string, 'str' is read-only?
            > >
            > > char *str = "test string";[/color]
            >
            > Because that is the way the langage is defined ?
            > You could wonder why the langage is designed this way.
            > I was not there, but my few knoledges in compilation let
            > me assume that, for some compiler, it was easier to do
            > so (it allow to store it in read-only memory).
            >
            > Marc Boyer[/color]


            shouldn't it be the other way? it should be easier to programmers.

            Comment

            • Marc Boyer

              #7
              Re: Read-only string

              Le 21-06-2006, v4vijayakumar <v4vijayakumar@ yahoo.com> a écrit :[color=blue]
              > Marc Boyer wrote:[color=green]
              >> Le 21-06-2006, v4vijayakumar <v4vijayakumar@ yahoo.com> a écrit :[color=darkred]
              >> > why the following string, 'str' is read-only?
              >> >
              >> > char *str = "test string";[/color]
              >>
              >> Because that is the way the langage is defined ?
              >> You could wonder why the langage is designed this way.
              >> I was not there, but my few knoledges in compilation let
              >> me assume that, for some compiler, it was easier to do
              >> so (it allow to store it in read-only memory).[/color]
              >
              > shouldn't it be the other way? it should be easier to programmers.[/color]

              You could not expect C to be an 'universal' langage,
              to find a C compiler for every platform *and* to require
              too many things from the compiler.
              So, yes, the C langage does not requires behaviors
              that would be too hard/costly to implement (like a stop
              of the program when dereferencing null pointer for
              example, or out-of-bound array acces).

              Marc Boyer

              Comment

              • Richard Bos

                #8
                Re: Read-only string

                "v4vijayaku mar" <v4vijayakumar@ yahoo.com> wrote:
                [color=blue]
                > Vladimir Oka wrote:[color=green]
                > > v4vijayakumar wrote:[color=darkred]
                > > > why the following string, 'str' is read-only?
                > > >
                > > > char *str = "test string";
                > > >
                > > > anyhow 'str' needs to be in memory. do you think, making 'str' red-only
                > > > would gain performance? or, it is right?[/color]
                > >
                > > Character pointer `str` is /not/ read-only. You are free to assign any
                > > other character pointer value to it. What /is/ read-only is the string
                > > literal somewhere in memory to which `str` points.
                > >
                > > One reason for this is that it enables embedded applications to put the
                > > string literal into (physically) read-only memory. This may or may not
                > > gain performance, but certainly saves RAM which in these systems tends
                > > to command a price premium.[/color]
                >
                > saving RAM?![/color]

                In embedded devices, yes, why not? In large programs on a PC, yes, why
                not, if the string isn't "test string" but a 2000-entry array of
                200-character strings?
                [color=blue]
                > anyhow still, you have to have these 12 bytes in RAM.[/color]

                Says who? You put them in ROM, of course. ROM is often cheaper than RAM.
                In "normal" programs, you can put them in the executable image, save
                some superfluous string copying, and eliminate duplicates.

                Richard

                Comment

                • Marc Thrun

                  #9
                  Re: Read-only string

                  v4vijayakumar schrieb:[color=blue]
                  >
                  > Vladimir Oka wrote:[color=green]
                  >> v4vijayakumar wrote:[color=darkred]
                  >>> why the following string, 'str' is read-only?
                  >>>
                  >>> char *str = "test string";
                  >>>
                  >>> anyhow 'str' needs to be in memory. do you think, making 'str' red-only
                  >>> would gain performance? or, it is right?[/color]
                  >> Character pointer `str` is /not/ read-only. You are free to assign any
                  >> other character pointer value to it. What /is/ read-only is the string
                  >> literal somewhere in memory to which `str` points.
                  >>
                  >> One reason for this is that it enables embedded applications to put the
                  >> string literal into (physically) read-only memory. This may or may not
                  >> gain performance, but certainly saves RAM which in these systems tends
                  >> to command a price premium.[/color]
                  >
                  >
                  > saving RAM?! anyhow still, you have to have these 12 bytes in RAM.
                  >[/color]

                  No, they can safely be placed into ROM, as the compiler can assume that
                  you will never modify the string literal. If you do so, you invoke
                  undefined behaviour, so the compiler is not responsible for this. If you
                  want a modifiable string, use the string literal as an initializer for
                  an array:

                  char str[] = "test string";

                  --
                  Marc Thrun

                  Comment

                  • Richard Bos

                    #10
                    Re: Read-only string

                    "v4vijayaku mar" <v4vijayakumar@ yahoo.com> wrote:
                    [color=blue]
                    > Marc Boyer wrote:[color=green]
                    > > Le 21-06-2006, v4vijayakumar <v4vijayakumar@ yahoo.com> a =E9crit :[color=darkred]
                    > > > why the following string, 'str' is read-only?
                    > > >
                    > > > char *str = "test string";[/color]
                    > >
                    > > Because that is the way the langage is defined ?
                    > > You could wonder why the langage is designed this way.
                    > > I was not there, but my few knoledges in compilation let
                    > > me assume that, for some compiler, it was easier to do
                    > > so (it allow to store it in read-only memory).[/color]
                    >
                    > shouldn't it be the other way? it should be easier to programmers.[/color]

                    Why would it be easier for programmers? You can get the behaviour you
                    want using

                    char str[] = "test string";

                    and the efficient behaviour using

                    char *str = "test string";

                    Choice is good. Catering to stupid hack programmers is not good.

                    Richard

                    Comment

                    • Vladimir Oka

                      #11
                      Re: Read-only string


                      v4vijayakumar wrote:[color=blue]
                      > Vladimir Oka wrote:[color=green]
                      > > v4vijayakumar wrote:[color=darkred]
                      > > > why the following string, 'str' is read-only?
                      > > >
                      > > > char *str = "test string";
                      > > >
                      > > > anyhow 'str' needs to be in memory. do you think, making 'str' red-only
                      > > > would gain performance? or, it is right?[/color]
                      > >
                      > > Character pointer `str` is /not/ read-only. You are free to assign any
                      > > other character pointer value to it. What /is/ read-only is the string
                      > > literal somewhere in memory to which `str` points.
                      > >
                      > > One reason for this is that it enables embedded applications to put the
                      > > string literal into (physically) read-only memory. This may or may not
                      > > gain performance, but certainly saves RAM which in these systems tends
                      > > to command a price premium.[/color]
                      >
                      >
                      > saving RAM?![/color]

                      RAM in your mobile phone ("cell" if you're on the other side of the
                      pond), to give one example, can command a price premium compared to
                      other types of memory used in it (mainly Flash). Being generally
                      faster, it can also be better used for time critical task (reading
                      constant strings is rarely such a task).

                      The fact that PC RAM and HDD space is (comapratively) cheap these days
                      is irrelevant to that particular market segment. Don't be blinded by
                      the obvious.
                      [color=blue]
                      > anyhow still, you have to have these 12 bytes in RAM.[/color]

                      Why, if you care to explain?

                      Yes, if you want to modify them, but then you're going about it the
                      wrong way. At least in C.

                      Comment

                      • Vladimir Oka

                        #12
                        Re: Read-only string


                        v4vijayakumar wrote:[color=blue]
                        > Marc Boyer wrote:[color=green]
                        > > Le 21-06-2006, v4vijayakumar <v4vijayakumar@ yahoo.com> a écrit :[color=darkred]
                        > > > why the following string, 'str' is read-only?
                        > > >
                        > > > char *str = "test string";[/color]
                        > >
                        > > Because that is the way the langage is defined ?
                        > > You could wonder why the langage is designed this way.
                        > > I was not there, but my few knoledges in compilation let
                        > > me assume that, for some compiler, it was easier to do
                        > > so (it allow to store it in read-only memory).
                        > >
                        > > Marc Boyer[/color]
                        >
                        >
                        > shouldn't it be the other way? it should be easier to programmers.[/color]

                        But it is! It makes it easy for me to create cost/performance efficient
                        designs.

                        It also makes it easy for you, if you care to study language enough to
                        come across use of character arrays:

                        char str[] = "hello, world";

                        Comment

                        • Nils O. Selåsdal

                          #13
                          Re: Read-only string [OT]

                          v4vijayakumar wrote:[color=blue]
                          >
                          > Vladimir Oka wrote:[color=green]
                          >> v4vijayakumar wrote:[color=darkred]
                          >>> why the following string, 'str' is read-only?
                          >>>
                          >>> char *str = "test string";
                          >>>
                          >>> anyhow 'str' needs to be in memory. do you think, making 'str' red-only
                          >>> would gain performance? or, it is right?[/color]
                          >> Character pointer `str` is /not/ read-only. You are free to assign any
                          >> other character pointer value to it. What /is/ read-only is the string
                          >> literal somewhere in memory to which `str` points.
                          >>
                          >> One reason for this is that it enables embedded applications to put the
                          >> string literal into (physically) read-only memory. This may or may not
                          >> gain performance, but certainly saves RAM which in these systems tends
                          >> to command a price premium.[/color]
                          >
                          >
                          > saving RAM?! anyhow still, you have to have these 12 bytes in RAM.[/color]

                          Imagine an application with a lot of strings (my linux shell /bin/bash
                          seems to have almost 50KiB of text!).
                          Placing these strings in a read only "segment" can allow OSs to share
                          that part among multiple running instances of the application.
                          And it allows merging of strings too, ("World" and "Hello World" might
                          need just a 12 bytes of storage for those 2 strings + the pointers to
                          them) save a bit here and there , it might add up.

                          Comment

                          • Kenneth Brody

                            #14
                            Re: Read-only string

                            Vladimir Oka wrote:[color=blue]
                            >
                            > v4vijayakumar wrote:[/color]
                            [...][color=blue][color=green][color=darkred]
                            > > > > char *str = "test string";[/color][/color][/color]
                            [...][color=blue][color=green]
                            > > shouldn't it be the other way? it should be easier to programmers.[/color]
                            >
                            > But it is! It makes it easy for me to create cost/performance efficient
                            > designs.
                            >
                            > It also makes it easy for you, if you care to study language enough to
                            > come across use of character arrays:
                            >
                            > char str[] = "hello, world";[/color]

                            And things like this can lay in hiding in a program for years, or
                            even decades. Literally!

                            I had written code, back in the early 1980's, which included the
                            equivalent of:

                            char *filename = "foo.xxx";

                            ...

                            strcpy(filename +4,"bar");

                            This "worked" until sometime in the 1990's, when the program was
                            ported to a system which actually did place "foo.xxx" into a read-
                            only memory segment. (Note that it was still in RAM. It's just
                            that the hardware and O/S supported marking some pages of memory
                            as "read-only data".)

                            --
                            +-------------------------+--------------------+-----------------------+
                            | Kenneth J. Brody | www.hvcomputer.com | #include |
                            | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h> |
                            +-------------------------+--------------------+-----------------------+
                            Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>


                            Comment

                            • Kenneth Brody

                              #15
                              Re: Read-only string [OT]

                              "Nils O. Selåsdal" wrote:
                              [...][color=blue]
                              > And it allows merging of strings too, ("World" and "Hello World" might
                              > need just a 12 bytes of storage for those 2 strings + the pointers to
                              > them) save a bit here and there , it might add up.[/color]

                              I assume that it could also merge "Hello" and "Hello World", _if_ the
                              "Hello" were defined without the trailing nul?

                              Now, how does one define a pointer to 5 chars?

                              cdecl says:

                              cdecl> explain char (*foo)[5];
                              declare foo as pointer to array 5 of char

                              but gcc complaint on this:

                              char (*foo)[5] = "Hello";

                              with:

                              foo.c:1: warning: initialization from incompatible pointer type

                              At first, I thought that's because "Hello" is actually an array of 6
                              chars, but changing it to "1234" gives the same error.

                              --
                              +-------------------------+--------------------+-----------------------+
                              | Kenneth J. Brody | www.hvcomputer.com | #include |
                              | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h> |
                              +-------------------------+--------------------+-----------------------+
                              Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>

                              Comment

                              Working...