Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 343
  • Last Modified:

far pointer

I have a code which is as follows

typedef struct {
    int offset
    int handle
}objectptr;

typedef objectptr far   *objptr;

I would like to know purpose of far keyword here?

If necessary i can attach code snippet

Regards
Sham




 

0
sham_ibmgs
Asked:
sham_ibmgs
1 Solution
 
Rob SiklosCommented:
0
 
sunnycoderCommented:
far pointers were used in legacy code to access memory locations outside the current segment (16 bit segment + 16 bit offset = 32 bit pointer while pointers at that time used to be 16 bits - the days of 16 bit systems and DOS). We used to have near far and huge pointers.
I would imagine that code would be using a farmalloc to allocate memory for objptr.
Here are details of architecture and segments that would give you the rationale
http://www.experts-exchange.com/Programming/Languages/CPP/Q_20106123.html
http://answers.yahoo.com/question/index?qid=20070611213330AAewopS
0
 
itsmeandnobodyelseCommented:
The 'far' keyword defaults to nothing in any 32-bit development. You simply should omit it beside you would develop 16bit applications.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
sham_ibmgsAuthor Commented:
@itsmeand: How 'far'  keyword is relevant to 16 bit applications?

 
0
 
sham_ibmgsAuthor Commented:
@itmeand: How the following structure[offset/handle] is used in 16 bit and 32 bit scenario?
typedef struct {
    int offset
    int handle
}objectptr;

typedef objectptr far   *objptr;

Regards
Sham
0
 
sham_ibmgsAuthor Commented:
@itmeaned: I have read the comment in your attached link, i could not understand the following point:

The Segment part 'addresses' 16 byte blocks, the Offset specifies an offset from the start of this 16 byte block (note this offset can be from 0 to 64K, leading to the uncomfortable situation where two different Segment/Offset pairs adress the same memory).

Can u explain, how pairs address to same memory?

Regards
Sham
0
 
sunnycoderCommented:
I guess you are mixing my comments with those of itsmeandnobodyelse but I would try to give some clarifications anyway

16 bit application - 16 bit pointer ... you cannot address more than 64 K. Address space however is 20 bits, so you cannot address entire memory using 16 bit pointers .... This is where far pointers enter the scene. Given the segmented memory model, capability to address memory in another segment allows you to address entire memory - 16 bit segment and 16 bit offset

far keyword does not affect how the struct would be used. It simply tells that memory is located in a different segment. Members would still be accessed using -> operator

Now that you have 32 (16+16) bit addressing capability and only 20 bit address space, an overlap is possible if your segments are not 64K in size.
e.g.
Seg1 0-16K
Seg2 16K-32K
A far pointer to Seg1 and offset 16K+1 is referencing the same memory as a far pointer to Seg2 and offset1

Cheers!
sunnycoder
0
 
sham_ibmgsAuthor Commented:
@sunny: In your latest post, i did not understand this point "Now that you have 32 (16+16) bit addressing capability and only 20 bit address space, an overlap is possible if your segments are not 64K in size" what do u mean (16+16) here?

Anyways, i would like to know another point: In 16 bit architecture[ex: 8086], segmented memory model is used, where each segment is of size 2^16 bytes(64K) and 2^4(16) different segments. What about 32 bit architecture, does it have segmented memory model or linear(consecutive) address model?

Regards
Sham
0
 
sunnycoderCommented:
The architecture that was reference in the overlap post had 20 bit addresses ... so 2^20 memory locations to address. A 32 bit far pointer lets you address 2^32 memory locations.
16+16 means 16 bits for offset and 16 for identifying the segment
32 bit architecture has segments like code segment, data segment but these are constructed based on needs of a program rather than being dictated by the physical limitations. These segments have no relation with the legacy memory models. You dont need far and huge pointers in this case.
0
 
sham_ibmgsAuthor Commented:
Following code is currently running in 32 bit processors/ Windows, am unable to understand, why far pointers and getdataaddres() is used? Cant we use simple pointers[int *] rather than POBJPTR as shown here?
Please help me in understanding:
1)  the reason for using type objectptr rather than simple int
2) Purpose of using far pointer

typedef struct {
    int offset
    int handle
}objectptr;
typedef objectptr far   *POBJPTR;

ERROR_CODE FAR  func (phWnd, pFileName, pDummy)
POBJPTR     phWnd;
POBJPTR     pFileName;
POBJPTR     pDummy;
   {
   char        szFileName [PATHNAME_LENGTH + 1];
   POBJPTR     pList;
   POBJPTR     pSize;
  SetFunctionName (TLBSave);
 /* save database file name */
   strcpy (szFileName, GRFGetDataAddress (pFileName));
.
.
}


VOID FAR* EXPORT GRFGetDataAddress (pObject)
POBJPTR     pObject;
   {
   LPCOPIEDHEADER lpMemBlock;
   UINT           uHandle;
   SetFunctionName (GRFGetDataAddress);
  /* check object pointer (and set variable uHandle) */
   CHECKOBJPTRFORADDRESS (pObject, uHandle, NULL);
  GETREALADDRESS (pObject, lpMemBlock, FALSE, NULL);
/* set pointer in return buffer */
   return ((VOID FAR*) ((LPSTR) lpMemBlock + sizeof (OBJECTHDR)));
}

Regards
Sham


0
 
sunnycoderCommented:
This looks like very old code ... even the syntax used is old. You cant replace objectptr far * with an int * but you can do away with the far keyword and use objectptr * only.
Far pointers are not required in this environment ... However, unless there is a compelling reason to do so, I would not recommend editing working code.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now