sizeof(void*) versus sizeof(foo*) 
Author Message
 sizeof(void*) versus sizeof(foo*)

Is it correct to assume that sizeof(void*) always equals sizeof(foo*)?
(Substitute any type you'd like for "foo").
--
Benjamin


Fri, 05 Nov 2004 09:00:31 GMT  
 sizeof(void*) versus sizeof(foo*)
Quote:

> Is it correct to assume that sizeof(void*) always equals sizeof(foo*)?
> (Substitute any type you'd like for "foo").

[-]
No.


Fri, 05 Nov 2004 09:31:48 GMT  
 sizeof(void*) versus sizeof(foo*)
On Sun, 19 May 2002 21:00:31 -0400, Benjamin Rutt

Quote:
> Is it correct to assume that sizeof(void*) always equals sizeof(foo*)?
> (Substitute any type you'd like for "foo").

This is true if and only if foo has one of the three character types,
signed char, unsigned char, or plain char.  Pointer to void and
pointer to char have exactly the same size and representation.
Pointer to other object types may be smaller, but not larger, than
pointer to void.

There is no conversion at all between pointer to void and pointer to
any type of function, so there is no guarantee at all about their
relative sizes.

--
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++ ftp://snurse-l.org/pub/acllc-c++/faq



Fri, 05 Nov 2004 13:58:57 GMT  
 sizeof(void*) versus sizeof(foo*)


| On Sun, 19 May 2002 21:00:31 -0400, Benjamin Rutt

|
| > Is it correct to assume that sizeof(void*) always equals sizeof(foo*)?
| > (Substitute any type you'd like for "foo").
|
| This is true if and only if foo has one of the three character types,
| signed char, unsigned char, or plain char.

Allow me to add the case that foo is type void.

<snip>



Fri, 05 Nov 2004 14:57:35 GMT  
 sizeof(void*) versus sizeof(foo*)


[snip]

Quote:
> |
> | Which means that you simply said that types 'void*'
> | and 'void*' have the same size.  Duh. :-)

> Yes, it is obvious that sizeof(void *) == sizeof(void *), but I thought
the
> correction was appropriate as Jack Klein phrased it as 'if and only if',
so
> excluding the case where foo is of  type void.

Ah, OK I see what you mean.  I originally misuderstood.  Apologies.

-Mike



Sat, 06 Nov 2004 00:42:58 GMT  
 sizeof(void*) versus sizeof(foo*)

wrote in comp.lang.c:

Quote:
> On Sun, 19 May 2002 21:00:31 -0400, Benjamin Rutt

> > Is it correct to assume that sizeof(void*) always equals sizeof(foo*)?
> > (Substitute any type you'd like for "foo").

> This is true if and only if foo has one of the three character types,
> signed char, unsigned char, or plain char.  Pointer to void and
> pointer to char have exactly the same size and representation.
> Pointer to other object types may be smaller, but not larger, than
> pointer to void.

> There is no conversion at all between pointer to void and pointer to
> any type of function, so there is no guarantee at all about their
> relative sizes.

<red faced>

Must have been a bad hair day.  The first two sentences of my answer
were perfectly correct, as was the entire second paragraph.  But, not
content to leave well enough alone, I drew, aimed, and shot myself in
the foot.

What I meant to say was that pointer to void and a pointer to any of
the character types must be able to hold at least as much information,
and perhaps more, than any other pointer to object types.  That says
nothing at all about their relative sizes.

--
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++ ftp://snurse-l.org/pub/acllc-c++/faq



Sat, 06 Nov 2004 11:09:14 GMT  
 sizeof(void*) versus sizeof(foo*)

Quote:
>Is it correct to assume that sizeof(void*) always equals sizeof(foo*)?
>(Substitute any type you'd like for "foo").

Pointers to different types may have different representations.  This
includes different sizes, too.  

The only guarantee you have is that sizeof(void *) == sizeof(char *).

If we exclude pointers to functions, no pointer type is likely to be
wider than char *.  This is not guaranteed by the standard, though.

Back in the days of 8086 C compilers, you could have sizeof(ptr) <
sizeof(funcptr), sizeof(ptr) == sizeof(funcptr) or sizeof(ptr) >
sizeof(funcptr), depending on the memory model you used to compile the
program.  This is far less common today, at least on hosted
implementations.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Sat, 06 Nov 2004 20:53:44 GMT  
 sizeof(void*) versus sizeof(foo*)

Quote:

>What I meant to say was that pointer to void and a pointer to any of
>the character types must be able to hold at least as much information,
>and perhaps more, than any other pointer to object types.  That says
>nothing at all about their relative sizes.

I don't think that is true either: consider an architecture with tagged
pointers. When casting to void you can strip the tag off, and reconstruct
it when casting back.

Tony.
--

FAIR ISLE: SOUTHEASTERLY 4 OR 5 INCREASING 6 OR 7, PERHAPS GALE 8 LATER.
OCCASIONAL RAIN. MODERATE, WITH FOG PATCHES AT FIRST.



Sun, 07 Nov 2004 06:43:34 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. sizeof(foo) -1 or sizeof(foo)

2. relation between sizeof(void*), sizeof(int)

3. difference between void foo(void) and void foo()

4. question about malloc and sizeof *foo

5. sizeof (struct) ! = sizeof struct elements

6. sizeof without sizeof

7. sizeof a/sizeof *a

8. sizeof(int) sizeof(long) etc.

9. Problem with sizeof of a struct void pointer

10. void *, ptr arithmetic and sizeof()

11. sizeof void*

12. sizeof(void) ??

 

 
Powered by phpBB® Forum Software