Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. sizeof related in c/c++

sizeof related in c/c++

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hawk23reddy
    wrote on last edited by
    #1

    hi all, i am sorry i dont know weather i could ask this question ornot coz, these r the thinks always run in my mind. since we call sizeof as operator but y does we use it as sizeof(type) as a function... plese if any buddy finds it silly forgive me i even tried with google i didnt find it out..

    E S D _ 4 Replies Last reply
    0
    • H hawk23reddy

      hi all, i am sorry i dont know weather i could ask this question ornot coz, these r the thinks always run in my mind. since we call sizeof as operator but y does we use it as sizeof(type) as a function... plese if any buddy finds it silly forgive me i even tried with google i didnt find it out..

      E Offline
      E Offline
      Emilio Garavaglia
      wrote on last edited by
      #2

      well ... technically speaking ... every operator IS a function. So what's the difference ?

      2 bugs found. > recompile ... 65534 bugs found. :doh:

      S 1 Reply Last reply
      0
      • H hawk23reddy

        hi all, i am sorry i dont know weather i could ask this question ornot coz, these r the thinks always run in my mind. since we call sizeof as operator but y does we use it as sizeof(type) as a function... plese if any buddy finds it silly forgive me i even tried with google i didnt find it out..

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        hawk23reddy wrote:

        y does we use it as sizeof(type) as a function...

        Because that's what the C and C++ Standard's define. To quote the C++ Standard:

        The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is not evaluated, or a parenthesized typeid.

        And you're better off using parentheses with expressions, as sizeof has higher precedence than most arithmetic operators. Consider this code:

        #include <stdio.h>

        int main(int argc, char** argv)
        {
        printf("%d\n", (int)(sizeof 3+4)); // Prints 8
        printf("%d\n", (int)(sizeof (3+4))); // Prints 4
        }

        Because sizeof has higher precedence than +, sizeof 3+4 == (sizeof 3) + 4.

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        1 Reply Last reply
        0
        • H hawk23reddy

          hi all, i am sorry i dont know weather i could ask this question ornot coz, these r the thinks always run in my mind. since we call sizeof as operator but y does we use it as sizeof(type) as a function... plese if any buddy finds it silly forgive me i even tried with google i didnt find it out..

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          The parenthesis are required if you are requesting the size of a type (e.g., int). Otherwise, the parenthesis are optional.

          "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          H 1 Reply Last reply
          0
          • E Emilio Garavaglia

            well ... technically speaking ... every operator IS a function. So what's the difference ?

            2 bugs found. > recompile ... 65534 bugs found. :doh:

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            emilio_grv wrote:

            well ... technically speaking ... every operator IS a function

            In a language like Haskell, yes. In C or C++, not so much - for example, can you show me how to get the address of +, so I can use it as a functor in an STL algorithm? To elaborate on the Haskell reference - any operator can be used where a function could be used by surrounding it in parentheses. Similarly, any function can be used as an operator by enclosing it in backticks. For example:

            -- Define an addition function
            add x y = x + y

            -- Define two functions that add integer lists. The first uses the + operator
            -- as a function, the second uses add. They are equivalent.
            addLists1 :: [Int] -> [Int] -> [Int]
            addLists1 l1 l2 = zipWith (+) l1 l2

            addLists2 :: [Int] -> [Int] -> [Int]
            addLists2 l1 l2 = zipWith add l1 l2

            -- The 4 lines of main are equivalent - they all add 3 and 4 with
            -- different combination of operator/function and operator/function syntax.
            main = do
            print (add 3 4)
            print ((+) 3 4)
            print (3 `add` 4)
            print (3 + 4)

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            E 1 Reply Last reply
            0
            • D David Crow

              The parenthesis are required if you are requesting the size of a type (e.g., int). Otherwise, the parenthesis are optional.

              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              H Offline
              H Offline
              hawk23reddy
              wrote on last edited by
              #6

              hi thankx for answer but y for int (or types) the parenthesis is required is there any conceret reason

              S 1 Reply Last reply
              0
              • H hawk23reddy

                hi thankx for answer but y for int (or types) the parenthesis is required is there any conceret reason

                S Offline
                S Offline
                Stuart Dootson
                wrote on last edited by
                #7

                As I said in this reply[^] - because the standard says so... But it's probably to simplify parsing - lord knows C++ (and to a degree, C) parsers need all the help they can get.

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                1 Reply Last reply
                0
                • S Stuart Dootson

                  emilio_grv wrote:

                  well ... technically speaking ... every operator IS a function

                  In a language like Haskell, yes. In C or C++, not so much - for example, can you show me how to get the address of +, so I can use it as a functor in an STL algorithm? To elaborate on the Haskell reference - any operator can be used where a function could be used by surrounding it in parentheses. Similarly, any function can be used as an operator by enclosing it in backticks. For example:

                  -- Define an addition function
                  add x y = x + y

                  -- Define two functions that add integer lists. The first uses the + operator
                  -- as a function, the second uses add. They are equivalent.
                  addLists1 :: [Int] -> [Int] -> [Int]
                  addLists1 l1 l2 = zipWith (+) l1 l2

                  addLists2 :: [Int] -> [Int] -> [Int]
                  addLists2 l1 l2 = zipWith add l1 l2

                  -- The 4 lines of main are equivalent - they all add 3 and 4 with
                  -- different combination of operator/function and operator/function syntax.
                  main = do
                  print (add 3 4)
                  print ((+) 3 4)
                  print (3 `add` 4)
                  print (3 + 4)

                  Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                  E Offline
                  E Offline
                  Emilio Garavaglia
                  wrote on last edited by
                  #8

                  Stuart Dootson wrote:

                  can you show me how to get the address of +, so I can use it as a functor in an STL algorithm?

                  Good point! And that's probably the difference that makes the standard deployers to use different therms. In theory it shold be &operator+, but that's ambiguous because of its overloading ... Thanks for pointing this out.

                  2 bugs found. > recompile ... 65534 bugs found. :doh:

                  S 1 Reply Last reply
                  0
                  • E Emilio Garavaglia

                    Stuart Dootson wrote:

                    can you show me how to get the address of +, so I can use it as a functor in an STL algorithm?

                    Good point! And that's probably the difference that makes the standard deployers to use different therms. In theory it shold be &operator+, but that's ambiguous because of its overloading ... Thanks for pointing this out.

                    2 bugs found. > recompile ... 65534 bugs found. :doh:

                    S Offline
                    S Offline
                    Stuart Dootson
                    wrote on last edited by
                    #9

                    Mmm - thinking about it, you *can* use something like that for class/struct types:

                    struct XX
                    {
                    };
                    XX operator+( XX const&, XX const& ) {}

                    struct YY
                    {
                    };
                    YY operator+( YY const&, YY const& ) {}

                    int main()
                    {
                    &static_cast<XX( & )( XX const&,XX const& )>(operator+);
                    &static_cast<YY( & )( YY const&,YY const& )>(operator+);
                    }

                    The static_cast effectively specifies which operator+ you want. But you can't use that for built-in types.

                    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                    1 Reply Last reply
                    0
                    • H hawk23reddy

                      hi all, i am sorry i dont know weather i could ask this question ornot coz, these r the thinks always run in my mind. since we call sizeof as operator but y does we use it as sizeof(type) as a function... plese if any buddy finds it silly forgive me i even tried with google i didnt find it out..

                      _ Offline
                      _ Offline
                      _Superman_
                      wrote on last edited by
                      #10

                      sizeof is not a function. It is an operator. Just because parenthesis is used does mean that it is a function. In that case if, switch, while, return etc. can be called functions. This is a excerpt from MSDN - The operand to sizeof can be one of the following: A type name. To use sizeof with a type name, the name must be enclosed in parentheses. An expression. When used with an expression, sizeof can be specified with or without the parentheses. The expression is not evaluated.

                      «_Superman_» I love work. It gives me something to do between weekends.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups