PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > sizeof(ptr) = ?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
sizeof(ptr) = ?

Réponse
 
LinkBack Outils de la discussion
Vieux 22/10/2007, 09h10   #1
William Xu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut sizeof(ptr) = ?

I test it with char, short, int, long, float, double and struct
pointers, all return 4 bytes. Does the standard say anything about
sizeof a pointer? (I didn't find it in the standard...)

--
William

http://williamxu.net9.org
  Réponse avec citation
Vieux 22/10/2007, 09h13   #2
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

William Xu wrote:
> I test it with char, short, int, long, float, double and struct
> pointers, all return 4 bytes. Does the standard say anything about
> sizeof a pointer? (I didn't find it in the standard...)
>

No, it is implementation specific.

--
Ian Collins.
  Réponse avec citation
Vieux 22/10/2007, 09h34   #3
Road Tang
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

William Xu wrote:

> I test it with char, short, int, long, float, double and struct
> pointers, all return 4 bytes. Does the standard say anything about
> sizeof a pointer? (I didn't find it in the standard...)
>


it's implementation defined.
e.g

on 32-bit machine, sizeof(ptr) may be 32
and 64-bit machine it returns 64.

Road
--
C FAQ: http://c-faq.com/
  Réponse avec citation
Vieux 22/10/2007, 09h34   #4
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

William Xu wrote:

> I test it with char, short, int, long, float, double and struct
> pointers, all return 4 bytes. Does the standard say anything about
> sizeof a pointer? (I didn't find it in the standard...)


No. It says that a pointer of type void * must be capable of holding any
pointer value, but nothing specific is said about the sizes of
pointers. Pointers to different types could be of the same or different
sizes.

  Réponse avec citation
Vieux 22/10/2007, 09h57   #5
William Xu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

Road Tang <roadtang@gmail.com> writes:

> it's implementation defined.
> e.g
>
> on 32-bit machine, sizeof(ptr) may be 32


Then, is it safe to say that sizeof(ptr) <= 32 ?

> and 64-bit machine it returns 64.


Similarly, sizeof(ptr) <= 64 ?

--
William

http://williamxu.net9.org
  Réponse avec citation
Vieux 22/10/2007, 10h01   #6
MisterE
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?



>I test it with char, short, int, long, float, double and struct
> pointers, all return 4 bytes. Does the standard say anything about
> sizeof a pointer? (I didn't find it in the standard...)
>
> --
> William



Pointers of any type will have the same size because they hold the address
value of memory that they point to, thus they always hold the same type of
data. On standard PC's this is a 32bit address (4 bytes), but it depends on
the system what the size is.


  Réponse avec citation
Vieux 22/10/2007, 10h19   #7
Road Tang
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

William Xu wrote:

> Road Tang <roadtang@gmail.com> writes:
>
>> it's implementation defined.
>> e.g
>>
>> on 32-bit machine, sizeof(ptr) may be 32

>
> Then, is it safe to say that sizeof(ptr) <= 32 ?


No, I did't find the Standard have such rules about it.
I just write that number for example.

>> and 64-bit machine it returns 64.

>
> Similarly, sizeof(ptr) <= 64 ?
>

You like this apply? but I'm still not sure.


--
C FAQ: http://c-faq.com/
  Réponse avec citation
Vieux 22/10/2007, 10h29   #8
Martien Verbruggen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

On Mon, 22 Oct 2007 14:04:59 +0530,
santosh <santosh.k83@gmail.com> wrote:
> William Xu wrote:
>
>> I test it with char, short, int, long, float, double and struct
>> pointers, all return 4 bytes. Does the standard say anything about
>> sizeof a pointer? (I didn't find it in the standard...)

>
> No. It says that a pointer of type void * must be capable of holding any
> pointer value, but nothing specific is said about the sizes of
> pointers.


Except, possibly, pointers to function.

Martien
--
|
Martien Verbruggen | You can't have everything, where would you
| put it?
|
  Réponse avec citation
Vieux 22/10/2007, 11h04   #9
Mark Bluemel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

William Xu wrote:
> Road Tang <roadtang@gmail.com> writes:
>
>> it's implementation defined.
>> e.g
>>
>> on 32-bit machine, sizeof(ptr) may be 32

>
> Then, is it safe to say that sizeof(ptr) <= 32 ?


No. It is likely though, as you have just asserted that a pointer is
less than 32 _bytes_ long...


If you meant 32-bits, definitely no. Prime for example had an 32-bit
architecture with 48-bit pointers ...
  Réponse avec citation
Vieux 22/10/2007, 11h06   #10
Mark Bluemel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

MisterE wrote:
>> I test it with char, short, int, long, float, double and struct
>> pointers, all return 4 bytes. Does the standard say anything about
>> sizeof a pointer? (I didn't find it in the standard...)
>>
>> --
>> William

>
>
> Pointers of any type will have the same size because they hold the address
> value of memory that they point to, thus they always hold the same type of
> data.


The standard doesnt state this. and it doesn't have to be the case.
Prime, for example, had 32-bit pointers for integer, short, long etc but
48-bit pointers for char, if I remember correctly.
  Réponse avec citation
Vieux 22/10/2007, 11h10   #11
Martin Wells
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

William:

> I test it with char, short, int, long, float, double and struct
> pointers, all return 4 bytes. Does the standard say anything about
> sizeof a pointer? (I didn't find it in the standard...)



The only thing required by the Standard is that sizeof(ptr) >= 1.
There's no need to know in portable programming... just use
sizeof(type*) where applicable.

Martin

  Réponse avec citation
Vieux 22/10/2007, 11h40   #12
William Xu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

Mark Bluemel <mark_bluemel@pobox.com> writes:

> No. It is likely though, as you have just asserted that a pointer is
> less than 32 _bytes_ long...
>
>
> If you meant 32-bits, definitely no. Prime for example had an 32-bit
> architecture with 48-bit pointers ...


Ah, yes, i meant 32-bits...;-)

I get it now. Thanks for all the .

--
William

http://williamxu.net9.org
  Réponse avec citation
Vieux 22/10/2007, 12h51   #13
Barry Schwarz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

On Oct 22, 1:34 am, Road Tang <roadt...@gmail.com> wrote:
> William Xu wrote:
> > I test it with char, short, int, long, float, double and struct
> > pointers, all return 4 bytes. Does the standard say anything about
> > sizeof a pointer? (I didn't find it in the standard...)

>
> it's implementation defined.
> e.g
>
> on 32-bit machine, sizeof(ptr) may be 32
> and 64-bit machine it returns 64.


I don't think this is true for any 32-bit 0r 64-bit system that has
ever exsited.

While sizeof(some ptr) may indeed be 32 or 64 (even though it seems
unlikely), it has nothing to do with whether the system is a 32 bit or
64 bit system. (I wonder if there is enough memory in the entire
world to fully populate a system with a 64 BYTE pointer.)

  Réponse avec citation
Vieux 22/10/2007, 12h54   #14
Barry Schwarz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

On Oct 22, 2:01 am, "MisterE" <Mist...@nimga.com> wrote:
> >I test it with char, short, int, long, float, double and struct
> > pointers, all return 4 bytes. Does the standard say anything about
> > sizeof a pointer? (I didn't find it in the standard...)

>
> > --
> > William

>
> Pointers of any type will have the same size because they hold the address
> value of memory that they point to, thus they always hold the same type of
> data. On standard PC's this is a 32bit address (4 bytes), but it depends on
> the system what the size is.


While this may be true in your experience, the C standard imposes no
such restriction. Furthermore, there are systems for which it is not
true.

  Réponse avec citation
Vieux 22/10/2007, 13h02   #15
Richard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

Barry Schwarz <schwarz45@yahoo.com> writes:

> On Oct 22, 2:01 am, "MisterE" <Mist...@nimga.com> wrote:
>> >I test it with char, short, int, long, float, double and struct
>> > pointers, all return 4 bytes. Does the standard say anything about
>> > sizeof a pointer? (I didn't find it in the standard...)

>>
>> > --
>> > William

>>
>> Pointers of any type will have the same size because they hold the address
>> value of memory that they point to, thus they always hold the same type of
>> data. On standard PC's this is a 32bit address (4 bytes), but it depends on
>> the system what the size is.

>
> While this may be true in your experience, the C standard imposes no
> such restriction. Furthermore, there are systems for which it is not
> true.


Could you explain how malloc works for such systems please.
  Réponse avec citation
Vieux 22/10/2007, 13h32   #16
Chris Dollin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

Richard wrote:

> Barry Schwarz <schwarz45@yahoo.com> writes:
>>> Pointers of any type will have the same size because they hold the address
>>> value of memory that they point to, thus they always hold the same type of
>>> data. On standard PC's this is a 32bit address (4 bytes), but it depends on
>>> the system what the size is.

>>
>> While this may be true in your experience, the C standard imposes no
>> such restriction. Furthermore, there are systems for which it is not
>> true.

>
> Could you explain how malloc works for such systems please.


It returns a `void*`.

(If that's not enough of an answer, you'll have to unpack your question.)

--
Chris "`essen`, vb, 'to buy many games'" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

  Réponse avec citation
Vieux 22/10/2007, 14h01   #17
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

Richard wrote:

> Barry Schwarz <schwarz45@yahoo.com> writes:
>
>> On Oct 22, 2:01 am, "MisterE" <Mist...@nimga.com> wrote:
>>> >I test it with char, short, int, long, float, double and struct
>>> > pointers, all return 4 bytes. Does the standard say anything about
>>> > sizeof a pointer? (I didn't find it in the standard...)
>>>
>>> > --
>>> > William
>>>
>>> Pointers of any type will have the same size because they hold the
>>> address value of memory that they point to, thus they always hold
>>> the same type of data. On standard PC's this is a 32bit address (4
>>> bytes), but it depends on the system what the size is.

>>
>> While this may be true in your experience, the C standard imposes no
>> such restriction. Furthermore, there are systems for which it is not
>> true.

>
> Could you explain how malloc works for such systems please.


Because void * is guaranteed to be able to store values of all object
pointer types.

  Réponse avec citation
Vieux 22/10/2007, 14h10   #18
Nick Keighley
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

On 22 Oct, 12:51, Barry Schwarz <schwar...@yahoo.com> wrote:

<snip>

> (I wonder if there is enough memory in the entire
> world to fully populate a system with a 64 BYTE pointer.)


64 x 8 = 512 bits

a 512 bit address can address 2^512 locations

2^512 ~ 10^154

the number of atoms in the observable universe is estimated to be
~10^80

so no you couldn't populate your system if you confine yourself to
the
observable universe


--
Nick Keighley


  Réponse avec citation
Vieux 22/10/2007, 14h11   #19
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

William Xu wrote:
> I test it with char, short, int, long, float, double and struct
> pointers, all return 4 bytes. Does the standard say anything about
> sizeof a pointer? (I didn't find it in the standard...)


(Some of these points have been made in other responses,
but a few don't seem to have appeared yet.)

The Standard guarantees that

- Any kind of data pointer can be converted to `void*'
and back without changing its value (this implies that
no data pointer has more value bits than `void*')

- Any function pointer can be converted to any other type
of function pointer and back without changing value
(hence, all function pointers have the same number of
value bits)

- The value 0 (the "null pointer constant") is valid for
all pointer types, both data pointers and function pointers

- `void*' and pointers to all three kinds of `char'
have the same size and representation (whatever it is)

- All pointers to all struct types have the same size
and representation (whatever it is)

- All pointers to all union types have the same size
and representation (whatever it is)

- All non-bit-field objects (hence, all pointers) occupy
at least one byte each

- A qualified type (`const', etc.) has the same size and
representation as its unqualified cousin

The Standard does *not* guarantee that all pointers have
the same representation or even the same size, and does *not*
guarantee that function pointers can be converted to data
pointers or vice versa.

--
Eric Sosman
esosman@ieee-dot-org.invalid
  Réponse avec citation
Vieux 22/10/2007, 14h34   #20
Mark Bluemel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

santosh wrote:
> Richard wrote:
>
>> Barry Schwarz <schwarz45@yahoo.com> writes:
>>
>>> On Oct 22, 2:01 am, "MisterE" <Mist...@nimga.com> wrote:


>>>> Pointers of any type will have the same size because they hold the
>>>> address value of memory that they point to, thus they always hold
>>>> the same type of data. On standard PC's this is a 32bit address (4
>>>> bytes), but it depends on the system what the size is.


>>> While this may be true in your experience, the C standard imposes no
>>> such restriction. Furthermore, there are systems for which it is not
>>> true.


>> Could you explain how malloc works for such systems please.


> Because void * is guaranteed to be able to store values of all object
> pointer types.


People do seem to get very worried when they find that some of what they
thought they knew is in fact false, don't they?

The point here is that all the standard requires is that

a) there must be a pointer representation (void *) which is capable of
pointing to any category of data. malloc() returns its result in such a
pointer.

b) there must be a valid way of converting from void * to any other
pointer type.

c) malloc() must ensure that the memory it allocates is suitably aligned
for any data type.

Nothing requires that the same representation should be used for both
the generic pointer type (void *) and all other (data) pointer types.

--
Mark "And ASCII isn't required by the standard either" Bluemel
  Réponse avec citation
Vieux 22/10/2007, 14h58   #21
Richard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

santosh <santosh.k83@gmail.com> writes:

> Richard wrote:
>
>> Barry Schwarz <schwarz45@yahoo.com> writes:
>>
>>> On Oct 22, 2:01 am, "MisterE" <Mist...@nimga.com> wrote:
>>>> >I test it with char, short, int, long, float, double and struct
>>>> > pointers, all return 4 bytes. Does the standard say anything about
>>>> > sizeof a pointer? (I didn't find it in the standard...)
>>>>
>>>> > --
>>>> > William
>>>>
>>>> Pointers of any type will have the same size because they hold the
>>>> address value of memory that they point to, thus they always hold
>>>> the same type of data. On standard PC's this is a 32bit address (4
>>>> bytes), but it depends on the system what the size is.
>>>
>>> While this may be true in your experience, the C standard imposes no
>>> such restriction. Furthermore, there are systems for which it is not
>>> true.

>>
>> Could you explain how malloc works for such systems please.

>
> Because void * is guaranteed to be able to store values of all object
> pointer types.


But we are talking about values of "special memory blocks" where the
pointers are different. How does malloc know which "memory type" to
allocate?

  Réponse avec citation
Vieux 22/10/2007, 15h25   #22
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

Richard wrote:

> santosh <santosh.k83@gmail.com> writes:
>
>> Richard wrote:
>>
>>> Barry Schwarz <schwarz45@yahoo.com> writes:
>>>
>>>> On Oct 22, 2:01 am, "MisterE" <Mist...@nimga.com> wrote:
>>>>> >I test it with char, short, int, long, float, double and struct
>>>>> > pointers, all return 4 bytes. Does the standard say anything
>>>>> > about sizeof a pointer? (I didn't find it in the standard...)
>>>>>
>>>>> > --
>>>>> > William
>>>>>
>>>>> Pointers of any type will have the same size because they hold the
>>>>> address value of memory that they point to, thus they always hold
>>>>> the same type of data. On standard PC's this is a 32bit address (4
>>>>> bytes), but it depends on the system what the size is.
>>>>
>>>> While this may be true in your experience, the C standard imposes
>>>> no
>>>> such restriction. Furthermore, there are systems for which it is
>>>> not true.
>>>
>>> Could you explain how malloc works for such systems please.

>>
>> Because void * is guaranteed to be able to store values of all object
>> pointer types.

>
> But we are talking about values of "special memory blocks" where the
> pointers are different. How does malloc know which "memory type" to
> allocate?


It doesn't. It allocates memory suitable for all types. After the
conversion to an appropriate pointer type, the memory block is accessed
as an array of that type.

  Réponse avec citation
Vieux 22/10/2007, 15h31   #23
Richard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

santosh <santosh.k83@gmail.com> writes:

> Richard wrote:
>
>> santosh <santosh.k83@gmail.com> writes:
>>
>>> Richard wrote:
>>>
>>>> Barry Schwarz <schwarz45@yahoo.com> writes:
>>>>
>>>>> On Oct 22, 2:01 am, "MisterE" <Mist...@nimga.com> wrote:
>>>>>> >I test it with char, short, int, long, float, double and struct
>>>>>> > pointers, all return 4 bytes. Does the standard say anything
>>>>>> > about sizeof a pointer? (I didn't find it in the standard...)
>>>>>>
>>>>>> > --
>>>>>> > William
>>>>>>
>>>>>> Pointers of any type will have the same size because they hold the
>>>>>> address value of memory that they point to, thus they always hold
>>>>>> the same type of data. On standard PC's this is a 32bit address (4
>>>>>> bytes), but it depends on the system what the size is.
>>>>>
>>>>> While this may be true in your experience, the C standard imposes
>>>>> no
>>>>> such restriction. Furthermore, there are systems for which it is
>>>>> not true.
>>>>
>>>> Could you explain how malloc works for such systems please.
>>>
>>> Because void * is guaranteed to be able to store values of all object
>>> pointer types.

>>
>> But we are talking about values of "special memory blocks" where the
>> pointers are different. How does malloc know which "memory type" to
>> allocate?

>
> It doesn't. It allocates memory suitable for all types. After the
> conversion to an appropriate pointer type, the memory block is accessed
> as an array of that type.


How can you allocate memory for "all types"? Where is this "special
block of memory with extra bits for padding" case?
  Réponse avec citation
Vieux 22/10/2007, 16h07   #24
Richard Bos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

Richard <rgrdev@gmail.com> wrote:

> santosh <santosh.k83@gmail.com> writes:
>
> > Richard wrote:
> >
> >> Barry Schwarz <schwarz45@yahoo.com> writes:
> >>
> >>> On Oct 22, 2:01 am, "MisterE" <Mist...@nimga.com> wrote:
> >>>> >I test it with char, short, int, long, float, double and struct
> >>>> > pointers, all return 4 bytes. Does the standard say anything about
> >>>> > sizeof a pointer? (I didn't find it in the standard...)
> >>>>
> >>>> Pointers of any type will have the same size because they hold the
> >>>> address value of memory that they point to, thus they always hold
> >>>> the same type of data. On standard PC's this is a 32bit address (4
> >>>> bytes), but it depends on the system what the size is.
> >>>
> >>> While this may be true in your experience, the C standard imposes no
> >>> such restriction. Furthermore, there are systems for which it is not
> >>> true.
> >>
> >> Could you explain how malloc works for such systems please.

> >
> > Because void * is guaranteed to be able to store values of all object
> > pointer types.

>
> But we are talking about values of "special memory blocks" where the
> pointers are different.


No, we're not. We weren't the last time you spouted this nonsensical
misinterpretation of C's pointer system, and we still aren't. Nor will
we next time you will stubbornly refuse to learn.

Richard
  Réponse avec citation
Vieux 22/10/2007, 16h31   #25
Richard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

rlb@hoekstra-uitgeverij.nl (Richard Bos) writes:

> Richard <rgrdev@gmail.com> wrote:
>
>> santosh <santosh.k83@gmail.com> writes:
>>
>> > Richard wrote:
>> >
>> >> Barry Schwarz <schwarz45@yahoo.com> writes:
>> >>
>> >>> On Oct 22, 2:01 am, "MisterE" <Mist...@nimga.com> wrote:
>> >>>> >I test it with char, short, int, long, float, double and struct
>> >>>> > pointers, all return 4 bytes. Does the standard say anything about
>> >>>> > sizeof a pointer? (I didn't find it in the standard...)
>> >>>>
>> >>>> Pointers of any type will have the same size because they hold the
>> >>>> address value of memory that they point to, thus they always hold
>> >>>> the same type of data. On standard PC's this is a 32bit address (4
>> >>>> bytes), but it depends on the system what the size is.
>> >>>
>> >>> While this may be true in your experience, the C standard imposes no
>> >>> such restriction. Furthermore, there are systems for which it is not
>> >>> true.
>> >>
>> >> Could you explain how malloc works for such systems please.
>> >
>> > Because void * is guaranteed to be able to store values of all object
>> > pointer types.

>>
>> But we are talking about values of "special memory blocks" where the
>> pointers are different.

>
> No, we're not. We weren't the last time you spouted this nonsensical
> misinterpretation of C's pointer system, and we still aren't. Nor will
> we next time you will stubbornly refuse to learn.
>
> Richard


"there are systems for which it is not true"

I ask again : how does malloc know what kind of "special" memory to
allocate? Special memory might have these special pointers.

And kindly don't accuse me of refusing to learn. If you want to bully
someone try someone else.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h30.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,41018 seconds with 33 queries