Thread: Need help with far pointers

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    India
    Posts
    32

    Need help with far pointers

    Hi I am trying to understand far pointers in C.

    When I compile the following source code, I get compilation errors.

    Code:
    #include<stdio.h>
    #include<conio.h>
    int main(void)
    {
        char far *a = 0x00000120;
        char far *b = 0x00100020;
        char far *c = 0x00120000;
        
        if(a == b)
             printf("\nHello");
        if(a == c)
             printf("\nHi");
        if(b == c)
             printf("\nHello Hi");
        if(a > b && a > c && b > c)
             printf("\nBye");
        getch();
        return 0;
    }
    Am I doing anything wrong? Any help would be appreciated.
    Thanks.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Far pointers were a extension supported by some particular compilers that have since been superseded. Those compilers targeted 16 bit systems such as MS-DOS (which predated early versions of Microsoft windows). Basically they allowed usage of a larger address space (into "extended" or "high" memory) that was implemented using installed drivers.

    Simply remove the far keyword from lines 5,6, and 7 to stop the compiler complaining about usage of "far".

    After doing that, you will probably still get warnings and errors from the compiler though. Converting a hex value (say 0x00000120) to a pointer is potentially an invalid conversion.

    Your code does not dereference those pointers, and should not - because the result of doing that is undefined behaviour (as per the meaning of the word "undefined" in the C standard). A compiler or program that exhibits undefined behaviour is allowed to do anything, whether good or bad. Common symptoms of undefined behaviour include program crashes, but other good or bad behaviours can and do occur in practice.

    It is also not a good idea to compare pointers using inequalities (>, etc). Comparing for equality versus inequality is okay. But operators like > (greater than), < (less than), <= (less than or equal to) when applied to pointers also give undefined behaviour unless both pointers contain the address of data in the same array. Assigning arbitrary hex values to two pointers does not usually give two pointers to elements within the same array.

    Also, <conio.h> and the getch() function are non-standard. They are also extensions only supported by certain compilers (coincidentally, those compilers happen to support the far extension).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    India
    Posts
    32
    Hi grumpy, thanks for replying and clearing the doubt.

    I have removed the far keyword and the program looks OK but does not print any output. I am wondering why this is so. Because any one of the IF conditions should definitely be satisfied.

    Also could you tell me after removing the FAR keyword, how would these hex addresses be resolved to point to memory locations? Would it be normalized like the way HUGE pointers operate or is there anything else?

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > Because any one of the IF conditions should definitely be satisfied.
    No, substitute in the values and it's obvious that nothing should be printed:
    Code:
        if(0x00000120 == 0x00100020)
             printf("\nHello");
        if(0x00000120 == 0x00120000)
             printf("\nHi");
        if(0x00100020 == 0x00120000)
             printf("\nHello Hi");
        if(0x00000120 > 0x00100020 && 
           0x00000120 > 0x00120000 && 
           0x00100020 > 0x00120000)
             printf("\nBye");
    > Would it be normalized like the way HUGE pointers operate or is there anything else?
    No idea what HUGE is, it's certainly not standard C. Either way, when you need to write to a certain address in memory and you cannot use mmap, you can use this syntax:
    Code:
    char *a = (char *)0x00000120;

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    None of your if conditions will be true. Hence no output.

    If we ignore the fact that the individual tests in "a > b && a > c && b > c" actually yield undefined behaviour as I mentioned earlier (which means anything is allowed to happen), we can fall back on comparing the values of a, b, and c. Looking at their hex values, a is not greater than b, a is not greater than c, and b is not greater than c. So that test would yield false.

    As to conversion of hex values into memory addresses, basically the hex value becomes the value of the pointer, after an appropriate type conversion. The mechanism of that type conversion (eg normalisation of huge pointers, if any) compiler and host system specific. Regardless of how the conversion is done, dereferencing those pointers still gives undefined behaviour.

    If you really want to understand far pointers, huge pointers, near pointers you need to read the documentation for your compiler. None of them are standard features of C - they are extensions specific to particular compilers and host systems.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    India
    Posts
    32
    Hi memcpy, thanks for that quick reply. I understood what was wrong. I did a quick hex to decimal conversion and realized that none of the IF conditions were being satisfied.

    Thanks again

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    India
    Posts
    32
    Thanks grumpy, I understood that explanation.

    Cheers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  2. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  3. Pointers to pointers in linked lists
    By G4B3 in forum C++ Programming
    Replies: 2
    Last Post: 07-23-2008, 03:54 AM
  4. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM
  5. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM