Solved

far pointer

Posted on 2008-06-10
11
335 Views
Last Modified: 2013-12-14
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
Comment
Question by:sham_ibmgs
11 Comments
 
LVL 9

Expert Comment

by:Rob Siklos
Comment Utility
0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
The 'far' keyword defaults to nothing in any 32-bit development. You simply should omit it beside you would develop 16bit applications.
0
 

Author Comment

by:sham_ibmgs
Comment Utility
@itsmeand: How 'far'  keyword is relevant to 16 bit applications?

 
0
 

Author Comment

by:sham_ibmgs
Comment Utility
@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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:sham_ibmgs
Comment Utility
@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
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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
 

Author Comment

by:sham_ibmgs
Comment Utility
@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
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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
 

Author Comment

by:sham_ibmgs
Comment Utility
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
 
LVL 45

Accepted Solution

by:
sunnycoder earned 100 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now