Manually allocated structs

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 27 05:49:00 PDT 2014


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.


More information about the Digitalmars-d-learn mailing list