pointer and CHAR_SIZE -- confusion

M

Mark

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
 
N

Noob

Mark said:
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.
 
J

James Kuyper

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

Eric Sosman

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

Shao Miller

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

Mark

[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
 
J

James Kuyper

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

glen herrmannsfeldt

Mark said:
[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
 
G

glen herrmannsfeldt

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top