pointer and CHAR_SIZE -- confusion

Discussion in 'C Programming' started by Mark, Feb 8, 2013.

  1. Mark

    Mark Guest

    Hello,

    I think I'm confused with relation between 'pointer' and CHAR_BIT value. Is
    there any at all? Specifically, when talking about pointer's size, why do we
    have to consider byte size? I understand that pointers are used with regard
    to how many bytes of memory they can address, but what is direct relation
    with bytes and as such with CHAR_BIT ?

    Thanks.

    Mark
     
    Mark, Feb 8, 2013
    #1
    1. Advertising

  2. Mark

    Noob Guest

    Mark wrote:

    > I think I'm confused with relation between 'pointer' and CHAR_BIT value.


    There are two types of pointers, object pointers and function pointers.
    They need not be the same size (e.g. IA-64 ABI).

    As far as I know, even char pointers and other object pointers need not
    be the same size.
     
    Noob, Feb 8, 2013
    #2
    1. Advertising

  3. Mark

    James Kuyper Guest

    On 02/08/2013 09:04 AM, Mark wrote:
    > Hello,
    >
    > I think I'm confused with relation between 'pointer' and CHAR_BIT value. Is
    > there any at all? ...


    There's no particularly strong one, at least, not one that's specific to
    pointers.

    > ... Specifically, when talking about pointer's size, why do we
    > have to consider byte size? ...


    In C, all objects other than bit-fields have a size that is an integral
    number of bytes. If you want to know how big the object is in bytes, you
    can use the sizeof operator. If you want to known how big it is in bits,
    then you need to multiply the size in bytes by CHAR_BIT. That's just as
    true of pointer types as it is of any other object type.

    The maximum number of different objects that a pointer 'p' could
    possibly point at is pow(2, CHAR_BIT * sizeof(*p)). Thus, the larger the
    pointer, the larger the amount of memory it could be used to address.
    This is an upper limit; the pointer representation might have
    redundancies that prevent it from achieving that upper limit, and a real
    pointer can never validly address more different memory locations than
    are supported by the system (which might, however, be larger than the
    number of different physical memory locations).

    > ... I understand that pointers are used with regard
    > to how many bytes of memory they can address, ...


    I'm not sure what you mean by "used with regard". Depending upon what
    you mean, you understanding may be incorrect. Could you give an example,
    preferably in the form of compilable code?

    > ... but what is direct relation
    > with bytes and as such with CHAR_BIT ?


    If you have two pointers, let's call them p and q, and they both point
    to the same type, and if subtracting them has defined behavior, then
    (p-q) gives the number of objects of that type between the two locations
    they point at. If you multiply by sizeof(*p), that will give you the
    number of bytes between those two locations. If you multiple again by
    CHAR_BIT, you'll get the number of bits.

    Other than that, I can't think of any particular connection between
    pointers and bytes and CHAR_BIT. It might help if you gave us an example
    of whatever it was that led to your question.
    --
    James Kuyper
     
    James Kuyper, Feb 8, 2013
    #3
  4. Mark

    Eric Sosman Guest

    On 2/8/2013 9:04 AM, Mark wrote:
    > Hello,
    >
    > I think I'm confused with relation between 'pointer' and CHAR_BIT value. Is
    > there any at all? Specifically, when talking about pointer's size, why do we
    > have to consider byte size? I understand that pointers are used with regard
    > to how many bytes of memory they can address, but what is direct relation
    > with bytes and as such with CHAR_BIT ?


    You may have assembled fragments of different discussions into
    one amalgamated misunderstanding ;-)

    Bear in mind that the size of a pointer and the size of the thing
    it points at are independent, and may well be different. An `int*'
    might occupy 8 bytes while an `int' occupies 4, or a `double*' might
    occupy 4 bytes while a `double' uses 8. A `char*' points at exactly
    one byte (there may be others nearby), but the pointer itself quite
    likely occupies more than one.

    If you know the number of bytes a certain pointer type occupies,
    you can multiply that quantity by CHAR_BIT to find the number of
    bits in the pointer's value. Raise 2 to that bit count and you get
    an upper bound on the number of distinct values the pointer could
    possibly have. It's not a tight bound, though, because not all
    those values need be valid: On systems where a `double' requires
    8-byte alignment, it's quite likely that at least 3 bits of a
    `double*' are "must be zero" -- and there may be other reserved
    bits, too.

    Finally, be aware that different pointer types may need
    different amounts of storage. On many systems all pointers of
    all kinds are represented the same way (and thus use the same
    amount of memory), but this is not universal and C does not
    require it. On some systems, pointers to functions look quite
    different from pointers to data. A few systems use different
    representations for pointers to different data types (a word-
    addressed machine, for example, might use a plain address to
    represent an `int*', but might need an address plus a byte
    number to point at a particular `char' inside a word). So on
    some systems you might find `sizeof(char*) != sizeof(int*)',
    and if you plan to malloc() space to hold a bunch of pointers
    you need to pay attention to their possibly different sizes.

    --
    Eric Sosman
    d
     
    Eric Sosman, Feb 8, 2013
    #4
  5. Mark

    Shao Miller Guest

    On 2/8/2013 09:04, Mark wrote:
    >
    > I think I'm confused with relation between 'pointer' and CHAR_BIT value. Is
    > there any at all? Specifically, when talking about pointer's size, why do we
    > have to consider byte size? I understand that pointers are used with regard
    > to how many bytes of memory they can address, but what is direct relation
    > with bytes and as such with CHAR_BIT ?
    >


    There is no relationship. 'CHAR_BIT' gives the whole number of bits in
    a byte. A pointer has nothing to do with that, except that a stored
    pointer value happens to be composed of a whole number of bytes.


    --
    - Shao Miller
    --
    "Thank you for the kind words; those are the kind of words I like to hear.

    Cheerily," -- Richard Harter
     
    Shao Miller, Feb 8, 2013
    #5
  6. Mark

    Mark Guest

    "James Kuyper" <> wrote in message
    news:kf37b2$26m$...

    [skip]
    > I'm not sure what you mean by "used with regard". Depending upon what
    > you mean, you understanding may be incorrect. Could you give an example,
    > preferably in the form of compilable code?


    I was reading a thread in the net where question was about size of a pointer
    on 5-bit system, and the answer was that since byte can be atleast 8bits,
    then the size of the pointer on such system can't be 5. Therefore, I thought
    it implies some relation b/w a pointer and CHAR_BIT ?

    Mark
     
    Mark, Feb 8, 2013
    #6
  7. Mark

    James Kuyper Guest

    On 02/08/2013 06:06 PM, Mark wrote:
    > "James Kuyper" <> wrote in message
    > news:kf37b2$26m$...
    >
    > [skip]
    >> I'm not sure what you mean by "used with regard". Depending upon what
    >> you mean, you understanding may be incorrect. Could you give an example,
    >> preferably in the form of compilable code?

    >
    > I was reading a thread in the net where question was about size of a pointer
    > on 5-bit system, and the answer was that since byte can be atleast 8bits,
    > then the size of the pointer on such system can't be 5. Therefore, I thought
    > it implies some relation b/w a pointer and CHAR_BIT ?


    Well, yes. Like any other object type that isn't a bit-field, a pointer
    type cannot have fewer than CHAR_BIT bits. It's nothing special about
    pointers. You also can't have an integer type with fewer than CHAR_BIT
    bits, not even _Bool (though some of them can be padding bits). Even on
    an implementation where CHAR_BIT was 128, a conforming implementation of
    C can't support strictly conforming code that uses a floating point type
    with fewer than CHAR_BIT bits.

    There was discussion about the fact that if you write code with
    undefined behavior, that behavior might include the compiler providing
    support for smaller types, but when you have to invoke undefined
    behavior to make something work, you're talking about an extension to
    standard C, not standard C itself.
     
    James Kuyper, Feb 9, 2013
    #7
  8. Mark <> wrote:
    >
    > "James Kuyper" <> wrote in message
    > news:kf37b2$26m$...


    > [skip]
    >> I'm not sure what you mean by "used with regard". Depending upon what
    >> you mean, you understanding may be incorrect. Could you give an example,
    >> preferably in the form of compilable code?


    > I was reading a thread in the net where question was about size of a pointer
    > on 5-bit system, and the answer was that since byte can be atleast 8bits,
    > then the size of the pointer on such system can't be 5. Therefore, I thought
    > it implies some relation b/w a pointer and CHAR_BIT ?


    CHAR_BIT is required to be at least 8, even though systems with six
    and seven bit character sets (used to be) not so rare.

    Other types are defined as multiples (possibly 1) of sizeof(char).

    That worked well for the early C systems, for most today, but maybe
    not for all.

    It used to be systems with 36 bit or 60 bit words that complicated it.

    Now, more and more systems use 16 bit characters.

    -- glen
     
    glen herrmannsfeldt, Feb 9, 2013
    #8
  9. christian.bau <> wrote:
    > On Feb 8, 11:06 pm, "Mark" <> wrote:


    >> I was reading a thread in the net where question was about size of a pointer
    >> on 5-bit system, and the answer was that since byte can be atleast 8bits,
    >> then the size of the pointer on such system can't be 5. Therefore, I thought
    >> it implies some relation b/w a pointer and CHAR_BIT ?


    > That is very, very, very confused.


    > In C, a byte is at least 8 bits, and nothing less than a byte can be
    > addressed. If one item in memory is only 5 bits, then one item in
    > memory cannot be a C byte. What a C byte is would depend on the
    > implementation, but most likely it would be two memory items or 10
    > bits.
    > A char pointer must be capable of addressing at least 65536 bytes
    > according to the C rules, so a char pointer must be at least 16 bits.


    I don't rememeber a discussion about 5 bits, but the 4 bit 4004
    has 8 bit index registers. You can address ROM, but only within
    the 256 byte page of the current instruction, or one of five
    pages of RAM, each page selected using a different instruction.

    > It must also be a whole number of bytes, so it would probably be two
    > bytes = 20 bits or three bytes = 30 bits. It could also by five bytes
    > = 50 bits; a bit unusual, but there is no reason in the C language why
    > it shouldn't be.


    -- glen
     
    glen herrmannsfeldt, Feb 10, 2013
    #9
    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. Replies:
    10
    Views:
    745
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    876
    Jordan Abel
    Mar 28, 2006
  3. Kavya
    Replies:
    2
    Views:
    397
  4. Replies:
    4
    Views:
    1,332
    Fred Zwarts
    Jul 2, 2009
  5. A
    Replies:
    7
    Views:
    658
Loading...

Share This Page