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, 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
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 11h08.


Édité par : vBulletin® version 3.7.2
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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,23315 seconds with 16 queries