size of a sizeof(pointer)

Discussion in 'C Programming' started by syntax, Feb 8, 2004.

  1. syntax

    syntax Guest

    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?
    syntax, Feb 8, 2004
    #1
    1. Advertising

  2. On Sun, 08 Feb 2004 11:37:15 -0800, syntax wrote:

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


    Maybe. It must be >= 2.

    > sizeof(char)= 2;


    sizeof(char) is, by definition, 1.

    > but what does sizeof(ptr) means??


    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
    Josh Sebastian, Feb 8, 2004
    #2
    1. Advertising

  3. syntax

    Malcolm Guest

    "syntax" <> wrote in message
    > what is the size of a pointer?
    >

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

    No, it gives the size of the pointer, probably 4.
    >
    > if no, can you give an counter example?
    >

    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.
    >
    > basically , i want to know what is the meaning of size of a ponter.
    >
    > as you know
    >
    > sizeof(int)=4;
    >
    > sizeof(char)= 2;
    >

    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.
    >
    > but what does sizeof(ptr) means??
    >
    Malcolm, Feb 8, 2004
    #3
  4. Josh Sebastian wrote:

    > On Sun, 08 Feb 2004 11:37:15 -0800, syntax wrote:
    >
    >> as you know
    >>
    >> sizeof(int)=4;

    >
    > Maybe. It must be >= 2.


    Wrong. It must, however, be an exact multiple of 1.

    >> sizeof(char)= 2;

    >
    > sizeof(char) is, by definition, 1.


    Right.

    >
    >> but what does sizeof(ptr) means??

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


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

    --
    Richard Heathfield :
    "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
    Richard Heathfield, Feb 8, 2004
    #4
  5. On Sun, 08 Feb 2004 19:58:20 +0000, Richard Heathfield wrote:

    > Josh Sebastian wrote:
    >
    >> On Sun, 08 Feb 2004 11:37:15 -0800, syntax wrote:
    >>
    >>> as you know
    >>>
    >>> sizeof(int)=4;

    >>
    >> Maybe. It must be >= 2.

    >
    > Wrong. It must, however, be an exact multiple of 1.


    Jeez... yeah, thanks.
    Josh Sebastian, Feb 8, 2004
    #5
  6. Josh Sebastian <> writes:
    > On Sun, 08 Feb 2004 11:37:15 -0800, syntax wrote:

    [...]
    > > but what does sizeof(ptr) means??

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


    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_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
    Keith Thompson, Feb 8, 2004
    #6
  7. syntax

    Mike Wahler Guest

    "Keith Thompson" <> wrote in message
    news:...
    > Josh Sebastian <> writes:
    > > On Sun, 08 Feb 2004 11:37:15 -0800, syntax wrote:

    > [...]
    > > > but what does sizeof(ptr) means??

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

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


    Or for that matter, to assume that all pointer types have the same size.

    -Mike
    Mike Wahler, Feb 8, 2004
    #7
  8. "Malcolm" <> writes:
    [...]
    > One confusing thing about C is that arrays and pointer have array/pointer
    > equivalence.


    No, there is no array/pointer equivalence (or rather, "equivalence" is
    a misleading term for what's really going on). Array names are
    implicitly converted to pointer values in many contexts.

    See the C FAQ at <http://www.eskimo.com/~scs/C-faq/faq.html>,
    particularly section 6, particularly question 6.3.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
    Keith Thompson, Feb 8, 2004
    #8
  9. syntax

    Malcolm Guest

    "Keith Thompson" <> wrote in message
    >
    > No, there is no array/pointer equivalence (or rather, "equivalence" is
    > a misleading term for what's really going on). Array names are
    > implicitly converted to pointer values in many contexts.
    >
    > See the C FAQ at <http://www.eskimo.com/~scs/C-faq/faq.html>,
    > particularly section 6, particularly question 6.3.
    >

    Exactly. "Equivalence" is the accepted term for what is going on, which is
    confusing.
    Malcolm, Feb 8, 2004
    #9
  10. syntax

    CBFalconer Guest

    Malcolm wrote:
    > "syntax" <> wrote in message
    >
    > > what is the size of a pointer?
    > >

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


    Nope. A pointer points. What information it needs to hold to do
    that is up to the implementation. It could consist of a URL and
    other information, just as a not too wild example. Another might
    be "Malcolms house, under the bed beside the dirty socks, last
    Tuesday". The amount of information needed is usually constrained
    by limiting the things that the pointer is allowed to point to.
    Clear now?

    At any rate the C expression "sizeof ptr", where ptr is an actual
    pointer, is available to tell you how much space that particular
    implementation needs for the job.

    Sometimes that pointer may be a real memory address. Today it
    more often represents an offset from another pointer which points
    to a block of some sort of storage. You should neither know nor
    care, unless you are implementing the system.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Feb 9, 2004
    #10
  11. syntax

    Jack Klein Guest

    On Sun, 08 Feb 2004 14:40:52 -0500, Josh Sebastian <>
    wrote in comp.lang.c:

    > On Sun, 08 Feb 2004 11:37:15 -0800, syntax wrote:
    >
    > > 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;

    >
    > Maybe. It must be >= 2.


    No, the number of bits in an int must be at least 16, but on some
    platforms CHAR_BIT is greater than 8.

    I am actually developing code right now on for a Texas Instruments
    2812 DSP with their Code Composer Studio. CHAR_BIT is 16. The types
    char, signed char, unsigned char, signed short, unsigned short, signed
    int and unsigned int all contain 16 bits and the sizeof operator
    yields a value of 1 for each and every one of these types. The
    processor only reads and writes memory in 16 bit words.

    In the past I have worked with a 32 bit DSP from Analog devices which
    only addressed memory in 32 bit words. CHAR_BIT was 32. All the
    integer types (this was before C99, so there was no long long type)
    had 32 bits and sizeof yielded 1, even for signed and unsigned long.

    > sizeof(char) is, by definition, 1.


    This is true.

    > > but what does sizeof(ptr) means??

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


    And often it is not. Under the TI compiler I mentioned above, while
    int has 16 bits and sizeof(int) is, pointers have 32 bits and
    sizeof(void *) is 2.

    Under Keil's compiler for the 8051, int has 16 bits and occupies 2
    bytes, sizeof(void *) is 3, although it doesn't really use all 24
    bits.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Feb 9, 2004
    #11
  12. syntax

    Grumble Guest

    Richard Heathfield wrote:

    > Josh Sebastian wrote:
    >
    >> syntax wrote:
    >>
    >>> sizeof(int)=4;

    >>
    >> Maybe. It must be >= 2.

    >
    > Wrong. It must, however, be an exact multiple of 1.


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

    How about 16-bit chars and 24-bit pointers?
    Grumble, Feb 9, 2004
    #12
  13. syntax

    pete Guest

    Grumble wrote:
    >
    > Richard Heathfield wrote:
    >
    > > Josh Sebastian wrote:
    > >
    > >> syntax wrote:
    > >>
    > >>> sizeof(int)=4;
    > >>
    > >> Maybe. It must be >= 2.

    > >
    > > Wrong. It must, however, be an exact multiple of 1.


    It must be greater than 1, on hosted implementations.

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


    The sum of the numbers of padding bits,
    value bits and the sign bit, is a multiple of CHAR_BIT.

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


    The bit representation of pointers is not specified.

    --
    pete
    pete, Feb 9, 2004
    #13
  14. syntax

    Richard Bos Guest

    pete <> wrote:

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

    >
    > It must be greater than 1, on hosted implementations.


    Chapter and verse, please.

    Of course, it's exceedingly awkward for a hosted implementation to have
    sizeof(int)==1, but it isn't illegal.

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

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

    >
    > The bit representation of pointers is not specified.


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

    Richard
    Richard Bos, Feb 9, 2004
    #14
  15. On Mon, 09 Feb 2004 12:40:21 GMT, in comp.lang.c , pete
    <> wrote:

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

    >
    >It must be greater than 1, on hosted implementations.


    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.com/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 =---
    Mark McIntyre, Feb 9, 2004
    #15
  16. syntax

    Richard Bos Guest

    Grumble <> wrote:

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

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


    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
    Richard Bos, Feb 9, 2004
    #16
  17. "Mike Wahler" <> wrote in
    news:qJxVb.20060$:

    >> > to, but the pointer itself. Often, it's == sizeof(int).

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

    >
    > Or for that matter, to assume that all pointer types have the same size.


    Indeed. For example, Keil C51 has 1 byte, 2 byte, and 3 byte pointer sizes
    depending upon which memory space the pointer points to.
    Mark A. Odell, Feb 9, 2004
    #17
  18. syntax

    Grumble Guest

    Richard Bos wrote:

    > Of course, it's exceedingly awkward for a hosted implementation
    > to have sizeof(int)==1 [...]


    Is it awkward because getc() can return either a char or EOF?
    Grumble, Feb 9, 2004
    #18
  19. On Mon, 9 Feb 2004, Richard Bos wrote:
    > pete <> wrote:
    > > Grumble wrote:
    > > > Richard Heathfield wrote:
    > > > > Josh Sebastian wrote:
    > > > >> [sizeof(int)] must be >= 2.
    > > > >
    > > > > Wrong. It must, however, be an exact multiple of 1.

    > >
    > > It must be greater than 1, on hosted implementations.

    >
    > Chapter and verse, please.


    <bu63eq$mtp$>
    and subsequent posts. This should be a FAQ.

    -Arthur
    Arthur J. O'Dwyer, Feb 9, 2004
    #19
  20. Mark McIntyre wrote:

    > On Mon, 09 Feb 2004 12:40:21 GMT, in comp.lang.c , pete
    > <> wrote:
    >
    >
    >>Grumble wrote:
    >>
    >>>Richard Heathfield wrote:
    >>>
    >>>
    >>>>Josh Sebastian wrote:
    >>>>
    >>>>
    >>>>>syntax wrote:
    >>>>>
    >>>>>
    >>>>>>sizeof(int)=4;
    >>>>>
    >>>>>Maybe. It must be >= 2.
    >>>>
    >>>>Wrong. It must, however, be an exact multiple of 1.

    >>
    >>It must be greater than 1, on hosted implementations.

    >
    >
    > Not if a char were 16 bits wide.
    >
    >


    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
    http://dop.users.uth.gr/
    University of Thessaly
    Computer & Communications Engineering dept.
    Papadopoulos Giannis, Feb 9, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek
    Replies:
    7
    Views:
    24,324
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    626
    CBFalconer
    Apr 10, 2004
  3. Vinu
    Replies:
    13
    Views:
    1,410
    Lawrence Kirby
    May 12, 2005
  4. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    554
    Joe Smith
    May 22, 2006
  5. Alex Vinokur

    sizeof (size_t) and sizeof (pointer)

    Alex Vinokur, Nov 12, 2007, in forum: C++
    Replies:
    19
    Views:
    785
    Ben Rudiak-Gould
    Nov 30, 2007
Loading...

Share This Page