Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > different pointer sizes and representations

Reply
Thread Tools

different pointer sizes and representations

 
 
Luca Forlizzi
Guest
Posts: n/a
 
      11-22-2010
Hello,

it is often remarked that pointer to objects and pointer to functions
can have different representations and also different sizes. Are these
the only possible differences or does the standard allows also
pointers to distinct object types (or to distinct function types) to
differ in representation and size (and alignment requirement, I
suppose) ?

Luca Forlizzi
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      11-22-2010
Luca Forlizzi <(E-Mail Removed)> writes:
> it is often remarked that pointer to objects and pointer to functions
> can have different representations and also different sizes. Are these
> the only possible differences or does the standard allows also
> pointers to distinct object types (or to distinct function types) to
> differ in representation and size (and alignment requirement, I
> suppose) ?


void* and pointers to character types have the same representation
and alignment requirements.

All pointers to struct types have the same representation and
alignment requirements. (Note that this refers to the alignment
of pointer objects, not the alignment of struct objects.)

All pointers to union types have the same representation and
alignment requirements.

(I don't *think* pointer-to-struct and pointer-to-union types
are required to have the same representation as each other, but
I'm not certain.)

There might be some guarantee regarding pointers to integer types
that differ only in signedness, but I'm not sure about that.

There are no other restrictions (beyond those that apply to scalar
types in general).

In most real-world implementations you're likely to encounter,
all pointers have the same representation. In rare cases,
pointer-to-function types might differ from pointer-to-object
types; typically they'd be bigger. In other rare cases, void*
and pointer-to-character types might differ from other object
pointer types. But an implementation could legally make double*
twice the size of int*.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Tim Rentsch
Guest
Posts: n/a
 
      11-23-2010
Keith Thompson <(E-Mail Removed)> writes:

> Luca Forlizzi <(E-Mail Removed)> writes:
>> it is often remarked that pointer to objects and pointer to functions
>> can have different representations and also different sizes. Are these
>> the only possible differences or does the standard allows also
>> pointers to distinct object types (or to distinct function types) to
>> differ in representation and size (and alignment requirement, I
>> suppose) ?

>
> void* and pointers to character types have the same representation
> and alignment requirements.
>
> All pointers to struct types have the same representation and
> alignment requirements. (Note that this refers to the alignment
> of pointer objects, not the alignment of struct objects.)
>
> All pointers to union types have the same representation and
> alignment requirements.
>
> (I don't *think* pointer-to-struct and pointer-to-union types
> are required to have the same representation as each other, but
> I'm not certain.)


They are not so required.

> There might be some guarantee regarding pointers to integer types
> that differ only in signedness, but I'm not sure about that.


Again there are not.

> There are no other restrictions (beyond those that apply to scalar
> types in general).


Except, pointers to compatible types have the same representation
and alignment requirements, and also qualified and non-qualified
versions of pointers to compatible types.
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      11-23-2010
On Mon, 22 Nov 2010 09:17:16 -0800, Keith Thompson wrote:

> In most real-world implementations you're likely to encounter, all
> pointers have the same representation. In rare cases, pointer-to-function
> types might differ from pointer-to-object types; typically they'd be
> bigger.


Or smaller. E.g. with DOS code, it was more common to have one code
segment and multiple data segments (so a function pointer was just an
offset but a data pointer was segment+offset) than the other way around.

Also, on single-chip microcontrollers, you can typically have data in RAM,
flash and/or EEPROM (with different code being required to access each
type), but code can only go in flash. Usually, you would tag the pointer
with a (non-standard) qualifier; occasionally you might use "unified"
pointers (with the type stored in the pointer), but this makes for bloated
code).

 
Reply With Quote
 
Morris Keesan
Guest
Posts: n/a
 
      11-23-2010
On Mon, 22 Nov 2010 09:35:57 -0500, Luca Forlizzi
<(E-Mail Removed)> wrote:

> it is often remarked that pointer to objects and pointer to functions
> can have different representations and also different sizes. Are these
> the only possible differences or does the standard allows also
> pointers to distinct object types (or to distinct function types) to
> differ in representation and size (and alignment requirement, I
> suppose) ?


Pointers to different object types can definitely differ from each other.
I've used systems where (char *)s differed in size and representation from
all other pointers.

I can't find anything in the standard which would prevent different
function pointer types from having different sizes, etc.
C99 6.3.2.3 paragraph 8 says, "A pointer to a function of one type may be
converted to a pointer to a function of another type and back again; the
result shall compare equal to the original pointer."


--
Morris Keesan -- (E-Mail Removed)
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      11-23-2010
"Morris Keesan" <(E-Mail Removed)> writes:

> On Mon, 22 Nov 2010 09:35:57 -0500, Luca Forlizzi
> <(E-Mail Removed)> wrote:
>
>> it is often remarked that pointer to objects and pointer to functions
>> can have different representations and also different sizes. Are these
>> the only possible differences or does the standard allows also
>> pointers to distinct object types (or to distinct function types) to
>> differ in representation and size (and alignment requirement, I
>> suppose) ?

>
> Pointers to different object types can definitely differ from each other.
> I've used systems where (char *)s differed in size and representation from
> all other pointers.
>
> I can't find anything in the standard which would prevent different
> function pointer types from having different sizes, etc.
> C99 6.3.2.3 paragraph 8 says, "A pointer to a function of one type may be
> converted to a pointer to a function of another type and back again; the
> result shall compare equal to the original pointer."


If two function types have the same return type, they
are both compatible with a function type having the
same return type but not specifying any parameter types
(eg, like 'int foo()'), and so pointers to these two types
must have the same size and representation, etc, since they
both must match the common pointer type (eg, (int (*)(int))
and (int (*)(float)) both must match (int (*)())).

Pointers to functions not having the same return type don't
have to be the same size etc.
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      11-23-2010
On Nov 23, 6:55*am, Tim Rentsch <(E-Mail Removed)> wrote:
> "Morris Keesan" <(E-Mail Removed)> writes:
>

Say we've got a chip with the following architecture: a 256 byte RAM
and a 65536 byte ROM, machine instructions accessing RAM through 8 bit
offsets that differ from the machine instruction to access ROM. What
dies that imply for pointer representation?


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-23-2010
On 11/22/2010 11:55 PM, Tim Rentsch wrote:
> "Morris Keesan"<(E-Mail Removed)> writes:
>
>> On Mon, 22 Nov 2010 09:35:57 -0500, Luca Forlizzi
>> <(E-Mail Removed)> wrote:
>>
>>> it is often remarked that pointer to objects and pointer to functions
>>> can have different representations and also different sizes. Are these
>>> the only possible differences or does the standard allows also
>>> pointers to distinct object types (or to distinct function types) to
>>> differ in representation and size (and alignment requirement, I
>>> suppose) ?

>>
>> Pointers to different object types can definitely differ from each other.
>> I've used systems where (char *)s differed in size and representation from
>> all other pointers.
>>
>> I can't find anything in the standard which would prevent different
>> function pointer types from having different sizes, etc.
>> C99 6.3.2.3 paragraph 8 says, "A pointer to a function of one type may be
>> converted to a pointer to a function of another type and back again; the
>> result shall compare equal to the original pointer."

>
> If two function types have the same return type, they
> are both compatible with a function type having the
> same return type but not specifying any parameter types
> (eg, like 'int foo()'), and so pointers to these two types
> must have the same size and representation, etc, since they
> both must match the common pointer type (eg, (int (*)(int))
> and (int (*)(float)) both must match (int (*)())).


Like Morris Keesan, I can't find Standard language saying that
the function pointers you mention are "compatible" in the sense that
the Standard defines the word. "May be converted" and "compatible"
aren't the same notion as I understand them; can you offer citations
to clarify?

Incidentally, note that an `(int (*)())' pointer and an
`(int (*)(float))' pointer cannot call the same target function;
one or the other is necessarily incorrect for the target.

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-23-2010
Malcolm McLean <(E-Mail Removed)> writes:
> On Nov 23, 6:55*am, Tim Rentsch <(E-Mail Removed)> wrote:
>> "Morris Keesan" <(E-Mail Removed)> writes:
>>

> Say we've got a chip with the following architecture: a 256 byte RAM
> and a 65536 byte ROM, machine instructions accessing RAM through 8 bit
> offsets that differ from the machine instruction to access ROM. What
> dies that imply for pointer representation?


Assuming functions are in ROM, it implies that function pointers
must be big enough to distinguish between any two functions in
the program.

If the implementation permitted no more than 255 functions, a
function pointer could be implemented as an 8-bit index into a table.

As for object pointers, I presume objects (at least constant ones)
can be stored in either RAM or ROM, so again, an object pointer
has to be big enough to distinguish between any two objects in
the program. For example, a function could have a "const char*"
parameter that points to an object that could be either in RAM
or in ROM, so you can't get away with 8-bit RAM-only pointers.
Using 8 bits for char* and 16 or more for const char* would be a
clever trick, but I don't think it would be conforming.

Then again, an implementation for such a small system is likely to
have non-conforming extensions to deal with the small RAM.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      11-23-2010
On 2010-11-23, Malcolm McLean <(E-Mail Removed)> wrote:
> Say we've got a chip with the following architecture: a 256 byte RAM
> and a 65536 byte ROM, machine instructions accessing RAM through 8 bit
> offsets that differ from the machine instruction to access ROM. What
> dies that imply for pointer representation?


That it is almost certainly non-conforming.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Win 7 changing font sizes without icon sizes? why? Computer Support 0 03-21-2010 11:32 AM
Re: Win 7 changing font sizes without icon sizes? why? Computer Support 0 03-21-2010 11:31 AM
The File Sizes of Pictures on my CDs Increased to Unreadable Sizes Marful Computer Support 11 03-08-2006 07:13 PM
Color Representations for Brown and Violet kvnsmnsn@hotmail.com Java 34 09-12-2005 09:51 PM
Any ideas on different file sizes showing of the same file on two different systems? Joe Computer Support 4 01-10-2005 01:05 AM



Advertisments