struct / cast / ? design problem
ketmar via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Mar 16 11:55:49 PDT 2015
On Mon, 16 Mar 2015 11:18:16 -0700, Charles Hixson via Digitalmars-d-learn
wrote:
> My current best answer is to turn the "unused" into a vector of bytes,
> and then pass that to the using routines as a ref. This still is wide
> open to errors at the calling end, but the BlockHead end can ensure that
> the length of the vector remains constant whenever it accesses it (well,
> whenever the head is being written to disk. The byte vector would be a
> static array at the BlockHead end. This would seem to allow the using
> end to cast the byte vector into an appropriate struct for its own use,
> and that writes to it would be seen by BlockHead as writes to the
> header...but only to restricted parts of the header. The problem here
> is that the ref array might allow itself to be resized at the user end.
> That's not a problem as long as the resizing is to something smaller,
> but I'm not sure what happens if it gets resized to something larger. It
> looks like allowing a buffer overflow.
if you passing the array, not a slice, it can't be resized. i.e.:
void foo (ref ubyte[8] a) {
a[2] = 42;
//a.length = 6; // this will not compile: constant a.length is not an
lvalue
}
void main () {
ubyte[8] aa;
assert(aa[2] == 0);
foo(aa);
assert(aa[2] == 42);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150316/72a97c0a/attachment.sig>
More information about the Digitalmars-d-learn
mailing list