How to implement sizeof operator in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maheshgupta024
    New Member
    • Jun 2008
    • 14

    How to implement sizeof operator in C

    Can anyone tell me how to implement sizeof in C language
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by maheshgupta024
    Can anyone tell me how to implement sizeof in C language
    There already is an implemenation of it. Do you want to use it or to create your own?

    Comment

    • maheshgupta024
      New Member
      • Jun 2008
      • 14

      #3
      yes.. I want to write a function which provide the same functionality.. .

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Since you can not write a function that takes a type as an argument you will not be able to do what you wish.

        You can get the size of a type programatically but the code would have to be specific to the type.

        The sizeof operator exists because there is no real other way to get the functionality it provides and it is something that is useful.

        Comment

        • oler1s
          Recognized Expert Contributor
          • Aug 2007
          • 671

          #5
          sizeof is a compile time operator. Asking how to implement sizeof only makes sense if you are writing your own compiler.

          Comment

          • AmberJain
            Recognized Expert Contributor
            • Jan 2008
            • 922

            #6
            The link below may help you out more------>

            LINK

            HOPE THIS HELPS.......... .....

            ============
            AmbrNewlearner
            ============

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by ambrnewlearner
              The link below may help you out more------>

              LINK
              All it does is show you that a sizeof operator is a compile time operator and
              necessarily highly platform dependent. That is/was an interesting thread: I
              didn't know that some of my compadres had posted in this forum ...
              I think it's just a temporary tap from a usenet feed though.

              kind regards,

              Jos

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by JosAH
                ..That is/was an interesting thread: I
                didn't know that some of my compadres had posted in this forum ...
                A macro that gets the address, adds 1 and subtracts that address would be very close.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by r035198x
                  A macro that gets the address, adds 1 and subtracts that address would be very close.
                  Only partly; there are two forms of the sizeof operator:

                  1: sizeof ( type-name )
                  2: sizeof unary-expression

                  your suggestion only works for the first alternative but what would the size be
                  of, say '~(a+b++)'? It can't be done without being part of the compiler and knowing
                  about the platform this darn thing is executed on.

                  kind regards,

                  Jos

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by JosAH
                    Only partly; there are two forms of the sizeof operator:

                    1: sizeof ( type-name )
                    2: sizeof unary-expression

                    your suggestion only works for the first alternative but what would the size be
                    of, say '~(a+b++)'? It can't be done without being part of the compiler and knowing
                    about the platform this darn thing is executed on.

                    kind regards,

                    Jos
                    Hence the "close".
                    BTW Have you implemented sizeof in .... <starting to get out of topic here so I'll stop>.

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by r035198x
                      Hence the "close".
                      BTW Have you implemented sizeof in .... <starting to get out of topic here so I'll stop>.
                      Try 'abs'

                      kind regards,

                      Jos

                      ps. sorry for being off-topic.

                      Comment

                      Working...