Discussion: sizeof(ptr) = ?
Afficher un message
Vieux 31/10/2007, 09h36   #60
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sizeof(ptr) = ?

santosh <santosh.k83@gmail.com> writes:
[...]
> The point was that different pointer types might differ in size. That
> doesn't mean that the memory pool from which malloc gets space has to
> be different for each differing pointer types.


Right. Pointers can differ; that doesn't mean that the memory they
point to can differ.

> It may be that the implementation, for example, decides to append extra
> information with pointers to floating types, but not to other types. In
> this case such pointers might conceivably be larger than others.


Right. Perhaps more realistically, on a system where machine pointers
point to words (where a word is several byte), an int* might be, say,
4 bytes, but a char* might be larger, with extra information added to
resolve which byte within the word it points to.

> Also an implementation might decide to allocate objects of a certain
> type in lower address spaces than others. In such a case, pointers to
> that type may be of smaller width, though the memory for the entire
> gamut is the same.


I don't believe a conforming implementation can do this, at least not
consistently. A pointer returned by malloc must be able to point to
any type of object; similarly, it's possible to declare a union of any
two or more object types. Restricting objects of a certain type to be
allocated only at low addresses might be possible, but other objects
would be dragged in as well.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
 
Page generated in 0,06180 seconds with 9 queries