sizeof

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pnreddy1976@gmail.com

    sizeof

    Hi,
    How can we write a function, which functionality is similar to sizeof
    function
    any one send me source code
    Reddy

  • Richard Heathfield

    #2
    Re: sizeof

    pnreddy1976@gma il.com said:
    [color=blue]
    > Hi,
    > How can we write a function, which functionality is similar to sizeof
    > function[/color]

    We can't, because sizeof is not a function. (And there's a good reason why
    it's not a function.)

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999

    email: rjh at above domain (but drop the www, obviously)

    Comment

    • Madhav

      #3
      Re: sizeof

      Richard Heathfield wrote:[color=blue]
      > pnreddy1976@gma il.com said:
      >[color=green]
      > > Hi,
      > > How can we write a function, which functionality is similar to sizeof
      > > function[/color]
      >
      > We can't, because sizeof is not a function. (And there's a good reason why
      > it's not a function.[/color]

      Can you tell us that reason, Richard?

      Regards,
      Madhav.

      Comment

      • Richard Heathfield

        #4
        Re: sizeof

        Madhav said:
        [color=blue]
        > Richard Heathfield wrote:[color=green]
        >> pnreddy1976@gma il.com said:
        >>[color=darkred]
        >> > Hi,
        >> > How can we write a function, which functionality is similar to sizeof
        >> > function[/color]
        >>
        >> We can't, because sizeof is not a function. (And there's a good reason
        >> why it's not a function.[/color]
        >
        > Can you tell us that reason, Richard?[/color]

        Spend a little time trying to write such a function, and you'll discover it
        for yourself quickly enough. Hint: what type should its parameter be?

        --
        Richard Heathfield
        "Usenet is a strange place" - dmr 29/7/1999

        email: rjh at above domain (but drop the www, obviously)

        Comment

        • Xicheng Jia

          #5
          Re: sizeof

          pnreddy1976@gma il.com wrote:[color=blue]
          > Hi,
          > How can we write a function, which functionality is similar to sizeof
          > function
          > any one send me source code[/color]

          the problem is that the result of sizeof() is interpolated at compile
          time. Can any function do it that way! :-)

          Xicheng

          Comment

          • Malcolm

            #6
            Re: sizeof




            "Xicheng Jia" <xicheng@gmail. com> wrote in message[color=blue]
            > pnreddy1976@gma il.com wrote:[color=green]
            >> Hi,
            >> How can we write a function, which functionality is similar to sizeof
            >> function
            >> any one send me source code[/color]
            >
            > the problem is that the result of sizeof() is interpolated at compile
            > time. Can any function do it that way! :-)
            >[/color]
            A compiler is permitted to inline functions, and then to simplify code. A
            really good compiler might simplify constructs like acos(0) to produce half
            PI, but this is rare.
            --
            Buy my book 12 Common Atheist Arguments (refuted)
            $1.25 download or $7.20 paper, available www.lulu.com/bgy1mm


            Comment

            • Frederick Gotham

              #7
              Re: sizeof

              Xicheng Jia posted:
              [color=blue]
              > Can any function do it that way! :-)[/color]


              A macro function perhaps:


              #define SizeOf(Type) \
              ( \
              (const char*)128 - \
              \
              (const char*)( (const Type*)128 - 1 ) \
              )


              --

              Frederick Gotham

              Comment

              • lovecreatesbeauty

                #8
                Re: sizeof


                Richard Heathfield wrote:[color=blue]
                > Spend a little time trying to write such a function, and you'll discover it
                > for yourself quickly enough. Hint: what type should its parameter be?[/color]

                I think it is because we need C language to tell us the sizes of
                various types and objects by sizeof. It's the purpose of sizeof itself
                and why it is called the name operator. We have no alternative choice.

                lovecreatesbeau ty

                Comment

                • Marc Thrun

                  #9
                  Re: sizeof

                  Frederick Gotham schrieb:[color=blue]
                  > Xicheng Jia posted:
                  >[color=green]
                  >> Can any function do it that way! :-)[/color]
                  >
                  >
                  > A macro function perhaps:
                  >
                  >
                  > #define SizeOf(Type) \
                  > ( \
                  > (const char*)128 - \
                  > \
                  > (const char*)( (const Type*)128 - 1 ) \
                  > )
                  >
                  >[/color]

                  Undefined behaviour - your pointer most likely does not point to any
                  valid object. And even if it does on certain implementations , it makes
                  no sense in c.l.c as conversion from integer to pointer types is
                  implementation defined and thus not portable.

                  --
                  Marc Thrun

                  Comment

                  • Frederick Gotham

                    #10
                    Re: sizeof

                    Marc Thrun posted:
                    [color=blue]
                    > Frederick Gotham schrieb:[color=green]
                    >> Xicheng Jia posted:
                    >>[color=darkred]
                    >>> Can any function do it that way! :-)[/color]
                    >>
                    >>
                    >> A macro function perhaps:
                    >>
                    >>
                    >> #define SizeOf(Type) \
                    >> ( \
                    >> (const char*)128 - \
                    >> \
                    >> (const char*)( (const Type*)128 - 1 ) \
                    >> )
                    >>
                    >>[/color]
                    >
                    > Undefined behaviour - your pointer most likely does not point to any
                    > valid object. And even if it does on certain implementations , it makes
                    > no sense in c.l.c as conversion from integer to pointer types is
                    > implementation defined and thus not portable.[/color]


                    Yes I'm aware of that.

                    This thread isn't about anything serious; it's more of a little mind game
                    to see who can write a "sizeof".


                    --

                    Frederick Gotham

                    Comment

                    • Andrew Poelstra

                      #11
                      Re: sizeof

                      On 2006-06-18, Xicheng Jia <xicheng@gmail. com> wrote:[color=blue]
                      > pnreddy1976@gma il.com wrote:[color=green]
                      >> Hi,
                      >> How can we write a function, which functionality is similar to sizeof
                      >> function
                      >> any one send me source code[/color]
                      >
                      > the problem is that the result of sizeof() is interpolated at compile
                      > time. Can any function do it that way! :-)
                      >[/color]

                      Yes, any standard function could do it that way; the compiler has control
                      over all the libraries included with it. Not only that, but with C99 VLA's,
                      sizeof /cannot/ be computer at compile time in some cases.

                      --
                      Andrew Poelstra < http://www.wpsoftware.net/blog >
                      To email me, use "apoelstra" at the above address.
                      I know that area of town like the back of my head.

                      Comment

                      • ada

                        #12
                        Re: sizeof

                        Andrew Poelstra <apoelstra@loca lhost.localdoma in> writes:
                        [color=blue]
                        > On 2006-06-18, Xicheng Jia <xicheng@gmail. com> wrote:[color=green]
                        >> pnreddy1976@gma il.com wrote:[color=darkred]
                        >>> Hi,
                        >>> How can we write a function, which functionality is similar to sizeof
                        >>> function
                        >>> any one send me source code[/color]
                        >>
                        >> the problem is that the result of sizeof() is interpolated at compile
                        >> time. Can any function do it that way! :-)
                        >>[/color]
                        >
                        > Yes, any standard function could do it that way; the compiler has control
                        > over all the libraries included with it. Not only that, but with C99 VLA's,
                        > sizeof /cannot/ be computer at compile time in some cases.[/color]
                        Why?[color=blue]
                        >
                        > --
                        > Andrew Poelstra < http://www.wpsoftware.net/blog >
                        > To email me, use "apoelstra" at the above address.
                        > I know that area of town like the back of my head.[/color]

                        Comment

                        • Xicheng Jia

                          #13
                          Re: sizeof

                          Andrew Poelstra wrote:[color=blue]
                          > On 2006-06-18, Xicheng Jia <xicheng@gmail. com> wrote:[color=green]
                          > > pnreddy1976@gma il.com wrote:[color=darkred]
                          > >> Hi,
                          > >> How can we write a function, which functionality is similar to sizeof
                          > >> function
                          > >> any one send me source code[/color]
                          > >
                          > > the problem is that the result of sizeof() is interpolated at compile
                          > > time. Can any function do it that way! :-)
                          > >[/color]
                          >
                          > Yes, any standard function could do it that way; the compiler has control
                          > over all the libraries included with it. Not only that, but with C99 VLA's,
                          > sizeof /cannot/ be computer at compile time in some cases.
                          >[/color]

                          Sorry, I should have said that the operand of sizeof() is interpolated
                          at compile-time. so i++ in sizeof(i++) does not increment i and the
                          following code prints 'i = 1' instead of 'i = 2'..

                          #include<stdio. h>
                          int main()
                          {
                          int i = 1, sz;
                          sz = sizeof(i++);
                          printf("i = %d\n", i);
                          return 0;
                          }

                          In ISO C, Macro is a different thing from function. and I am not sure
                          if inline function can do things like sizeof() does though . any hints
                          about this, thanks..:)

                          Xicheng

                          Comment

                          • Keith Thompson

                            #14
                            Re: sizeof

                            pnreddy1976@gma il.com writes:[color=blue]
                            > How can we write a function, which functionality is similar to sizeof
                            > function[/color]

                            Why would you want to do that?
                            [color=blue]
                            > any one send me source code[/color]

                            Nope.

                            The only plausible reason to do something like this is as a homework
                            assignment (though it's a particularly bad one). If this is the case
                            here, we're certainly not going to do your homework for you.

                            If you have some other reason, tell us what it is. Tell us what
                            you're *really* trying to accomplish, and we might be able to help you
                            find a better solution. (Based on your question, a better solution is
                            obvious: use the sizeof operator, that's what it's for.)

                            --
                            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.

                            Comment

                            • Richard Heathfield

                              #15
                              Re: sizeof

                              ada said:
                              [color=blue]
                              > Andrew Poelstra <apoelstra@loca lhost.localdoma in> writes:[/color]

                              <snip>
                              [color=blue][color=green]
                              >> Not only that, but with C99
                              >> VLA's, sizeof /cannot/ be computer at compile time in some cases.[/color]
                              > Why?[/color]

                              Here's one reason:

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

                              int main(void)
                              {
                              puts("How many array elements would you like?");
                              char buf[32];
                              if(fgets(buf, sizeof buf, stdin) != NULL)
                              {
                              unsigned long n = strtoul(buf, NULL, 10);
                              if(n > 0)
                              {
                              int arr[n];
                              /* ... */

                              There is simply no way that the compiler can know at compile time how many
                              elements the user will ask for at run time.

                              --
                              Richard Heathfield
                              "Usenet is a strange place" - dmr 29/7/1999

                              email: rjh at above domain (but drop the www, obviously)

                              Comment

                              Working...