Hello, i am new to nim language. Am i misunderstand the manual memory management in nim or i could not able to allocate new memory and assign it to a int? So in C, i can do this
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char** argv)
{
int* a = (int*)malloc(sizeof * a);
printf("%p\n", a);
*a = 10;
printf("%i\n", *a);
free(a);
a = NULL;
return 0;
}
But how can i do this in nim? (sorry for bad english)
I new tried it, but what is wrong with the this straight forward approach:
proc main(): int =
var
a: ptr int = cast[ptr int](alloc(sizeof(int)))
echo cast[int](a)
a[] = 10
echo a[]
dealloc(a)
a = nil
return 0
echo main()
Starting point for me was
https://nim-lang.org/docs/lib.html
typing in obvious "alloc" which gives
https://nim-lang.org/docs/system.html#alloc%2CNatural
There may exist other, maybe better solutions still.
proc main() =
var a = create(int)
echo a[]
a[] = 10
echo a[]
dealloc(a)
a = nil
main()
We also have the create template in the stdlib that's super easy to use, and cuts all the clutter.