size of a sizeof(pointer)

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

    size of a sizeof(pointer)

    what is the size of a pointer?

    suppose i am writing,


    datatype *ptr;
    sizeof(ptr);


    now what does this sizeof(ptr) will give? will it give the size of the
    data the pointer is pointing to?

    if no, can you give an counter example?

    basically , i want to know what is the meaning of size of a ponter.

    as you know

    sizeof(int)=4;

    sizeof(char)= 2;

    but what does sizeof(ptr) means??

    can anybody explain?
  • Josh Sebastian

    #2
    Re: size of a sizeof(pointer)

    On Sun, 08 Feb 2004 11:37:15 -0800, syntax wrote:
    [color=blue]
    > what is the size of a pointer?
    >
    > suppose i am writing,
    >
    >
    > datatype *ptr;
    > sizeof(ptr);
    >
    >
    > now what does this sizeof(ptr) will give? will it give the size of the
    > data the pointer is pointing to?
    >
    > if no, can you give an counter example?
    >
    > basically , i want to know what is the meaning of size of a ponter.
    >
    > as you know
    >
    > sizeof(int)=4;[/color]

    Maybe. It must be >= 2.
    [color=blue]
    > sizeof(char)= 2;[/color]

    sizeof(char) is, by definition, 1.
    [color=blue]
    > but what does sizeof(ptr) means??[/color]

    It's the amount of space the pointer itself takes up. Not the data pointed
    to, but the pointer itself. Often, it's == sizeof(int).

    Josh

    Comment

    • Malcolm

      #3
      Re: size of a sizeof(pointer)


      "syntax" <sanko50@yahoo. com.hk> wrote in message[color=blue]
      > what is the size of a pointer?
      >[/color]
      A pointer is a variable that holds an address. The size of a pointer is the
      size of this address.
      For instance, most computers have an address space of 4GB. 32 bits allows
      you 4GB, so the size of a pointer will be 32 bits, or 4 (char is usually 8
      bits). On some microcomputers the address space is only 64K, so 16-bit
      pointers are used.[color=blue]
      >
      > datatype *ptr;
      > sizeof(ptr);
      >
      > now what does this sizeof(ptr) will give? will it give the size of the
      > data the pointer is pointing to?
      >[/color]
      No, it gives the size of the pointer, probably 4.[color=blue]
      >
      > if no, can you give an counter example?
      >[/color]
      One confusing thing about C is that arrays and pointer have array/pointer
      equivalence.

      char string[32];

      printf("sizeof string %d\n", (int) sizeof(string)) ;

      will give you 32.

      char *string = malloc(32);

      printf(" sizeof string %d\n", (int) sizeof(string)) ;

      will give you the size of a pointer on your system, probably 4.[color=blue]
      >
      > basically , i want to know what is the meaning of size of a ponter.
      >
      > as you know
      >
      > sizeof(int)=4;
      >
      > sizeof(char)= 2;
      >[/color]
      sizeof(char) is always 1, one of the little quirks of the C language.
      sizeof(int) is very commonly 4, but it can be any size. It is meant to be
      the natural size for the machine to use, which means the width of the
      register.
      For technical reasons pointers are usually the same size as ints, but again
      they can be any size.[color=blue]
      >
      > but what does sizeof(ptr) means??
      >[/color]



      Comment

      • Richard Heathfield

        #4
        Re: size of a sizeof(pointer)

        Josh Sebastian wrote:
        [color=blue]
        > On Sun, 08 Feb 2004 11:37:15 -0800, syntax wrote:
        >[color=green]
        >> as you know
        >>
        >> sizeof(int)=4;[/color]
        >
        > Maybe. It must be >= 2.[/color]

        Wrong. It must, however, be an exact multiple of 1.
        [color=blue][color=green]
        >> sizeof(char)= 2;[/color]
        >
        > sizeof(char) is, by definition, 1.[/color]

        Right.
        [color=blue]
        >[color=green]
        >> but what does sizeof(ptr) means??[/color]
        >
        > It's the amount of space the pointer itself takes up. Not the data pointed
        > to, but the pointer itself. Often, it's == sizeof(int).[/color]

        But, of course, it doesn't have to be (as you know).

        --
        Richard Heathfield : binary@eton.pow ernet.co.uk
        "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
        C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
        K&R answers, C books, etc: http://users.powernet.co.uk/eton

        Comment

        • Josh Sebastian

          #5
          Re: size of a sizeof(pointer)

          On Sun, 08 Feb 2004 19:58:20 +0000, Richard Heathfield wrote:
          [color=blue]
          > Josh Sebastian wrote:
          >[color=green]
          >> On Sun, 08 Feb 2004 11:37:15 -0800, syntax wrote:
          >>[color=darkred]
          >>> as you know
          >>>
          >>> sizeof(int)=4;[/color]
          >>
          >> Maybe. It must be >= 2.[/color]
          >
          > Wrong. It must, however, be an exact multiple of 1.[/color]

          Jeez... yeah, thanks.

          Comment

          • Keith Thompson

            #6
            Re: size of a sizeof(pointer)

            Josh Sebastian <curien@cox.net > writes:[color=blue]
            > On Sun, 08 Feb 2004 11:37:15 -0800, syntax wrote:[/color]
            [...][color=blue][color=green]
            > > but what does sizeof(ptr) means??[/color]
            >
            > It's the amount of space the pointer itself takes up. Not the data pointed
            > to, but the pointer itself. Often, it's == sizeof(int).[/color]

            It's true that the size of a pointer is often equal to sizeof(int),
            but it's dangerous (an unnecessary) to assume that it always is.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
            Schroedinger does Shakespeare: "To be *and* not to be"

            Comment

            • Grumble

              #7
              Re: size of a sizeof(pointer)

              Richard Heathfield wrote:
              [color=blue]
              > Josh Sebastian wrote:
              >[color=green]
              >> syntax wrote:
              >>[color=darkred]
              >>> sizeof(int)=4;[/color]
              >>
              >> Maybe. It must be >= 2.[/color]
              >
              > Wrong. It must, however, be an exact multiple of 1.[/color]

              An implementation cannot have 16-bit chars and 24-bit ints?

              How about 16-bit chars and 24-bit pointers?

              Comment

              • pete

                #8
                Re: size of a sizeof(pointer)

                Grumble wrote:[color=blue]
                >
                > Richard Heathfield wrote:
                >[color=green]
                > > Josh Sebastian wrote:
                > >[color=darkred]
                > >> syntax wrote:
                > >>
                > >>> sizeof(int)=4;
                > >>
                > >> Maybe. It must be >= 2.[/color]
                > >
                > > Wrong. It must, however, be an exact multiple of 1.[/color][/color]

                It must be greater than 1, on hosted implementations .
                [color=blue]
                > An implementation cannot have 16-bit chars and 24-bit ints?[/color]

                The sum of the numbers of padding bits,
                value bits and the sign bit, is a multiple of CHAR_BIT.
                [color=blue]
                > How about 16-bit chars and 24-bit pointers?[/color]

                The bit representation of pointers is not specified.

                --
                pete

                Comment

                • Mark McIntyre

                  #9
                  Re: size of a sizeof(pointer)

                  On Mon, 09 Feb 2004 12:40:21 GMT, in comp.lang.c , pete
                  <pfiland@mindsp ring.com> wrote:
                  [color=blue]
                  >Grumble wrote:[color=green]
                  >>
                  >> Richard Heathfield wrote:
                  >>[color=darkred]
                  >> > Josh Sebastian wrote:
                  >> >
                  >> >> syntax wrote:
                  >> >>
                  >> >>> sizeof(int)=4;
                  >> >>
                  >> >> Maybe. It must be >= 2.
                  >> >
                  >> > Wrong. It must, however, be an exact multiple of 1.[/color][/color]
                  >
                  >It must be greater than 1, on hosted implementations .[/color]

                  Not if a char were 16 bits wide.


                  --
                  Mark McIntyre
                  CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                  CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>


                  ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
                  http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
                  ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

                  Comment

                  • Richard Bos

                    #10
                    Re: size of a sizeof(pointer)

                    pete <pfiland@mindsp ring.com> wrote:
                    [color=blue]
                    > Grumble wrote:[color=green]
                    > >
                    > > Richard Heathfield wrote:
                    > >[color=darkred]
                    > > > Josh Sebastian wrote:
                    > > >
                    > > >> syntax wrote:
                    > > >>
                    > > >>> sizeof(int)=4;
                    > > >>
                    > > >> Maybe. It must be >= 2.
                    > > >
                    > > > Wrong. It must, however, be an exact multiple of 1.[/color][/color]
                    >
                    > It must be greater than 1, on hosted implementations .[/color]

                    Chapter and verse, please.

                    Of course, it's exceedingly awkward for a hosted implementation to have
                    sizeof(int)==1, but it isn't illegal.
                    [color=blue][color=green]
                    > > An implementation cannot have 16-bit chars and 24-bit ints?[/color]
                    >
                    > The sum of the numbers of padding bits,
                    > value bits and the sign bit, is a multiple of CHAR_BIT.
                    >[color=green]
                    > > How about 16-bit chars and 24-bit pointers?[/color]
                    >
                    > The bit representation of pointers is not specified.[/color]

                    Even so, all types have sizes measurable in whole chars; look up the
                    definition of sizeof.

                    Richard

                    Comment

                    • Mark A. Odell

                      #11
                      Re: size of a sizeof(pointer)

                      "Mike Wahler" <mkwahler@mkwah ler.net> wrote in
                      news:qJxVb.2006 0$uM2.15340@new sread1.news.pas .earthlink.net:
                      [color=blue][color=green][color=darkred]
                      >> > to, but the pointer itself. Often, it's == sizeof(int).[/color]
                      >>
                      >> It's true that the size of a pointer is often equal to sizeof(int),
                      >> but it's dangerous (an unnecessary) to assume that it always is.[/color]
                      >
                      > Or for that matter, to assume that all pointer types have the same size.[/color]

                      Indeed. For example, Keil C51 has 1 byte, 2 byte, and 3 byte pointer sizes
                      depending upon which memory space the pointer points to.

                      Comment

                      • Grumble

                        #12
                        Re: size of a sizeof(pointer)

                        Richard Bos wrote:
                        [color=blue]
                        > Of course, it's exceedingly awkward for a hosted implementation
                        > to have sizeof(int)==1 [...][/color]

                        Is it awkward because getc() can return either a char or EOF?

                        Comment

                        • Richard Bos

                          #13
                          Re: size of a sizeof(pointer)

                          Grumble <invalid@kma.eu .org> wrote:
                          [color=blue]
                          > Richard Bos wrote:
                          >[color=green]
                          > > Of course, it's exceedingly awkward for a hosted implementation
                          > > to have sizeof(int)==1 [...][/color]
                          >
                          > Is it awkward because getc() can return either a char or EOF?[/color]

                          That, and related problems, yes. If you need to take these legal-but-
                          unlikely implementations into account (i.e., if you really want to be as
                          anal-retentive about ISO-conformance as your common huff-throwing newbie
                          (and uncommon troll) makes us out to be), you need to check for feof()
                          and ferror() after every read operation, instead of simply for EOF.
                          Personally, I never do.

                          Richard

                          Comment

                          • Arthur J. O'Dwyer

                            #14
                            Re: size of a sizeof(pointer)


                            On Mon, 9 Feb 2004, Richard Bos wrote:[color=blue]
                            > pete <pfiland@mindsp ring.com> wrote:[color=green]
                            > > Grumble wrote:[color=darkred]
                            > > > Richard Heathfield wrote:
                            > > > > Josh Sebastian wrote:
                            > > > >> [sizeof(int)] must be >= 2.
                            > > > >
                            > > > > Wrong. It must, however, be an exact multiple of 1.[/color]
                            > >
                            > > It must be greater than 1, on hosted implementations .[/color]
                            >
                            > Chapter and verse, please.[/color]

                            <bu63eq$mtp$9@s unnews.cern.ch>
                            and subsequent posts. This should be a FAQ.

                            -Arthur

                            Comment

                            • Papadopoulos Giannis

                              #15
                              Re: size of a sizeof(pointer)

                              Mark McIntyre wrote:
                              [color=blue]
                              > On Mon, 09 Feb 2004 12:40:21 GMT, in comp.lang.c , pete
                              > <pfiland@mindsp ring.com> wrote:
                              >
                              >[color=green]
                              >>Grumble wrote:
                              >>[color=darkred]
                              >>>Richard Heathfield wrote:
                              >>>
                              >>>
                              >>>>Josh Sebastian wrote:
                              >>>>
                              >>>>
                              >>>>>syntax wrote:
                              >>>>>
                              >>>>>
                              >>>>>>sizeof(in t)=4;
                              >>>>>
                              >>>>>Maybe. It must be >= 2.
                              >>>>
                              >>>>Wrong. It must, however, be an exact multiple of 1.[/color]
                              >>
                              >>It must be greater than 1, on hosted implementations .[/color]
                              >
                              >
                              > Not if a char were 16 bits wide.
                              >
                              >[/color]

                              Is there any alive implementation that uses 16bit chars?? (I know of the
                              existance of a machine that a byte is 6-bit)

                              --
                              #include <stdio.h>
                              #define p(s) printf(#s" endian")
                              int main(void){int v=1;*(char*)&v? p(Little):p(Big );return 0;}

                              Giannis Papadopoulos

                              University of Thessaly
                              Computer & Communications Engineering dept.

                              Comment

                              Working...