shared arrray problem

Charles Hixson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 19 10:51:05 PST 2016


I have a piece of code that looks thus:

/**    Returns an editable file header missing the header length and data
  * length portions.  Those cannot be edited by a routine outside this 
class.
  * Access to them is available via the lenHead and lenRec functions.
  * Warning:  Do NOT change the size of the header.  If you do the size
  * will be reset to the current value before it is saved, and results are
  * unpredictable.  Also do not replace it.  This class maintains it's own
  * pointer to the header, and your replacement will be ignored. */
ubyte[]    header()        @property    {    return fHead[4..$];    }

I want other classes to be able to modify the tail of the array, but not 
to resize it.  Eventually it should be written to a file. This way works 
(well, should work) but looks radically unsafe for the reasons indicated 
in the comments.  I'm sure there must be a better way, but can't think 
of what it would be.  The only alternative I've been able to think of 
is, approx:

bool    saveToHeader (ubyte[] rec)    { ... buf[4..$] = rec[0..$]; ... }

but that would be guaranteed to have an extra copying step, and it's 
bool because if the length of the passed parameter isn't correct 
saveToHeader would fail.  It may still be better since in this case the 
copying would be rather minimal, but the general problem bothers me 
because I don't see a good solution.



More information about the Digitalmars-d-learn mailing list