tricky array problem !

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

    tricky array problem !

    #include<stdio. h>
    int main(void){
    char a[]="abcde";
    char *p=a;
    p++;
    p++;
    p[2]='z';
    printf("%s\n",p );
    return 0;
    }

    output :
    cdz

    ok than ,

    #include<stdio. h>
    int main(void){
    char *p="abcde";
    p[2]='z';
    printf("%s\n",p );
    return 0;
    }

    output:
    Segmentation fault

    In both cases P is of the type char * than in one case the z can
    change and the othe csae z cnt change Why ?? Please explain ..

  • Ian Collins

    #2
    Re: tricky array problem !

    onkar wrote:
    #include<stdio. h>
    int main(void){
    char a[]="abcde";
    char *p=a;
    p++;
    p++;
    p[2]='z';
    printf("%s\n",p );
    return 0;
    }
    >
    output :
    cdz
    >
    ok than ,
    >
    #include<stdio. h>
    int main(void){
    char *p="abcde";
    This is a string literal, look it up in the FAQ, or the many threads
    asking this question.

    --
    Ian Collins.

    Comment

    • Richard Heathfield

      #3
      Re: tricky array problem !

      onkar said:
      #include<stdio. h>
      int main(void){
      char a[]="abcde";
      You own the array 'a'.
      char *p=a;
      p++;
      p++;
      p[2]='z';
      You change the array 'a'. No problem.
      char *p="abcde";
      You don't own the string literal "abcde".
      p[2]='z';
      You try to change it. Problem.

      --
      Richard Heathfield
      "Usenet is a strange place" - dmr 29/7/1999

      email: rjh at the above domain, - www.

      Comment

      • loic-dev@gmx.net

        #4
        Re: tricky array problem !

        Hello,
        #include<stdio. h>
        int main(void){
        char a[]="abcde";
        char *p=a;
        p++;
        p++;
        p[2]='z';
        printf("%s\n",p );
        return 0;
        }
        >
        output :
        cdz
        The array "a" is placed to the stack, and is initialized to "abcde".
        You can do whatever you want with that array (provided that the index
        is within the tolerated range).
        #include<stdio. h>
        int main(void){
        char *p="abcde";
        p[2]='z';
        printf("%s\n",p );
        return 0;
        }
        >
        output:
        Segmentation fault
        You have a pointer to a string literal, which is stored eventually to a
        read-only segment. In this case, trying to modify the content leads to
        a SIGSEGV.

        HTH,
        Loic.

        Comment

        • Christopher Benson-Manica

          #5
          Re: tricky array problem !

          loic-dev@gmx.net wrote:
          char a[]="abcde";
          The array "a" is placed to the stack, and is initialized to "abcde".
          It has automatic storage duration; automatic storage is often
          implemented as a stack, but need not be.
          char *p="abcde";
          You have a pointer to a string literal, which is stored eventually to a
          read-only segment.
          It may very well be stored in read-only memory, and certainly appears
          to be based on the behavior reported by the OP, but again, such need
          not be the case.

          --
          C. Benson Manica | I *should* know what I'm talking about - if I
          cbmanica(at)gma il.com | don't, I need to know. Flames welcome.

          Comment

          • loic-dev@gmx.net

            #6
            Re: tricky array problem !

            Hi Chris,
            char a[]="abcde";
            >
            The array "a" is placed to the stack, and is initialized to "abcde".
            >
            It has automatic storage duration; automatic storage is often
            implemented as a stack, but need not be.
            I am a system programmer, not a C standard guy... Just out of
            curiosity, could you give an example of OS/compiler which does not
            implement automatic variable as a stack? And what would be the
            implementation then?
            char *p="abcde";
            >
            You have a pointer to a string literal, which is stored eventually to a
            read-only segment.
            >
            It may very well be stored in read-only memory, and certainly appears
            to be based on the behavior reported by the OP, but again, such need
            not be the case.
            Absolutely. I guess that modifying a string literal is an "undefined
            behavior" accordingly to the C standard, right?

            Cheers,
            Loic.

            Comment

            • Keith Thompson

              #7
              Re: tricky array problem !

              "onkar" <onkar.n.m@gmai l.comwrites:
              #include<stdio. h>
              int main(void){
              char a[]="abcde";
              char *p=a;
              p++;
              p++;
              p[2]='z';
              printf("%s\n",p );
              return 0;
              }
              >
              output :
              cdz
              >
              ok than ,
              >
              #include<stdio. h>
              int main(void){
              char *p="abcde";
              p[2]='z';
              printf("%s\n",p );
              return 0;
              }
              >
              output:
              Segmentation fault
              The comp.lang.c FAQ is at <http://www.c-faq.com/>. You have asked
              question 1.32.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
              We must do something. This is something. Therefore, we must do this.

              Comment

              • Richard Heathfield

                #8
                Re: tricky array problem !

                loic-dev@gmx.net said:
                Hi Chris,
                >
                char a[]="abcde";
                >>
                The array "a" is placed to the stack, and is initialized to "abcde".
                >>
                >It has automatic storage duration; automatic storage is often
                >implemented as a stack, but need not be.
                >
                I am a system programmer, not a C standard guy... Just out of
                curiosity, could you give an example of OS/compiler which does not
                implement automatic variable as a stack? And what would be the
                implementation then?
                IIRC the cc51 compiler for the 8051 will sometimes place automatic objects
                in registers.

                <snip>
                I guess that modifying a string literal is an "undefined
                behavior" accordingly to the C standard, right?
                Correct.

                --
                Richard Heathfield
                "Usenet is a strange place" - dmr 29/7/1999

                email: rjh at the above domain, - www.

                Comment

                • Keith Thompson

                  #9
                  Re: tricky array problem !

                  Richard Heathfield <rjh@see.sig.in validwrites:
                  loic-dev@gmx.net said:
                  > char a[]="abcde";
                  >>>
                  >The array "a" is placed to the stack, and is initialized to "abcde".
                  >>>
                  >>It has automatic storage duration; automatic storage is often
                  >>implemented as a stack, but need not be.
                  >>
                  >I am a system programmer, not a C standard guy... Just out of
                  >curiosity, could you give an example of OS/compiler which does not
                  >implement automatic variable as a stack? And what would be the
                  >implementati on then?
                  >
                  IIRC the cc51 compiler for the 8051 will sometimes place automatic objects
                  in registers.
                  I think most compilers do that kind of thing; it's a relatively
                  straightforward optimization.

                  I think there is (or was?) some IBM mainframe system which doesn't
                  have a system stack in the usual sense; what would otherwise be a
                  stack frame for a function call is allocated on the heap. I'm sure
                  someone here has the details.

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                  We must do something. This is something. Therefore, we must do this.

                  Comment

                  • queniao

                    #10
                    Re: tricky array problem !


                    "Richard Heathfield дµÀ£º
                    "
                    onkar said:
                    >
                    #include<stdio. h>
                    int main(void){
                    char a[]="abcde";
                    >
                    You own the array 'a'.
                    >
                    char *p=a;
                    p++;
                    p++;
                    p[2]='z';
                    >
                    You change the array 'a'. No problem.
                    >
                    char *p="abcde";f
                    when you use " char * p " ,the pointer is wild. that's the problem.
                    >
                    You don't own the string literal "abcde".
                    >
                    p[2]='z';
                    >
                    You try to change it. Problem.
                    >
                    --
                    Richard Heathfield
                    "Usenet is a strange place" - dmr 29/7/1999

                    email: rjh at the above domain, - www.

                    Comment

                    • queniao

                      #11
                      Re: tricky array problem !


                      "onkar дµÀ£º
                      "
                      #include<stdio. h>
                      int main(void){
                      char a[]="abcde";
                      ' a ' is a pointer of initiated stack.
                      char *p=a;
                      ' p ' point the same location in memory
                      p++;
                      p++;
                      p[2]='z';
                      printf("%s\n",p );
                      return 0;
                      }
                      >
                      output :
                      cdz
                      >
                      ok than ,
                      >
                      #include<stdio. h>
                      int main(void){
                      char *p="abcde";
                      but ' p ' in here point to a random location( may be read-only
                      segement)
                      p[2]='z';
                      printf("%s\n",p );
                      return 0;
                      }
                      >
                      output:
                      Segmentation fault
                      >
                      In both cases P is of the type char * than in one case the z can
                      change and the othe csae z cnt change Why ?? Please explain ..

                      Comment

                      • Keith Thompson

                        #12
                        Re: tricky array problem !

                        "queniao" <pangxiangcai@g mail.comwrites:
                        "onkar дµÀ£º
                        "
                        [...]
                        >#include<stdio .h>
                        >int main(void){
                        > char *p="abcde";
                        but ' p ' in here point to a random location( may be read-only
                        segement)
                        No, "p" does not point to a random location. It points to the first
                        character of a string literal, consisting of the character "abcde"
                        followed by a '\0' character. For example, reading the value of *p is
                        well defined; it yields the value 'a'.

                        The problem, as several people have said, is that attempting to modify
                        a string literal invokes undefined behavior. If you're going to
                        initialize or assign a pointer to point into a string literal, it's
                        best to declare it as a pointer to const char:

                        const char *p = "abcde";

                        Then the compiler will (in most cases) detect any attempt to modify
                        the string literal.

                        --
                        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                        San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                        We must do something. This is something. Therefore, we must do this.

                        Comment

                        • Jack Klein

                          #13
                          Re: tricky array problem !

                          On 15 Dec 2006 18:08:21 -0800, "queniao" <pangxiangcai@g mail.com>
                          wrote in comp.lang.c:
                          >
                          "Richard Heathfield дµÀ£º
                          "
                          onkar said:
                          #include<stdio. h>
                          int main(void){
                          char a[]="abcde";
                          You own the array 'a'.
                          char *p=a;
                          p++;
                          p++;
                          p[2]='z';
                          You change the array 'a'. No problem.
                          char *p="abcde";f
                          >
                          when you use " char * p " ,the pointer is wild. that's the problem.
                          Either your command of the English language is very poor, or you do
                          not know what you are talking about at all.

                          There is no such thing as a "wild pointer" in C.

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

                          • Richard Heathfield

                            #14
                            Re: tricky array problem !

                            queniao said:
                            "Richard Heathfield" [wrote]:
                            >onkar said:
                            <snip>
                            char *p="abcde";f
                            >
                            when you use " char * p " ,the pointer is wild. that's the problem.
                            No, not really. It ought really to be const char *, but there's nothing
                            terribly wrong with pointing a char * at a string literal as long as you
                            don't write through that pointer.

                            --
                            Richard Heathfield
                            "Usenet is a strange place" - dmr 29/7/1999

                            email: rjh at the above domain, - www.

                            Comment

                            • Mark McIntyre

                              #15
                              Re: tricky array problem !

                              On 15 Dec 2006 13:43:08 -0800, in comp.lang.c , loic-dev@gmx.net
                              wrote:
                              >I am a system programmer, not a C standard guy... Just out of
                              >curiosity, could you give an example of OS/compiler which does not
                              >implement automatic variable as a stack? And what would be the
                              >implementati on then?
                              Most implementations will put automatics that are small enough into a
                              register. Its a common optimisation.
                              --
                              Mark McIntyre

                              "Debugging is twice as hard as writing the code in the first place.
                              Therefore, if you write the code as cleverly as possible, you are,
                              by definition, not smart enough to debug it."
                              --Brian Kernighan

                              Comment

                              Working...