pointer and CHAR_SIZE -- confusion

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

  1. Mark

    Mark Guest


    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 ?


    Mark, Feb 8, 2013
    1. Advertisements

  2. Mark

    Noob Guest

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

  3. Mark

    James Kuyper Guest

    There's no particularly strong one, at least, not one that's specific to
    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'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?
    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, Feb 8, 2013
  4. Mark

    Eric Sosman Guest

    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, Feb 8, 2013
  5. Mark

    Shao Miller Guest

    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, Feb 8, 2013
  6. Mark

    Mark Guest

    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, Feb 8, 2013
  7. Mark

    James Kuyper Guest

    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
  8. 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
  9. 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.
    -- glen
    glen herrmannsfeldt, Feb 10, 2013
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.