How to dynamically alias a struct to a byte array?

Charles Hixson charleshixsn at earthlink.net
Sun Feb 17 11:07:32 PST 2013


The situation is I have a C library routine that returns a pointer to a 
malloc_ed byte array.  The handler routine that I've written immediately 
copies the unmanaged byte array to a byte[], and then calls 
std.stdlib.free(barray).  Then it returns the byte array.  So I've 
already copied it once.

Now other code looks at the byte array, and decides what kind of struct 
it represents.  When it figures this out, is there any way I can declare 
the array to be of the desired struct type without copying it over 
again?  I don't want to cast it each time I use it, since this is data 
read in from a file.  Better would be to copy it over and over.

The only thing I've though of so far is to declare a class that holds a 
bunch of abstract methods, and then have multiple descendants that each 
have a special variant of the struct.  And even then I've either got to 
copy it again, or to cast it each time I use it.  (Though at least with 
this approach all the casts are located in one area of the code.)


More information about the Digitalmars-d-learn mailing list