Topic: Suggestion: Based pointers


Author: Allan_W@my-dejanews.com (Allan W)
Date: Wed, 15 May 2002 17:59:36 GMT
Raw View
I'd like to see the next version of Standard C++ include something
that I will call "Based Pointers." A based pointer is an offset
from some traditional pointer, which is declared seperately.

I haven't yet worked out all of the details, particularly with
respect to declaration or initialization of these pointers. But once
it's been initialized, it works like this:

    When the based pointer is used as an rvalue, it automatically
    converts to a standard pointer by adding the offset to the address
    in the traditional pointer. For instance,
        *ptr
    is equivalent to *(trad+off), where trad is the traditional pointer
    and off is the offset. Unlike normal pointer arithmetic, the
    addition would not take into account the sizeof the object being
    pointed to...

    When the based pointer is assigned, it automatically converts a
    standard address into a based address by subtracting the value
    in the traditional pointer (again, without respect to sizeof).
    In other words, modifying the based pointer really modifies the
    offset, without affecting the traditional pointer.

The point is to allow pointers into an area of memory that can move.

One example of this is in memory-mapped files. Many operating systems
have the ability to map an entire disk file into virtual memory,
which can then be accessed simply by supplying an appropriate address.

    struct DataFile;
    DataFile *datafile;

    struct FreeList {
        // Problematic -- can BasedPointer refer to FreeList?
        std::BasedPointer<FreeList,datafile> prev;
        std::BasedPointer<FreeList,datafile> next;
        int size; // Size of this free block (including the FreeList data)
    };

    struct CustList {
        // Problematic -- can BasedPointer refer to CustList?
        std::BasedPointer<CustList,datafile> prev;
        std::BasedPointer<CustList,datafile> next;
        // Avoid using std::string -- the text data needs to
        // be contained inside the LinkedList structure
        char    Name[50]; // Customer name
        double  balance;  // Balance due
        // etc.
    };

    struct DataFile {
        char  version[16]; // Version number of data file
        BasedPointer<FreeList, datafile> freeHead;
        int freeSize;      // # items on freeHead list
        BasedPointer<FreeList, datafile> custHead;
        int custSize;      // # items on custHead list
        char unused[320];  // Must be 0; reserved for future use
    };

    void*OpenMapFile(const char*Filename);
    void CloseMapFile(void *address);

    int main() {
        datafile = (DataFile*)OpenMapFile("MyFile.dat");
        if (!datafile) throw "Could not map datafile!";
        for (CustList *c = datafile->custHead; c; c = c->next)
            std::cout << c->Name << std::endl;
        CloseMapFile((void*)datafile);
    }

>From here on, llData and llFree can be used just like any other
pointer -- but in reality, instead of storing the address of
a linked-list node, it stores the offset from *datafile.

Another example of the same idea would be a region of memory
shared between two processes on the same computer -- the shared
memory might not have the same virtual address in both processes.
Each process would have it's own traditional pointer pointing to
the beginning of shared memory, and the pointers within the region
would be BasedPointers.

This is probably a library-only change -- I don't imagine the
language proper needs to change to implement this.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Carl Daniel" <cpdaniel@pacbell.net>
Date: Wed, 15 May 2002 18:19:59 GMT
Raw View
"Allan W" <Allan_W@my-dejanews.com> wrote in message
news:23b84d65.0205141408.5fceab6c@posting.google.com...
> I'd like to see the next version of Standard C++ include something
> that I will call "Based Pointers." A based pointer is an offset
> from some traditional pointer, which is declared seperately.

You might be interested to know that Microsoft's C/C++ compilers have
supported this scheme for over 10 years, going clear back to 16-bit DOS
code.  The MS implementation, since it dates back to pre-C++ days, is a
core-language feature, not a library addition as you propose.

-cd


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Wed, 15 May 2002 20:58:09 GMT
Raw View
In article <23b84d65.0205141408.5fceab6c@posting.google.com>, Allan W
<Allan_W@my-dejanews.com> writes
>I'd like to see the next version of Standard C++ include something
>that I will call "Based Pointers." A based pointer is an offset
>from some traditional pointer, which is declared seperately.
>
>I haven't yet worked out all of the details, particularly with
>respect to declaration or initialization of these pointers. But once
>it's been initialized, it works like this:
>
>    When the based pointer is used as an rvalue, it automatically
>    converts to a standard pointer by adding the offset to the address
>    in the traditional pointer. For instance,
>        *ptr
>    is equivalent to *(trad+off), where trad is the traditional pointer
>    and off is the offset. Unlike normal pointer arithmetic, the
>    addition would not take into account the sizeof the object being
>    pointed to...
>
>    When the based pointer is assigned, it automatically converts a
>    standard address into a based address by subtracting the value
>    in the traditional pointer (again, without respect to sizeof).
>    In other words, modifying the based pointer really modifies the
>    offset, without affecting the traditional pointer.

This looks a little like the named address space proposal in the
Embedded C TR going to vote as a draft TR from WG14.


--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Allan_W@my-dejanews.com (Allan W)
Date: Thu, 16 May 2002 19:46:31 GMT
Raw View
Francis Glassborow <francis.glassborow@ntlworld.com> wrote
> This looks a little like the named address space proposal in the
> Embedded C TR going to vote as a draft TR from WG14.

I'm not familiar with that proposal.

Is there a publicly-available draft of the spec?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Thu, 16 May 2002 21:00:44 GMT
Raw View
In article <23b84d65.0205161144.45a5c600@posting.google.com>, Allan W
<Allan_W@my-dejanews.com> writes
>Francis Glassborow <francis.glassborow@ntlworld.com> wrote
>> This looks a little like the named address space proposal in the
>> Embedded C TR going to vote as a draft TR from WG14.
>
>I'm not familiar with that proposal.
>
>Is there a publicly-available draft of the spec?

http://std.dkuug.dk/JTC1/SC22/WG14

and follow the links.


--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]