tryick use of sizeof again[Explaination Wanted]

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

    #46
    Re: tryick use of sizeof again[Explaination Wanted]

    Harald van D k <true...@gmail. comwrote:
    Ian Collins wrote:
    Harald van D k wrote:
    Keith Thompson wrote:
    Can anyone think of a case where the evaluation of the
    argument of sizeof (when it's a VLA) has a visible
    effect on the behavior of the program?
    >
    #include <stdio.h>
    int f(void) {
    puts("f is called!");
    return 0;
    }
    int main(void) {
    enum { constant_size = 1 };
    const int nonconstant_siz e = 1;
    >
    char nonvla[constant_size];
    char vla[nonconstant_siz e];
    >
    printf("sizeof (&nonvla)[f()] = %zu\n",
    sizeof (&nonvla)[f()]);
    printf("sizeof (&vla)[f()] = %zu\n",
    sizeof (&vla)[f()]);
    >
    return 0;
    }
    >
    This should print:
    >
    sizeof (&nonvla)[f()] = 1
    f is called!
    sizeof (&vla)[f()] = 1
    Should it?
    >
    This is the behaviour mandated by the standard and
    implemented by the compiler I'm using. If you believe
    the standard would have been better if sizeof on VLAs
    were specified differently, then I don't necessarily
    disagree, but if you believe the standard requires (or
    allows) any different output, then could you please
    explain why?
    6.5.3.4p2: "... If the type of the operand is a variable
    length array type, the operand is evaluated; otherwise,
    the operand is not evaluated and the result is an
    integer constant."

    Since you don't apply sizeof to an operand of variable
    length array type, I don't see how or why f should be
    called.

    I believe the evaluation clause in 6.5.3.4p2 is more for
    cases like...

    sizeof (int [nonconstexpr])

    Since pointer arithmetic is required to work on VLAs,
    such pointers (including those created by using the name
    of a VLA) necessarily carry information about the VLAs
    size, either directly or indirectly.

    Consider...

    #include <stdio.h>
    #include <stdlib.h>

    int foo()
    {
    return 1 + rand() % 10;
    }

    size_t bah(char (*vlap)[*])
    {
    return sizeof *vlap;
    }

    int main()
    {
    int vla[foo()];
    printf("%zu\n", bah(&vla));
    return 0;
    }

    The function foo() is only called once. A compiler that
    calls foo() twice would be broken.

    --
    Peter

    Comment

    • Richard Tobin

      #47
      Re: tryick use of sizeof again[Explaination Wanted]

      In article <lnd4wjzoxk.fsf @nuthaus.mib.or g>,
      Keith Thompson <kst-u@mib.orgwrote:
      >Can anyone think of a case where the evaluation of the argument of
      >sizeof (when it's a VLA) has a visible effect on the behavior of the
      >program?
      Gcc with flags "-std=c99 -pedantic -Wall' thinks the following program
      is legal:

      #include <stdio.h>

      int main(void)
      {
      int n = 42;
      printf("%d\n", (int)sizeof(cha r [n++]));
      printf("%d\n", (int)sizeof(cha r [n++]));
      return 0;
      }

      and it prints:

      42
      43

      -- Richard
      --
      "Considerat ion shall be given to the need for as many as 32 characters
      in some alphabets" - X3.4, 1963.

      Comment

      • Keith Thompson

        #48
        Re: tryick use of sizeof again[Explaination Wanted]

        Harald van D©¦k <truedfx@gmail. comwrites:
        On Sat, 15 Sep 2007 14:35:46 -0700, Keith Thompson wrote:
        >"Harald van D)&k" <truedfx@gmail. comwrites:
        >>On Sat, 15 Sep 2007 12:52:39 -0700, Keith Thompson wrote:
        >[...]
        >>>Can anyone think of a case where the evaluation of the argument of
        >>>sizeof (when it's a VLA) has a visible effect on the behavior of the
        >>>program?
        >>>
        >>#include <stdio.h>
        >>int f(void) {
        >> puts("f is called!");
        >> return 0;
        >>}
        >>int main(void) {
        >> enum { constant_size = 1 };
        >> const int nonconstant_siz e = 1;
        >>>
        >> char nonvla[constant_size];
        >> char vla[nonconstant_siz e];
        >>>
        >> printf("sizeof (&nonvla)[f()] = %zu\n", sizeof (&nonvla)[f()]);
        >> printf("sizeof (&vla)[f()] = %zu\n", sizeof (&vla)[f()]);
        >>>
        >> return 0;
        >>}
        >>>
        >The type of the operand in both of your sizeof expressions is char, not
        >a VLA type.
        >
        I think you've overlooked the &. The operands to both sizeof operators
        have array types.
        You're right (and gcc gets this wrong).

        --
        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."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        • Flash Gordon

          #49
          Re: tryick use of sizeof again[Explaination Wanted]

          Harald van Dijk wrote, On 16/09/07 00:52:
          On Sat, 15 Sep 2007 23:13:33 +0100, Flash Gordon wrote:
          >Harald van Dijk wrote, On 15/09/07 22:13:
          >>On Sat, 15 Sep 2007 21:05:53 +0100, Flash Gordon wrote:
          >>>Keith Thompson wrote, On 15/09/07 20:52:
          >>>>
          >>><snip>
          >>>>
          >>>>Can anyone think of a case where the evaluation of the argument of
          >>>>sizeof (when it's a VLA) has a visible effect on the behavior of the
          >>>>program?
          >>>size_t size=5
          >>>int array[++size];
          >>No, sizeof(array) will always return 6 in this case. The array size is
          >>evaluated when the declaration is reached, but not re-evaluated
          >>afterwards.
          >I meant to say
          >sizeof array[++size];
          >Obviously I my brain was disconnected at the time.
          >
          array[++size] is an expression of type int, so ++size will not be
          evaluated.
          I've definitely been working too many hours this week. Normal service
          will be restored once I've taken my brain out and washed all the rubbish
          out of it.
          --
          Flash Gordon

          Comment

          • Charlie Gordon

            #50
            Re: tryick use of sizeof again[Explaination Wanted]

            "Richard Tobin" <richard@cogsci .ed.ac.uka écrit dans le message de news:
            fchthe$23po$1@p c-news.cogsci.ed. ac.uk...
            In article <lnd4wjzoxk.fsf @nuthaus.mib.or g>,
            Keith Thompson <kst-u@mib.orgwrote:
            >
            >>Can anyone think of a case where the evaluation of the argument of
            >>sizeof (when it's a VLA) has a visible effect on the behavior of the
            >>program?
            >
            Gcc with flags "-std=c99 -pedantic -Wall' thinks the following program
            is legal:
            >
            #include <stdio.h>
            >
            int main(void)
            {
            int n = 42;
            printf("%d\n", (int)sizeof(cha r [n++]));
            printf("%d\n", (int)sizeof(cha r [n++]));
            return 0;
            }
            >
            and it prints:
            >
            42
            43
            While it seems logic to do so, the language of the standard is not clear:

            6.5.3.4p2: "... If the type of the operand is a variable
            length array type, the operand is evaluated; otherwise,
            the operand is not evaluated and the result is an
            integer constant."

            What if the type of char[n++] ?
            How can we evaluate char[n++] ?

            char [n++] *is* a type, it does not *have* a type.

            I don't think the language of the standard correctly specifies what should
            be the behaviour of such code.

            A more compelling example would be

            #include <stdio.h>
            #include <stdlib.h>

            int main(int argc, char** argv)
            {
            if (argc 1) {
            int n = strtol(argv[1], NULL, 0);
            int matrix[n][n];
            int i = 1;

            printf("i=%d, n=%d\n", i, n);
            printf ("sizeof(matrix ) = %zd\n", sizeof(matrix)) ;
            printf ("sizeof(mat rix[i++]) = %zd\n", sizeof(matrix[i++]));
            printf("i=%d\n" , i);

            printf ("sizeof(cha r[i++]) = %zd\n", sizeof(char[i++]));
            printf("i=%d\n" , i);
            }
            return 0;
            }

            with gcc upto version 4.1.3, I get what I interpret as non conforming
            behaviour:

            $ ./vla 2
            i=1, n=2
            sizeof(matrix) = 16
            sizeof(matrix[i++]) = 8
            i=1
            sizeof(char[i++]) = 1
            i=2

            I think we have a defect here: the argument to sizeof should be evaluated is
            it *is* a variable length array type, not if it is an expression whois type
            can be determined from static analysis.

            --
            Chqrlie.


            Comment

            • Richard Bos

              #51
              Re: tryick use of sizeof again[Explaination Wanted]

              CBFalconer <cbfalconer@yah oo.comwrote:
              "christian. bau" wrote:
              "Charlie Gordon" <n...@chqrlie.o rgwrote:
              Why be unnecessarily obtuse when the issue is not C++ specific.
              How do you know it is not C++ specific? For example, after

              char c - '0';
              c += sizeof ('0');

              what is the value stored in c? There are subtle differences
              between the sizeof operator in C and C++, and someone knowing
              absolutely everything about C but nothing about C++ would most
              likely not know the right answer to the problem I wrote.
              >
              Why did you snip away the expression that triggered the query?
              Because it's immaterial to his point, which is that _all_ such
              expressions _may_ be different in C++, and only comp.lang.c++ can tell
              you for certain.
              I.e:
              >
              > int v = sizeof ++ch;
              >
              which has the same answer in C and C++ (I think),
              Precisely. _You think_. comp.lang.c++ would _know_.

              Richard

              Comment

              • Keith Thompson

                #52
                Re: tryick use of sizeof again[Explaination Wanted]

                rlb@hoekstra-uitgeverij.nl (Richard Bos) writes:
                CBFalconer <cbfalconer@yah oo.comwrote:
                [...]
                >I.e:
                >>
                >> int v = sizeof ++ch;
                >>
                >which has the same answer in C and C++ (I think),
                >
                Precisely. _You think_. comp.lang.c++ would _know_.
                You're assuming that the folks in comp.lang.c++ know enough about C to
                be sure about such things. (That may be an accurate assumption.)

                --
                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."
                -- Antony Jay and Jonathan Lynn, "Yes Minister"

                Comment

                • CBFalconer

                  #53
                  Re: tryick use of sizeof again[Explaination Wanted]

                  Richard Bos wrote:
                  CBFalconer <cbfalconer@yah oo.comwrote:
                  >
                  .... snip ...
                  >>
                  >> int v = sizeof ++ch;
                  >>
                  >which has the same answer in C and C++ (I think),
                  >
                  Precisely. _You think_. comp.lang.c++ would _know_.
                  No, they wouldn't. In general their knowledge of C is probably
                  similar to my knowledge of C++ - i.e. fair, but not comprehensive.

                  --
                  Chuck F (cbfalconer at maineline dot net)
                  Available for consulting/temporary embedded and systems.
                  <http://cbfalconer.home .att.net>


                  --
                  Posted via a free Usenet account from http://www.teranews.com

                  Comment

                  • Flash Gordon

                    #54
                    Re: tryick use of sizeof again[Explaination Wanted]

                    CBFalconer wrote, On 17/09/07 08:29:
                    Richard Bos wrote:
                    >CBFalconer <cbfalconer@yah oo.comwrote:
                    >>
                    ... snip ...
                    >>>>> int v = sizeof ++ch;
                    >>which has the same answer in C and C++ (I think),
                    >Precisely. _You think_. comp.lang.c++ would _know_.
                    >
                    No, they wouldn't. In general their knowledge of C is probably
                    similar to my knowledge of C++ - i.e. fair, but not comprehensive.
                    I think the point is they would _know_ the behaviour in C++. After all,
                    the original code _was_ C++ because it used C++ specific features on
                    another line.
                    --
                    Flash Gordon

                    Comment

                    • Kelsey Bjarnason

                      #55
                      Re: tryick use of sizeof again[Explaination Wanted]

                      [snips]

                      On Sat, 15 Sep 2007 00:31:53 +0200, Charlie Gordon wrote:
                      Yeah right sizeof('a') == 1 in C++ and sizeof('a') == sizeof(int) in C (how
                      stupid and counter-intuitive!)
                      There were legitimate reasons for this, however.
                      someone knowing absolutely everything about C but nothing about C++ would
                      fit my definition of obtuse perfectly.
                      Actually, that'd fit my definition of "C programmer who hasn't learned C++."

                      Comment

                      • CBFalconer

                        #56
                        Re: tryick use of sizeof again[Explaination Wanted]

                        Kelsey Bjarnason wrote:
                        On Sat, 15 Sep 2007 00:31:53 +0200, Charlie Gordon wrote:
                        >
                        [snips]
                        >
                        >Yeah right sizeof('a') == 1 in C++ and sizeof('a') == sizeof(int)
                        >in C (how stupid and counter-intuitive!)
                        >
                        There were legitimate reasons for this, however.
                        >
                        >someone knowing absolutely everything about C but nothing about
                        >C++ would fit my definition of obtuse perfectly.
                        >
                        Actually, that'd fit my definition of "C programmer who hasn't
                        learned C++."
                        LOL. Accurate.

                        --
                        Chuck F (cbfalconer at maineline dot net)
                        Available for consulting/temporary embedded and systems.
                        <http://cbfalconer.home .att.net>



                        --
                        Posted via a free Usenet account from http://www.teranews.com

                        Comment

                        • CBFalconer

                          #57
                          Re: tryick use of sizeof again[Explaination Wanted]

                          Flash Gordon wrote:
                          CBFalconer wrote, On 17/09/07 08:29:
                          >Richard Bos wrote:
                          >>CBFalconer <cbfalconer@yah oo.comwrote:
                          >>>
                          >... snip ...
                          >>
                          >>>>>> int v = sizeof ++ch;
                          >>>>
                          >>>which has the same answer in C and C++ (I think),
                          >>>
                          >>Precisely. _You think_. comp.lang.c++ would _know_.
                          >>
                          >No, they wouldn't. In general their knowledge of C is probably
                          >similar to my knowledge of C++ - i.e. fair, but not comprehensive.
                          >
                          I think the point is they would _know_ the behaviour in C++. After
                          all, the original code _was_ C++ because it used C++ specific
                          features on another line.
                          Which (I think) is just what I said. An answer requires knowledge
                          of both systems.

                          --
                          Chuck F (cbfalconer at maineline dot net)
                          Available for consulting/temporary embedded and systems.
                          <http://cbfalconer.home .att.net>



                          --
                          Posted via a free Usenet account from http://www.teranews.com

                          Comment

                          • Kelsey Bjarnason

                            #58
                            Re: tryick use of sizeof again[Explaination Wanted]

                            On Mon, 17 Sep 2007 17:45:22 -0400, CBFalconer wrote:
                            Kelsey Bjarnason wrote:
                            >On Sat, 15 Sep 2007 00:31:53 +0200, Charlie Gordon wrote:
                            >>
                            >[snips]
                            >>
                            >>Yeah right sizeof('a') == 1 in C++ and sizeof('a') == sizeof(int)
                            >>in C (how stupid and counter-intuitive!)
                            >>
                            >There were legitimate reasons for this, however.
                            >>
                            >>someone knowing absolutely everything about C but nothing about
                            >>C++ would fit my definition of obtuse perfectly.
                            >>
                            >Actually, that'd fit my definition of "C programmer who hasn't
                            >learned C++."
                            >
                            LOL. Accurate.
                            He do seem to think that learning C magically imparts a deep knowledge of
                            C++, don't he?

                            Weird stuff.

                            Comment

                            • Richard Bos

                              #59
                              Re: tryick use of sizeof again[Explaination Wanted]

                              CBFalconer <cbfalconer@yah oo.comwrote:
                              Flash Gordon wrote:
                              CBFalconer wrote, On 17/09/07 08:29:
                              Richard Bos wrote:
                              >CBFalconer <cbfalconer@yah oo.comwrote:
                              >>
                              ... snip ...
                              >
                              >>>>> int v = sizeof ++ch;
                              >>>
                              >>which has the same answer in C and C++ (I think),
                              >>
                              >Precisely. _You think_. comp.lang.c++ would _know_.
                              >
                              No, they wouldn't. In general their knowledge of C is probably
                              similar to my knowledge of C++ - i.e. fair, but not comprehensive.
                              I think the point is they would _know_ the behaviour in C++. After
                              all, the original code _was_ C++ because it used C++ specific
                              features on another line.
                              >
                              Which (I think) is just what I said. An answer requires knowledge
                              of both systems.
                              No, it requires knowledge of C++. It was a question about the behaviour
                              of the posted code, which was C++ code; it was not a question about the
                              potential difference in behaviour between that code and similar C code.

                              Richard

                              Comment

                              • Flash Gordon

                                #60
                                Re: tryick use of sizeof again[Explaination Wanted]

                                Richard Bos wrote, On 18/09/07 07:13:
                                CBFalconer <cbfalconer@yah oo.comwrote:
                                >
                                >Flash Gordon wrote:
                                >>CBFalconer wrote, On 17/09/07 08:29:
                                >>>Richard Bos wrote:
                                >>>>CBFalcone r <cbfalconer@yah oo.comwrote:
                                >>>>>
                                >>>... snip ...
                                >>>>
                                >>>>>>>> int v = sizeof ++ch;
                                >>>>>which has the same answer in C and C++ (I think),
                                >>>>Precisely . _You think_. comp.lang.c++ would _know_.
                                >>>No, they wouldn't. In general their knowledge of C is probably
                                >>>similar to my knowledge of C++ - i.e. fair, but not comprehensive.
                                >>I think the point is they would _know_ the behaviour in C++. After
                                >>all, the original code _was_ C++ because it used C++ specific
                                >>features on another line.
                                >Which (I think) is just what I said. An answer requires knowledge
                                >of both systems.
                                >
                                No, it requires knowledge of C++. It was a question about the behaviour
                                of the posted code, which was C++ code; it was not a question about the
                                potential difference in behaviour between that code and similar C code.
                                Yes, I meant what Richard has just posted.
                                --
                                Flash Gordon

                                Comment

                                Working...