🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2325

Re: pre/post conditions, and pointer-testing (was Re: F90 vs C++/C, optimization question?)

#2325
From: J. Kanze
Date: Fri, 27 Oct 1995 14:55
58 lines
3163 bytes
Alex Martelli (martelli@cadlab.cadlab.it) wrote:

|> This specific problem highlights ANOTHER sore need: a *portable* way
|> to test for "is this pointer inside this array, or not".  Not only
|> would this be precious for pre/post/invariant assertions, it would
|> come in handy in all sorts of low-level programming situations --
|> a memory allocation subsystem could check to see whether it's
|> being passed a valid pointer for deallocation, for example (a more
|> general solution to this latter issue would probably also need a
|> portable way to test whether a given pointer is, or isn't, suitably
|> aligned for a given type).

|> bool isPointerInside(const void* pointerBeingTested,
|>           const void* startOfArray,
|>           size_t sizeOfArray);
|> ideally with startOfArray/sizeOfArray allowed to be, in fact, a
|> _sub_-array, e.g., give "double A[20];",

|> isPointerInside((const void*)&A[11],(const void*)&A[8], 5*sizeof(double))

|> would be valid (NOT undefined behaviour) and yield true,

|> isPointerInside((const void*)&A[14],(const void*)&A[8], 5*sizeof(double))

|> would be valid (NOT undefined behaviour) and yield false.


|> and maybe also:

|> template<class T> isPointerAligned(const void* pointerBeingTested);
|> (to be called as, e.g., isPointerAligned<double>((const void*)myptr))

|> would, I feel, be worthy additions, mostly for low-level programming.

What you want can be done portably.  Consider:

    template< class Iterator >
    bool
    isContained( Iterator ptr , Iterator start , Iterator finish )
    {
        bool            found( false ) ;
        for ( ; ! found && start != finish ; start ++ )
            found = (start == ptr) ;
        return found ;
    }

Regretfully, the performance of the above leaves much to be desired, at
least for large arrays.  But it is standard conforming, and I believe
that it is the only way to do this in a standards conforming manner.
-- 
James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle--
                             --Beratung in industrieller Datenverarbeitung

[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <46qhpb$3re@gabi.gabi-soft.fr>
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage
References: <45tq00$dkd@hawk.hcsc.com> <465q4m$j07@wcap.centerline.com> <46is0j$j7c@hawk.hcsc.com> <46kuem$j45@manuel.anu.edu.au> <martelli.814695566@cadlab>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage
From: kanze@gabi-soft.fr (J. Kanze)
Newsgroups: comp.std.c++
Subject: Re: pre/post conditions, and pointer-testing (was Re: F90 vs C++/C, optimization question?)
Date: 27 Oct 1995 14:55:21 GMT
Organization: GABI Software, Sarl.
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <46qhpb$3re@gabi.gabi-soft.fr>
References: <45tq00$dkd@hawk.hcsc.com> <465q4m$j07@wcap.centerline.com> <46is0j$j7c@hawk.hcsc.com> <46kuem$j45@manuel.anu.edu.au> <martelli.814695566@cadlab>
NNTP-Posting-Host: taumet.eng.sun.com
Content-Type: text
X-Newsreader: TIN [version 1.2 PL2]
Content-Length: 2201
X-Lines: 54
Originator: clamage@taumet