When you declare an array of chars and store a string in it, where isthe position of the null character \0? And what happens to the unused memorylocations?

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

    When you declare an array of chars and store a string in it, where isthe position of the null character \0? And what happens to the unused memorylocations?

    When you declare an array of chars and store a string in it, where is
    the position of the null character \0? And what happens to the unused
    memory locations?

    #include <stdio.h>
    int main(void)
    {
    char gstring2[25] = "dudes";
    gstring2[5] = 'a';
    printf("%s \n", gstring2);
    return 0;
    }

    The output of main function was

    dudesa

    How come this code works, and the statement
    gstring2[5] = 'a';
    doesn't overwrite the null character?
  • Peter Nilsson

    #2
    Re: When you declare an array of chars and store a string in it,where is the position of the null character \0? And what happens to the unusedmemory locations?

    Gary wrote:
    When you declare an array of chars and store a string in it,
    where is the position of the null character \0?
    Wherever you ask it to be, if you ask there to be one.
    And what happens to the unused memory locations?
    Uninitialised elements of a struct or array object will
    be 'zero initialised'.
    #include <stdio.h>
    int main(void)
    {
    char gstring2[25] = "dudes";
    Same as...

    char gstring2[25] =
    { 'd', 'u', 'd', 'e', 's',
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    gstring2[5] = 'a';
    printf("%s \n", gstring2);
    return 0;
    }
    >
    The output of main function was
    >
    dudesa
    >
    How come this code works, and the statement
    gstring2[5] = 'a';
    doesn't overwrite the null character?
    It does overwrite the null character, which was followed by
    another one.

    You need to be careful though of situations like...

    char foo[5] = "dudes";

    C, unlike C++, allows such an initialisation. There is no
    terminating null stored as there is no room for it.

    --
    Peter

    Comment

    • Default User

      #3
      Re: When you declare an array of chars and store a string in it, where is the position of the null character \0? And what happens to the unused memory locations?

      Gary wrote:
      When you declare an array of chars and store a string in it, where is
      the position of the null character \0?
      After the last character of string data.
      And what happens to the unused
      memory locations?
      They are set to zero.
      #include <stdio.h>
      int main(void)
      {
      char gstring2[25] = "dudes";
      gstring2[5] = 'a';
      printf("%s \n", gstring2);
      return 0;
      }
      >
      The output of main function was
      >
      dudesa
      You overwrote the null terminator with a character.
      How come this code works,
      Because there were extra characters that were set to zero due to the
      partial initialization.
      and the statement
      gstring2[5] = 'a';
      doesn't overwrite the null character?
      It did, but a new terminator was in place.

      If you had this:


      char str[] = "dudes";

      Then you'd probably be in trouble. The next adjacent byte wouldn't even
      belong to your object so overwriting the null terminator would cause
      undefined behavior as soon as you tried a string operation.



      Brian

      Comment

      • arnuld

        #4
        Re: When you declare an array of chars and store a string in it, where is the position of the null character \0? And what happens to the unused memory locations?

        On Mon, 05 May 2008 15:17:44 -0700, Peter Nilsson wrote:
        You need to be careful though of situations like...
        >
        char foo[5] = "dudes";
        >
        C, unlike C++, allows such an initialisation. There is no
        terminating null stored as there is no room for it.

        yes and you get garbage on the screen:



        #include <stdio.h>


        int main( void )
        {
        char oye[2] = "ok";

        printf("%s\n", oye);

        return 0;
        }

        ============= OUTPUT =============
        /home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra test.c
        /home/arnuld/programs/C $ ./a.out
        okHßÿ¿3.L
        /home/arnuld/programs/C $



        it *accidentally* terminated because at some random place in memory it
        found the NULL ?



        --

        my email ID is @ the above blog.
        just check the "About Myself" page :)

        Comment

        • rio

          #5
          Re: When you declare an array of chars and store a string in it, where is the position of the null character \0? And what happens to the unused memory locations?


          "arnuld" <sunrise@see.si gs.invalidha scritto nel messaggio
          news:pan.2008.0 5.07.03.11.12.3 36850@see.sigs. invalid...
          >On Mon, 05 May 2008 15:17:44 -0700, Peter Nilsson wrote:
          >
          >You need to be careful though of situations like...
          >>
          > char foo[5] = "dudes";
          >>
          >C, unlike C++, allows such an initialisation. There is no
          >terminating null stored as there is no room for it.
          >
          >
          yes and you get garbage on the screen:
          >
          >
          >
          #include <stdio.h>
          >
          >
          int main( void )
          {
          char oye[2] = "ok";
          is it not better oye[4] = "ok"; ?
          "ok" is o+k+\0
          printf("%s\n", oye);
          >
          return 0;
          }
          >
          ============= OUTPUT =============
          /home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra test.c
          /home/arnuld/programs/C $ ./a.out
          okHßÿ¿3.L
          here someone can see that "\0" is not copied from "ok" to oye[2]
          /home/arnuld/programs/C $
          >
          >
          >
          it *accidentally* terminated because at some random place in memory it
          found the NULL ?
          >
          >
          >
          --

          my email ID is @ the above blog.
          just check the "About Myself" page :)
          >


          Comment

          • Default User

            #6
            Re: When you declare an array of chars and store a string in it, where is the position of the null character \0? And what happens to the unused memory locations?

            arnuld wrote:
            On Mon, 05 May 2008 15:17:44 -0700, Peter Nilsson wrote:
            >
            You need to be careful though of situations like...

            char foo[5] = "dudes";

            C, unlike C++, allows such an initialisation. There is no
            terminating null stored as there is no room for it.
            >
            >
            yes and you get garbage on the screen:
            printf("%s\n", oye);
            No, you get undefined behavior.




            Brian

            Comment

            • Ben Bacarisse

              #7
              Re: When you declare an array of chars and store a string in it, where is the position of the null character \0? And what happens to the unused memory locations?

              "rio" <a@b.cwrites:
              "arnuld" <sunrise@see.si gs.invalidha scritto nel messaggio
              news:pan.2008.0 5.07.03.11.12.3 36850@see.sigs. invalid...
              >>On Mon, 05 May 2008 15:17:44 -0700, Peter Nilsson wrote:
              >>
              >>You need to be careful though of situations like...
              >>>
              >> char foo[5] = "dudes";
              >>>
              >>C, unlike C++, allows such an initialisation. There is no
              >>terminating null stored as there is no room for it.
              >>
              >yes and you get garbage on the screen:
              >>
              >#include <stdio.h>
              >>
              >int main( void )
              >{
              > char oye[2] = "ok";
              >
              is it not better oye[4] = "ok"; ?
              "ok" is o+k+\0
              Not to illustrate the point, no. Both Peter Nilsson and Arnuld posted
              code that has an array that is not a string, and in both cases it was
              deliberate.

              If you want to fix the problem, it is hard to beat 'char oye[] = "ok";'
              since you then don't need a size. If you must have a size, anything
              other than 3 will be mildly confusing in this example.
              <snip>

              Best to trim your replies a bit more. In particular, remove sig blocks
              unless you are commenting on them.

              --
              Ben.

              Comment

              • rio

                #8
                Re: When you declare an array of chars and store a string in it, where is the position of the null character \0? And what happens to the unused memory locations?

                "arnuld" <sunrise@see.si gs.invalidha scritto nel messaggio
                news:pan.2008.0 5.07.03.11.12.3 36850@see.sigs. invalid...
                >On Mon, 05 May 2008 15:17:44 -0700, Peter Nilsson wrote:
                >
                >You need to be careful though of situations like...
                >>
                > char foo[5] = "dudes";
                >>
                >C, unlike C++, allows such an initialisation. There is no
                >terminating null stored as there is no room for it.
                >
                >
                yes and you get garbage on the screen:
                >
                >
                >
                #include <stdio.h>
                >
                >
                int main( void )
                {
                char oye[2] = "ok";
                is it not better oye[4] = "ok"; ?
                "ok" is o+k+\0
                printf("%s\n", oye);
                >
                return 0;
                }
                >
                ============= OUTPUT =============
                /home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra test.c
                /home/arnuld/programs/C $ ./a.out
                okHßÿ¿3.L
                here someone can see that "\0" is not copied from "ok" to oye[2]
                /home/arnuld/programs/C $
                >
                >
                >
                it *accidentally* terminated because at some random place in memory it
                found the NULL ?
                >
                >
                >
                --

                my email ID is @ the above blog.
                just check the "About Myself" page :)
                >



                Comment

                • rio

                  #9
                  Re: When you declare an array of chars and store a string in it, where is the position of the null character \0? And what happens to the unused memory locations?

                  "arnuld" <sunrise@see.si gs.invalidha scritto nel messaggio
                  news:pan.2008.0 5.07.03.11.12.3 36850@see.sigs. invalid...
                  >On Mon, 05 May 2008 15:17:44 -0700, Peter Nilsson wrote:
                  >
                  >You need to be careful though of situations like...
                  >>
                  > char foo[5] = "dudes";
                  >>
                  >C, unlike C++, allows such an initialisation. There is no
                  >terminating null stored as there is no room for it.
                  >
                  >
                  yes and you get garbage on the screen:
                  >
                  >
                  >
                  #include <stdio.h>
                  >
                  >
                  int main( void )
                  {
                  char oye[2] = "ok";
                  is it not better oye[4] = "ok"; ?
                  "ok" is o+k+\0
                  printf("%s\n", oye);
                  >
                  return 0;
                  }
                  >
                  ============= OUTPUT =============
                  /home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra test.c
                  /home/arnuld/programs/C $ ./a.out
                  okHßÿ¿3.L
                  here someone can see that "\0" is not copied from "ok" to oye[2]
                  /home/arnuld/programs/C $
                  >
                  >
                  >
                  it *accidentally* terminated because at some random place in memory it
                  found the NULL ?
                  >
                  >
                  >
                  --

                  my email ID is @ the above blog.
                  just check the "About Myself" page :)
                  >



                  Comment

                  Working...