Real: Ptr Casting syntax?? 
Author Message
 Real: Ptr Casting syntax??

!! Sorry to send this twice first attempt was an error.

Why does this code not function? The data variable contains the proper
value so half the code is correct, but it is not "received" by i. hmmm....

i, PtrInt: integer;
data := ptr;

PtrInt := 1;

i := integer(data^);

result: i = 0.

Thanks all, cheers.



Sat, 01 Oct 2005 00:26:48 GMT  
 Real: Ptr Casting syntax??
[[ This message was both posted and mailed: see
   the "To," "Cc," and "Newsgroups" headers for details. ]]


Quote:

> !! Sorry to send this twice first attempt was an error.

From a strictly correct viewpoint, it is an error to post either to
comp.lang.Pascal.ansi-iso since the newsgroup's focus is on ISO
standard Pascal and type casting isn't a feature of either ISO Pascal
standard.

[Followups set to comp.lang.pascal.mac, comp.lang.pascal.misc]

Quote:
> Why does this code not function? The data variable contains the proper
> value so half the code is correct, but it is not "received" by i. hmmm....

> i, PtrInt: integer;
> data := ptr;

This looks like a posting typographical error.  To declare the variable
data to have type ptr, the declaration should be:

data: ptr;

Quote:
> PtrInt := 1;

> i := integer(data^);

> result: i = 0.

Given your other postings to comp.lang.pascal.mac and
comp.lang.pascal.misc, it looks like you're using CodeWarior Pascal on
a Macintosh so I'll focus my reply on that dialect.

Since the ptr type is being used, I suspect the code is also using the
MacTypes unit from Apple's Universal Interfaces.  If so, the ptr type
is defined to be a pointer to an 8-bit (one byte sized memory chunck)
signed integer; whereas, an integer type is a 16-bit (two byte sized
memory chunck).

Since the Macintosh is a big endian, twos complement machine, an
integer is stored in ascending byte memory locations with the scheme of
high/most significant byte followed by low/least significant byte.  So
the integer value 1 is stored as $00 for the high byte and $01 for the
low byte. ($ signifies hexadecimal number base.)

Hence, data^ accesses one byte from the PtrInt memory address which is
the high byte containing value $00.  Then it gets type cast to a two
byte integer $0000 and is subsequently stored in i.  Thus the result of
i = 0 is exactly what you have directed the compiler to produce as the
"correct" result even though it isn't quite the result you were hoping
to get.

A good example of the perils of type casting.  It bypasses all Pascal
sanity checks and built-in type and assignment conversions and gives
you exactly what you asked for.  If you make a mistake in the asking,
you get the answer you asked for albeit not the answer you intended to
get.

As it turns out, there is no need for type casting in this example.
CodeWarrior Pascal uses integer subrange type assignment
compatibilities rules in assignment statements involving different
sized integer types.  As long as the value of type T1 is in the range
of values representable by type T2 then the assignment T2 := T1 is
legal and any necessary size conversion is handled correctly and
automatically.

Thus:

uses MacTypes;

var
  i, PtrInt: integer;
   data: ptr;
begin
  PtrInt := 1;

  i := data^;
end.

will yield the result of i = 1.

If you do insist on using type casting, the correct type casting to use
is:

uses MacTypes;

var
  i, PtrInt: integer;
   data: ptr;
begin
  PtrInt := 1;

  i := IntegerPtr(data)^;
end.

Note the diffence is in cohersing the type of the pointer from a
pointer to a byte to a pointer to two bytes.  Thus when the pointer is
dereferenced, two bytes are accessed yielding the correct desired
result.

Note for other readers:  The above code snippets are inherently
non-portable.  The code relies upon Macintosh specific machine
characteristics, non-standard Pascal dialect extensions, and OS vendor
(i.e., Apple) dictated and supplied interfaces.

Gale Paeper



Sat, 01 Oct 2005 06:25:42 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Ptr Casting syntax??

2. 4DA2DE39 cmp dword ptr [edx],$00

3. ptr to string

4. ptr to array

5. Prot mode ptr need a touch?

6. Phy addr from prot mode ptr

7. function Sin(X: Real): Real;

8. longint to real/real to longint

9. tspa357c.zip Turbo Pascal 7.0 real mode units for (real:-) programmers

10. tspa3570.zip Turbo Pascal 7.0 real mode units for (real:-) programmers

11. tspa3570.zip Turbo Pascal 7.0 real mode units for (real:-) programmers

12. type casting (objects)

 

 
Powered by phpBB® Forum Software