[comp.windows.ms.programmer] casting near & far pointers

rhoda@mercury.sybase.com (Rhoda Neimand) (05/23/91)

A unix person has just asked me a curious question.  I'm kind of puzzled
because I've never thought of doing this before.  To me, a far pointer
is a far pointer and a near pointer is a near pointer, period.

His questions:

Can you cast a far pointer to a near pointer?  My answer is that if you did,
you would lose its "farness", therefore it would no longer point to anything
unless you were sure that its segment address was currently in DS.  Is this
correct?

Can you cast a near pointer to a far pointer?  Not sure here.  If you did,
would the segment address be 0?  Does having a segment address of 0 mean
something special?  MAKEINTRESOURCE does something like this.  I am actually
confused about the purpose of MAKEINTRESOURCE.

I feel that either casting is not meaningful.  Is this correct?

Thanks
----------------------------------------------------------------------
Rhoda Neimand			                  [I speak for myself]
{sun,lll-tis,pyramid,pacbell}!sybase!rhoda -or- rhoda@sybase.com
"I didn't want to, Jim"		  Mr. Spock in _This Side Of Paradise_
----------------------------------------------------------------------

ebergman@isis.cs.du.edu (Eric Bergman-Terrell) (05/23/91)

A near pointer points to a location in the current segment.  A far pointer
can refer to an address in another segment.  Consequently, if you cast
a far pointer to a near pointer, the result will be bogus if the far pointer
refered to a different segment.

You can always go the other direction - you can cast near to far, near to
huge, far to huge, etc.  In fact, if you're using windows.h this is happening
implicitly - look at the ansi prototypes for the windows api routines.

And remember - segments are for worms!

Terrell

cadsi@ccad.uiowa.edu (CADSI) (05/23/91)

From article <12902@sybase.sybase.com>, by rhoda@mercury.sybase.com (Rhoda Neimand):
> A unix person has just asked me a curious question.  I'm kind of puzzled
> because I've never thought of doing this before.  To me, a far pointer
> is a far pointer and a near pointer is a near pointer, period.
> 
> His questions:
> 
> Can you cast a far pointer to a near pointer?  My answer is that if you did,
> you would lose its "farness", therefore it would no longer point to anything
> unless you were sure that its segment address was currently in DS.  Is this
> correct?

Yup.

> 
> Can you cast a near pointer to a far pointer?  Not sure here.  If you did,

Yup.

> would the segment address be 0?  Does having a segment address of 0 mean

Could be.  Just as long as FAR:NEAR point to the data.

> something special?  MAKEINTRESOURCE does something like this.  I am actually
> confused about the purpose of MAKEINTRESOURCE.

Nope.

> 
> I feel that either casting is not meaningful.  Is this correct?

Nope.  If you write in the medium model, pointers are near by default.
of you wanna use the Windows calls, they take LPSTR (char far *) pointers
in general.  Thus, When you do something like:

	char near *s;
	.
	.
	.
	SetDlgItemText(win, IDC_CTL, (LPSTR)s);

the compiler does the (char far *) cast by pushing the data segment
followed by the near char *s pointer.  Additionally, if you need to
use DLL's, far pointers are much easier to deal with.  Thus, a DLL
in large model can talk to an app in medium/small models just by
casting the near pointers to far and sending those to the DLL.
There are many other reasons but this just shows that casting near/far
is really used.


|----------------------------------------------------------------------------|
|Tom Hite					|  The views expressed by me |
|Manager, Product development			|  are mine, not necessarily |
|CADSI (Computer Aided Design Software Inc.	|  the views of CADSI.       |
|----------------------------------------------------------------------------|

Chris_Graham@f344.n632.z3.fidonet.org (Chris Graham) (05/30/91)

 * Original <23 May 91 00:16:27> was from Rhoda Neimand to All 

 > RN: From: rhoda@mercury.sybase.com (Rhoda Neimand)
 > RN: 
 > RN: A unix person has just asked me a curious question.  I'm kind
 > RN: of puzzled

Yes, unix people tend to do that to us. :-)

 > RN: because I've never thought of doing this before.  To me, a
 > RN: far pointer
 > RN: is a far pointer and a near pointer is a near pointer, period.

Good enough for me.

 > RN: 
 > RN: His questions:
 > RN: 
 > RN: Can you cast a far pointer to a near pointer?  My answer is
 > RN: that if you did,
 > RN: you would lose its "farness", therefore it would no longer
 > RN: point to anything
 > RN: unless you were sure that its segment address was currently
 > RN: in DS.  Is this
 > RN: correct?

Yup. You loose the segment address and are left only with the offset. This can 
be very dangerous indeed. Especially when doing lost of nasty segment work.

 > RN: 
 > RN: Can you cast a near pointer to a far pointer? 

No problem. This is safe.

 >RN: Not sure here.  If you did,
 > RN: would the segment address be 0?

I don't think so, in the assembly I've looked through,  DS is generally pushed.

 >RN: Does having a segment address of 0 mean
 > RN: something special?

Yes, this is where the 80x8x series of processors keep their interrupt vectors. 
The IBM PC BIOS uses segment 40h ( or 0:0400h ) as the BIOS data area. In any 
mode pointing to anywhere ( that is not owned by yourself ) is a very dangerous 
thing to do. Hence, under DOS bad code can be hard to find. In a protected mode 
environment, such as OS/2 or Windows in 286/386 modes, the memory protection 
will trap this and nail your program - before it does any damage.

 > RN:  MAKEINTRESOURCE does something like this.
 > RN:  I am actually confused about the purpose of MAKEINTRESOURCE.

Have a look in Petzolds Book:

First edition ( the grey one ), page 335-336
Second edition ( the white one ), page 313-314

It explains the special case of having a segment value of zero.

 > RN: 
 > RN: I feel that either casting is not meaningful.  Is this correct?

Yes they are. See above.

 > RN: 
 > RN: Thanks

No problems. Hope this helps.

 > RN: ----------------------------------------------------------------------
 > RN: Rhoda Neimand			                  [I speak for myself]
 > RN: {sun,lll-tis,pyramid,pacbell}!sybase!rhoda -or- rhoda@sybase.com
 > RN: "I didn't want to, Jim"		  Mr. Spock in _This Side Of Paradise_
 > RN: ----------------------------------------------------------------------

Note my tag line.