%p and different pointer representations
Mark H. Colburn
mark at jhereg.Jhereg.MN.ORG
Sun Feb 26 08:23:50 AEST 1989
In article <16112 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:
>Yes. Moreover, since sizeof(void *)==sizeof(char *)==2, when you
>print a pointer-to-function using %p and a (void *) cast (on that
>machine in that model), you will lose some information.
Wait a minute, Chris. The pANS says (ss 3.2.2.3, pp 37 Oct 31, 88):
"A pointer to void may be converted to or from a pointer to any
incomplete or other type. A pointer to any incomplete or object
type may be converted to a pointer to void and back again; the
result shall compare equal to the original pointer."
What you are saying would seem to contradict this.
Also, I don't beleive that there is any requirement that (void *) has to be
2 bytes long. In my opinion void should be defined to be at least as
large as the largest pointer available in the current compiler model.
Therefore, on the IBM-PC, sizeof (void *) should be 4 in every model except
for small or tiny. This can be checked using the following program:
#include <stdio.h>
int main()
{
printf("sizeof(char *) = %d\n", sizeof(char *));
printf("sizeof(void *) = %d\n", sizeof(void *));
printf("sizeof(int (*)()) = %d\n", sizeof(int (*)()));
return (0);
}
Using Turbo C, I got the following results:
+-------------------------------------------------------------------+
| Model | sizeof(char *) | sizeof(void *) | sizeof(int (*)()) |
+=============+================+================+===================|
| Huge | 4 | 4 | 4 |
+-------------+----------------+----------------+-------------------|
| Large | 4 | 4 | 4 |
+-------------+----------------+----------------+-------------------|
>> | Medium | 2 | 2 | 4 | <<
+-------------+----------------+----------------+-------------------|
| Small | 2 | 2 | 2 |
+-------------+----------------+----------------+-------------------|
| Tiny | 2 | 2 | 2 |
+-------------+----------------+----------------+-------------------+
If you take a look at the medium model, you will notice the annomally.
However, on the IBM-PC, the compiler may be able to tell the difference
between a code and data pointer by checking which segment it is in. I am
not sure exactly how Turbo C does this, although I will check. This
assumption would break down if the application was attempting to
dynamically load functions and execute them (which would tend to put them
into the data segment). Taking a function pointer in this case, may,
indeed, case problems. There are several applications that I know of that
do this.
I will point out, however, the the %p notation does appear to work in all
of the memory models correctly. I did not try dynamic function loading,
but I suppose that I could.
--
Mark H. Colburn "Look into a child's eye;
Minnetech Consulting, Inc. there's no hate and there's no lie;
mark at jhereg.mn.org there's no black and there's no white."
More information about the Comp.lang.c
mailing list