Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointer and CHAR_SIZE -- confusion

Reply
Thread Tools

pointer and CHAR_SIZE -- confusion

 
 
Mark
Guest
Posts: n/a
 
      02-08-2013
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


 
Reply With Quote
 
 
 
 
Noob
Guest
Posts: n/a
 
      02-08-2013
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.

 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      02-08-2013
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
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      02-08-2013
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
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
Shao Miller
Guest
Posts: n/a
 
      02-08-2013
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
 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      02-08-2013

"James Kuyper" <(E-Mail Removed)> wrote in message
news:kf37b2$26m$(E-Mail Removed)...

[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


 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      02-09-2013
On 02/08/2013 06:06 PM, Mark wrote:
> "James Kuyper" <(E-Mail Removed)> wrote in message
> news:kf37b2$26m$(E-Mail Removed)...
>
> [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.
 
Reply With Quote
 
glen herrmannsfeldt
Guest
Posts: n/a
 
      02-09-2013
Mark <(E-Mail Removed)> wrote:
>
> "James Kuyper" <(E-Mail Removed)> wrote in message
> news:kf37b2$26m$(E-Mail Removed)...


> [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
 
Reply With Quote
 
glen herrmannsfeldt
Guest
Posts: n/a
 
      02-10-2013
christian.bau <(E-Mail Removed)> wrote:
> On Feb 8, 11:06*pm, "Mark" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
confusion involving pointer to pointer when converting from derived to base Kavya C++ 2 12-23-2006 07:31 PM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments