Such as
int* getInstance() {
static int* inst = (int*)malloc(4);
return inst;
}
for(int i=0;i < 10; ++i)
getInstance()
"static int* inst = (int*)malloc(4);" excute only once at this code, How to implement it when I use nim code. ( don't use emit pragma )You can use this
proc getInstance(): ptr int =
var inst {.global} = cast[ptr int](alloc sizeof(int))
result = inst
But I prefer to just use a global variable as that is more honest, you have global state.
Oh great!! It's ok, Thank you!
"var inst {.global} = castptr int" I only need this perform once function, Convenient to use test code, , It don't worry about memory address changes and out of memory.