Problem With C?

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

    #1

    Problem With C?

    Why does the following program runs and works correctly
    =============== =============== ===============
    #include <stdio.h>
    #include <string.h>

    char a = 'a'; / * One byte*/

    int main(int argc, char **argv)
    {

    printf("%s", &a);
    /* Prints a */

    /* Undefined behaviour on a*/
    strcpy (&a, "HELLOOOOOOOOOO OOOOOOOOOOOOOOO OOOO");

    printf("%s", &a);
    /* Should not print HELOOOO...... correctly */
    /*i Think it must print H */

    return 0;
    }


    =============== =============== =============

    I have copied a full string into a char and prints that string
    correctly ?
  • raashid bhatt

    #2
    Re: Problem With C?

    and Even more strange

    printf("%d", (int)sizeof(a)) ;

    Prints 1 (one byte) even after copying the whole string into it

    Comment

    • Ian Collins

      #3
      Re: Problem With C?

      raashid bhatt wrote:
      Why does the following program runs and works correctly
      By some queer definition of correctly!
      =============== =============== ===============
      #include <stdio.h>
      #include <string.h>
      >
      char a = 'a'; / * One byte*/
      >
      Typo.
      int main(int argc, char **argv)
      {
      >
      printf("%s", &a);
      /* Prints a */
      >
      By chance. The memory after a happens to be 0.
      /* Undefined behaviour on a*/
      strcpy (&a, "HELLOOOOOOOOOO OOOOOOOOOOOOOOO OOOO");
      >
      Completely undefined, everything depends on what memory you have
      overwritten.
      printf("%s", &a);
      /* Should not print HELOOOO...... correctly */
      /*i Think it must print H */
      >
      return 0;
      }
      >
      >
      =============== =============== =============
      >
      I have copied a full string into a char and prints that string
      correctly ?
      By chance. The block of memory after a happens to be 0 filled.

      --
      Ian Collins.

      Comment

      • Ian Collins

        #4
        Re: Problem With C?

        raashid bhatt wrote:
        and Even more strange
        >
        printf("%d", (int)sizeof(a)) ;
        >
        Prints 1 (one byte) even after copying the whole string into it
        a is a char and sizeof is a compiler time operator. What else would you
        expect?

        --
        Ian Collins.

        Comment

        • Barry Schwarz

          #5
          Re: Problem With C?

          On Fri, 26 Sep 2008 21:18:02 -0700 (PDT), raashid bhatt
          <raashidbhatt@g mail.comwrote:
          >Why does the following program runs and works correctly
          >============== =============== =============== =
          >#include <stdio.h>
          >#include <string.h>
          >
          >char a = 'a'; / * One byte*/
          >
          >int main(int argc, char **argv)
          >{
          >
          > printf("%s", &a);
          You invoke undefined behavior here.
          > /* Prints a */
          >
          > /* Undefined behaviour on a*/
          > strcpy (&a, "HELLOOOOOOOOOO OOOOOOOOOOOOOOO OOOO");
          And here also.

          One of the more unlucky manifestations of undefined behavior is to do
          what you expect. Murphy's law suggests that it will continue to do so
          until the most inopportune time when it will do something completely
          unexpected.
          >
          > printf("%s", &a);
          > /* Should not print HELOOOO...... correctly */
          > /*i Think it must print H */
          You can think whatever you want but you are just guessing.
          >
          > return 0;
          >}
          >
          >
          >============== =============== ==============
          >
          >I have copied a full string into a char and prints that string
          >correctly ?
          Once you invoke undefined behavior, there is no such thing as
          correctly.

          --
          Remove del for email

          Comment

          • blargg

            #6
            Re: Problem With C?

            In article <k9ird4h597jffd 0u0rm0i94ghl406 tb7nd@4ax.com>,
            Barry Schwarz <schwarzb@dqel. comwrote:

            [...]
            Once you invoke undefined behavior, there is no such thing as
            correctly.
            Don't you mean "there is no such thing as INcorrect"? Anything the
            program does in response undefined behavior is correct
            (standard-conforming).

            Comment

            • vippstar@gmail.com

              #7
              Re: Problem With C?

              On Sep 27, 9:38 am, blargg <blargg....@gis hpuppy.comwrote :
              In article <k9ird4h597jffd 0u0rm0i94ghl406 tb...@4ax.com>,
              Barry Schwarz <schwa...@dqel. comwrote:
              >
              [...]
              >
              Once you invoke undefined behavior, there is no such thing as
              correctly.
              >
              Don't you mean "there is no such thing as INcorrect"? Anything the
              program does in response undefined behavior is correct
              (standard-conforming).
              Don't snip context. There's no such thing as incorrect from the
              implementations view; there's no such thing as correct from the
              programmers view.

              Comment

              • John Bode

                #8
                Re: Problem With C?

                On Sep 26, 11:18 pm, raashid bhatt <raashidbh...@g mail.comwrote:
                Why does the following program runs and works correctly
                =============== =============== ===============
                #include <stdio.h>
                #include <string.h>
                >
                char a = 'a'; / * One byte*/
                >
                int main(int argc, char **argv)
                {
                >
                printf("%s", &a);
                /* Prints a */
                By sheer luck, the memory immediately following a is set to 0; you
                could have had 'a' followed by a stream of junk. You have invoked
                undefined behavior here. Had you written

                printf("%c\n", a);

                then you would have seen the output you expect and not have invoked
                UB.
                >
                /* Undefined behaviour on a*/
                strcpy (&a, "HELLOOOOOOOOOO OOOOOOOOOOOOOOO OOOO");
                >
                printf("%s", &a);
                /* Should not print HELOOOO...... correctly */
                /*i Think it must print H */
                >
                I'm curious as to why you would think that. The %s conversion
                specifier expects a pointer to a zero-terminated string of char; 'E'
                is not zero. If you want to just print 'H', you would again write

                printf("%c\n", a);

                Of course, you've already invoked UB with the strcpy() call (since
                you're writing to an object that was declared char, not char[N]), so
                there's no guarantee you'd see the output you want anyway.
                return 0;
                >
                }
                >
                =============== =============== =============
                >
                I have copied a full string into a char and prints that string
                correctly ?
                UB is working in your favor. That's usually bad news.

                Comment

                • John Bode

                  #9
                  Re: Problem With C?

                  On Sep 26, 11:21 pm, raashid bhatt <raashidbh...@g mail.comwrote:
                  and Even more strange
                  >
                  printf("%d", (int)sizeof(a)) ;
                  >
                  Prints 1 (one byte) even after copying the whole string into it
                  As it would; sizeof returns the size of the *type* of a, which is
                  char, which is by definition 1 byte.

                  Assume the following:

                  char a;
                  char ar[10] = "Hello";
                  char *ap = ar;

                  sizeof a == sizeof(char) == 1
                  sizeof ar == sizeof (char [10]) == 10
                  sizeof *ap == sizeof (char *) == however big a pointer type is (4
                  bytes on my system)

                  Note that sizeof ar is always 10, regardless of the length of the
                  string stored in it. strlen() will return the length of the string in
                  ar:

                  strlen(ar) == 5

                  Comment

                  • CBFalconer

                    #10
                    Re: Problem With C?

                    raashid bhatt wrote:
                    >
                    Why does the following program runs and works correctly
                    =============== =============== ===============
                    #include <stdio.h>
                    #include <string.h>
                    >
                    char a = 'a'; / * One byte*/
                    >
                    int main(int argc, char **argv) {
                    printf("%s", &a);
                    /* Prints a */
                    >
                    /* Undefined behaviour on a*/
                    strcpy (&a, "HELLOOOOOOOOOO OOOOOOOOOOOOOOO OOOO");
                    >
                    printf("%s", &a);
                    /* Should not print HELOOOO...... correctly */
                    /*i Think it must print H */
                    >
                    return 0;
                    }
                    >
                    I have copied a full string into a char and prints that string
                    correctly ?
                    Each of your printfs invokes undefined behaviour. So does the
                    strcpy line. That includes actually printing the value out, on
                    your machine, at this time.

                    --
                    [mail]: Chuck F (cbfalconer at maineline dot net)
                    [page]: <http://cbfalconer.home .att.net>
                    Try the download section.

                    Comment

                    • Peter Nilsson

                      #11
                      Re: Problem With C?

                      raashid bhatt <raashidbh...@g mail.comwrote:
                      Why does the following program runs and works correctly
                      It might run, it might even give 'expected' output, but it
                      is not correct and it seems you already know that.

                      Analysing deliberately broken code is a waste of time. It's
                      much more beneficial to study which constructs are correct.
                      =============== =============== ===============
                      #include <stdio.h>
                      #include <string.h>
                      >
                      char a = 'a'; / * One byte*/
                      >
                      int main(int argc, char **argv)
                      {
                        printf("%s", &a);
                        /* Prints a */
                      printf("%.1s", &a);
                        /* Undefined behaviour on a*/
                        strcpy (&a, "HELLOOOOOOOOOO OOOOOOOOOOOOOOO OOOO");
                      strncpy(&a, "HELLOOOOOOOOOO OOOOOOOOOOOOOOO OOOO", 1);
                        printf("%s", &a);
                      printf("%.1s", &a);
                        /* Should not print HELOOOO...... correctly */
                        /*i Think it must print H */
                        return 0;
                      }
                      --
                      Peter

                      Comment

                      Working...