A safer interface for core.stdc

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Sat Feb 7 17:26:27 PST 2015


On Sun, Feb 08, 2015 at 12:39:39AM +0000, bearophile via Digitalmars-d wrote:
> Andrei Alexandrescu:
> 
> >Such wrappers would allow safe code to use more C stdlib primitives.
> 
> I'd also like a safer templated wrapper for calloc() and malloc() and
> similar.
[...]

You mean something like this?

	T* malloc(T)() @trusted
	{
		return cast(T*)malloc(T.sizeof);
	}

	struct MyStruct {
		int x, y, z;
	}

	void main() {
		auto p = malloc!MyStruct();

		// Not sure how to make free() usable from @safe, unless
		// we wrap the pointer returned by malloc().
		free(p);
	}


T

-- 
Leather is waterproof.  Ever see a cow with an umbrella?


More information about the Digitalmars-d mailing list