Manually allocated structs

Domingo Alvarez Duarte via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 27 06:44:36 PDT 2014


On Sunday, 27 July 2014 at 12:49:01 UTC, Adam D. Ruppe wrote:
> I would do it something like this:
>
> struct test {
> 	size_t size;
>
> 	@property char[] buf() {
> 		return (_buf.ptr)[0 .. size];
> 	}
> 	private char[0] _buf;
> }
>
>
> The buf property returns a slice that uses the size member to 
> give you bounds checking, but uses the ptr of the final member 
> in the struct to bypass bounds checking on that array.
>
> That way, you can allocate as much memory as you need without 
> having to use the naked pointer anywhere outside, getting D to 
> help you stay in bounds.

Yes it did what I was expecting, thank you !



More information about the Digitalmars-d-learn mailing list