Can i get a array Slicing where the top from?and how?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 31 14:03:44 PDT 2016


On 03/30/2016 11:39 PM, Dsby wrote:
 > Like this:
 >
 > ubyte[] fun()
 > {
 >      ubyte[] str = cast(ubyte[])"hello world";
 >      ubyte[] by = Mallocator.instance.allocate(str.length);
 >      by[] = str[];
 >      return by[5..$];
 > }
 >
 > void mian()
 > {
 >      ubyte[] aa = fun();
 >     // i want free the aa's momeny.
 >     //Mallocator.instance.deallocate(aa);// it is erro, aa.ptr != by.ptr?
 >   // can I get the by's ptr and length?
 > }
 >
 >
 > can I get "by" in main function by aa?
 > i can get the way?is it possible ?

No, you can't.

Normally, you would have a cleanup function that corresponds to fun(), 
which the users call when they're done with their resources.

void foo_cleanup(ubyte[] by) {
     // Do your magic here to find the original pointer.
     // For example, you can use an AA to map from
     // by.ptr to the original pointer.
}

Ali



More information about the Digitalmars-d-learn mailing list