#include <mex.h>
#include <string.h>

static int init = 0;
static char *test = NULL;

void cleanup ()
{
	if (test)
		free(test);
}

void mexFunction (int nresults, mxArray **arrresults, int nargs,
		const mxArray **arrargs)
{
	if (!init)
	{
		test = (char *)malloc(10);
		mexMakeMemoryPersistent(test);
		strcpy(test, "test");
		mexAtExit(cleanup);
		init = 1;

		mxArray *tmp = mxCreateDoubleMatrix(2, 1, mxREAL);
		double *tmp2 = mxGetPr(tmp);
		tmp2[0] = (int)test;
		tmp2[1] = ((long long)(int)test) >> 32;
		mexPutVariable("global", "ctl_teststr", tmp);
	}
}
