March 31, 2016 Can i get a array Slicing where the top from?and how? | ||||
---|---|---|---|---|
| ||||
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 ? |
March 31, 2016 Re: Can i get a array Slicing where the top from?and how? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dsby | 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 |
Copyright © 1999-2021 by the D Language Foundation