Velocity Reviews > size of a sizeof(pointer)

# size of a sizeof(pointer)

Jack Klein
Guest
Posts: n/a

 02-09-2004
On Sun, 08 Feb 2004 14:40:52 -0500, Josh Sebastian <(E-Mail Removed)>
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/~a...FAQ-acllc.html

Grumble
Guest
Posts: n/a

 02-09-2004
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?

pete
Guest
Posts: n/a

 02-09-2004
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

Richard Bos
Guest
Posts: n/a

 02-09-2004
pete <(E-Mail Removed)> 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.

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

Mark McIntyre
Guest
Posts: n/a

 02-09-2004
On Mon, 09 Feb 2004 12:40:21 GMT, in comp.lang.c , pete
<(E-Mail Removed)> 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>

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

Richard Bos
Guest
Posts: n/a

 02-09-2004
Grumble <(E-Mail Removed)> 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
(and uncommon troll) makes us out to be), you need to check for feof()
Personally, I never do.

Richard

Mark A. Odell
Guest
Posts: n/a

 02-09-2004
"Mike Wahler" <(E-Mail Removed)> wrote in

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

Grumble
Guest
Posts: n/a

 02-09-2004
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?

Arthur J. O'Dwyer
Guest
Posts: n/a

 02-09-2004

On Mon, 9 Feb 2004, Richard Bos wrote:
> pete <(E-Mail Removed)> 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.

>

<bu63eq\$mtp\$(E-Mail Removed)>
and subsequent posts. This should be a FAQ.

-Arthur

Guest
Posts: n/a

 02-09-2004
Mark McIntyre wrote:

> On Mon, 09 Feb 2004 12:40:21 GMT, in comp.lang.c , pete
> <(E-Mail Removed)> 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)(Big);return 0;}

http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.