sizeof(x)

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

    sizeof(x)

    In C99 it is mentioned:

    "The sizeof operator yields the size (in bytes) of its operand, which
    may be an expression or the parenthesized name of a type.".


    If I am not wrong, this implies that

    int x;

    size_t y= sizeof(x);


    is not valid.


    and only the following is valid:


    int x;

    size_t y= sizeof x;


    However I am puzzled, and thought the first was also valid in
    C90/C95(/C++03).

  • Harald van =?UTF-8?b?RMSzaw==?=

    #2
    Re: sizeof(x)

    On Sun, 30 Mar 2008 22:50:00 +0300, Ioannis Vranos wrote:
    In C99 it is mentioned:
    >
    "The sizeof operator yields the size (in bytes) of its operand, which
    may be an expression or the parenthesized name of a type.".
    >
    If I am not wrong, this implies that
    >
    int x;
    >
    size_t y= sizeof(x);
    >
    >
    is not valid.
    You are; (x) is a perfectly valid expression, so there's no problem
    taking the size of (x).

    Comment

    • Ioannis Vranos

      #3
      Re: sizeof(x)

      Harald van Dijk wrote:
      On Sun, 30 Mar 2008 22:50:00 +0300, Ioannis Vranos wrote:
      >In C99 it is mentioned:
      >>
      >"The sizeof operator yields the size (in bytes) of its operand, which
      >may be an expression or the parenthesized name of a type.".
      >>
      >If I am not wrong, this implies that
      >>
      >int x;
      >>
      >size_t y= sizeof(x);
      >>
      >>
      >is not valid.
      >
      You are; (x) is a perfectly valid expression, so there's no problem
      taking the size of (x).

      I first saw that only sizeof x is valid at the pdf hosted at
      http://cprog.tomsweb.net.

      Then I checked the C99 standard and it mentions what is shown above.
      Clearly C99 doesn't mention parenthesized expression.

      Comment

      • Ioannis Vranos

        #4
        Re: sizeof(x)

        Ioannis Vranos wrote:
        >
        I first saw that only sizeof x is valid at the pdf hosted at
        http://cprog.tomsweb.net.

        More specifically the above writes:

        "sizeof Returns size of operand in bytes; two forms:
        1) sizeof(type)
        2) sizeof expression"


        Then I checked the C99 standard and it mentions what is shown above.
        Clearly C99 doesn't mention parenthesized expression.

        Comment

        • Ian Collins

          #5
          Re: sizeof(x)

          Ioannis Vranos wrote:
          Ioannis Vranos wrote:
          >I first saw that only sizeof x is valid at the pdf hosted at
          >http://cprog.tomsweb.net.
          >
          >
          More specifically the above writes:
          >
          "sizeof Returns size of operand in bytes; two forms:
          1) sizeof(type)
          2) sizeof expression"
          >
          >
          Did you read what Harald said: "(x) is a perfectly valid expression"?

          --
          Ian Collins.

          Comment

          • Ioannis Vranos

            #6
            Re: sizeof(x)

            Ian Collins wrote:
            Ioannis Vranos wrote:
            >Ioannis Vranos wrote:
            >>I first saw that only sizeof x is valid at the pdf hosted at
            >>http://cprog.tomsweb.net.
            >>
            >More specifically the above writes:
            >>
            >"sizeof Returns size of operand in bytes; two forms:
            > 1) sizeof(type)
            > 2) sizeof expression"
            >>
            >>
            Did you read what Harald said: "(x) is a perfectly valid expression"?
            .... right. However sizeofx doesn't compile and if (x) was considered an
            expression it should be sizeof (x), and sizeof(x) shouldn't compile.

            Comment

            • Harald van =?UTF-8?b?RMSzaw==?=

              #7
              Re: sizeof(x)

              On Sun, 30 Mar 2008 23:07:05 +0300, Ioannis Vranos wrote:
              Harald van Dijk wrote:
              >On Sun, 30 Mar 2008 22:50:00 +0300, Ioannis Vranos wrote:
              >>In C99 it is mentioned:
              >>>
              >>"The sizeof operator yields the size (in bytes) of its operand, which
              >>may be an expression or the parenthesized name of a type.".
              >>>
              >>If I am not wrong, this implies that
              >>>
              >>int x;
              >>>
              >>size_t y= sizeof(x);
              >>>
              >>>
              >>is not valid.
              >>
              >You are; (x) is a perfectly valid expression, so there's no problem
              >taking the size of (x).
              >
              I first saw that only sizeof x is valid at the pdf hosted at
              http://cprog.tomsweb.net.
              That doesn't say sizeof(x) is invalid any more than the standard does.
              Then I checked the C99 standard and it mentions what is shown above.
              Clearly C99 doesn't mention parenthesized expression.
              Yes, it does. Look at the grammar.

              unary-expression:
              sizeof unary-expression

              unary-expression:
              postfix-expression

              postfix-expression:
              primary-expression

              primary-expression:
              ( expression )

              A parenthesised expression is a primary-expression, which is a postfix-
              expression, which is a unary-expression, which is a valid operand of
              sizeof.

              The standard doesn't explicitly mention that parenthesised expressions
              are valid operands of +, -, *, /, ^, &, or pretty much any other
              operator. The grammar makes that clear already.

              Comment

              • Harald van =?UTF-8?b?RMSzaw==?=

                #8
                Re: sizeof(x)

                On Sun, 30 Mar 2008 23:11:55 +0300, Ioannis Vranos wrote:
                Ian Collins wrote:
                >Ioannis Vranos wrote:
                >>Ioannis Vranos wrote:
                >>>I first saw that only sizeof x is valid at the pdf hosted at
                >>>http://cprog.tomsweb.net.
                >>>
                >>More specifically the above writes:
                >>>
                >>"sizeof Returns size of operand in bytes; two forms:
                >> 1) sizeof(type)
                >> 2) sizeof expression"
                >>>
                >>>
                >Did you read what Harald said: "(x) is a perfectly valid expression"?
                >
                ... right. However sizeofx doesn't compile and if (x) was considered an
                expression it should be sizeof (x), and sizeof(x) shouldn't compile.
                sizeofx doesn't compile because there's no sizeof operator. There's a
                sizeofx identifier. sizeof(x) does and should compile because sizeof( is
                not a valid identifier. This is the same reason why a+++++b is not valid,
                but a+++--b is: the first is split into {a}{++}{++}{+}{ b}, and the second
                is split into {a}{++}{+}{--}{b}.

                Comment

                • Eric Sosman

                  #9
                  Re: sizeof(x)

                  Ioannis Vranos wrote:
                  Ian Collins wrote:
                  >Ioannis Vranos wrote:
                  >>Ioannis Vranos wrote:
                  >>>I first saw that only sizeof x is valid at the pdf hosted at
                  >>>http://cprog.tomsweb.net.
                  >>More specifically the above writes:
                  >>>
                  >>"sizeof Returns size of operand in bytes; two forms:
                  >> 1) sizeof(type)
                  >> 2) sizeof expression"
                  >>>
                  >>>
                  >Did you read what Harald said: "(x) is a perfectly valid expression"?
                  >
                  ... right. However sizeofx doesn't compile and if (x) was considered an
                  expression it should be sizeof (x), and sizeof(x) shouldn't compile.
                  Try sizeof((((((((( ((((((((((((x)) ))))))))))))))) ))))
                  and perhaps the answer will come to you.

                  --
                  Eric Sosman
                  esosman@ieee-dot-org.invalid

                  Comment

                  • Ioannis Vranos

                    #10
                    Re: sizeof(x)

                    Eric Sosman wrote:
                    Ioannis Vranos wrote:
                    >Ian Collins wrote:
                    >>Ioannis Vranos wrote:
                    >>>Ioannis Vranos wrote:
                    >>>>I first saw that only sizeof x is valid at the pdf hosted at
                    >>>>http://cprog.tomsweb.net.
                    >>>More specifically the above writes:
                    >>>>
                    >>>"sizeof Returns size of operand in bytes; two forms:
                    >>> 1) sizeof(type)
                    >>> 2) sizeof expression"
                    >>>>
                    >>>>
                    >>Did you read what Harald said: "(x) is a perfectly valid expression"?
                    >>
                    >... right. However sizeofx doesn't compile and if (x) was considered an
                    >expression it should be sizeof (x), and sizeof(x) shouldn't compile.
                    >
                    Try sizeof((((((((( ((((((((((((x)) ))))))))))))))) ))))
                    and perhaps the answer will come to you.

                    Yes, it had already arrived. :-) Thanks anyway.

                    Comment

                    • Keith Thompson

                      #11
                      Re: sizeof(x)

                      Ioannis Vranos <ivranos@nospam .no.spamfreemai l.grwrites:
                      Ian Collins wrote:
                      >Ioannis Vranos wrote:
                      >>Ioannis Vranos wrote:
                      >>>I first saw that only sizeof x is valid at the pdf hosted at
                      >>>http://cprog.tomsweb.net.
                      >>>
                      >>More specifically the above writes:
                      >>>
                      >>"sizeof Returns size of operand in bytes; two forms:
                      >> 1) sizeof(type)
                      >> 2) sizeof expression"
                      >>>
                      >>>
                      >Did you read what Harald said: "(x) is a perfectly valid expression"?
                      >
                      ... right. However sizeofx doesn't compile and if (x) was considered an
                      expression it should be sizeof (x), and sizeof(x) shouldn't compile.
                      C code is split into tokens before those tokens are parsed. (The
                      process actually involves "preprocess or tokens", which are later
                      converted to "tokens", but that doesn't matter in this case.)

                      ``sizeof'' is a keyword; like all keywords, it has the form of an
                      identifier. ``('' and ``)'' are punctuators. Two adjacent
                      identifiers, keywords, or numeric constants must be separated by
                      whitespace (a comment counts as whitespace). A keyword and a
                      punctuator don't need any whitespace to separate them.

                      ``sizeofx'' is just a single identifier that has nothing to do with
                      the ``sizeof'' keyword. It compiles just fine if you happen to have
                      declared it:

                      int sizeofx = 42;
                      sizeofx;

                      ``sizeof x'' is two tokens, ``sizeof'' and ``x''. If ``x'' is an
                      expression (actually a unary-expression; see the grammar), then that's
                      a legal expression.

                      ``sizeof(x)'' is four tokens, ``sizeof'', ``('', ``x'', and ``)''. If
                      ``x'' is a type-name, then that's a valid expression. If ``x'' is an
                      expression, then ``(x)'' is also a valid expression, and the whole
                      thing is also a valid expression.

                      --
                      Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                      Nokia
                      "We must do something. This is something. Therefore, we must do this."
                      -- Antony Jay and Jonathan Lynn, "Yes Minister"

                      Comment

                      • Kaz Kylheku

                        #12
                        Re: sizeof(x)

                        On Mar 30, 1:11 pm, Ioannis Vranos <ivra...@nospam .no.spamfreemai l.gr>
                        wrote:
                        expression it should be sizeof (x), and sizeof(x) shouldn't compile.
                        Good grief!

                        Do you realize that children that were in junior highschool back when
                        you first started being a goof here now have CS degrees and software
                        jobs?

                        You need to disable your anti-learning filter, or at least reduce the
                        coefficients in its confusion matrix a little bit.

                        Comment

                        • Richard Tobin

                          #13
                          Re: sizeof(x)

                          In article <fsort9$26if$1@ ulysses.noc.ntu a.gr>,
                          Ioannis Vranos <ivranos@nospam .no.spamfreemai l.grwrote:
                          >Then I checked the C99 standard and it mentions what is shown above.
                          >Clearly C99 doesn't mention parenthesized expression.
                          A parenthesized expression is an expression, (that's a fact about C
                          grammar, not the English language). sizeof(x) is no more a problem
                          than a[(x)].

                          -- Richard
                          --
                          :wq

                          Comment

                          Working...