How to deal with the dynamic alloced memory of called c library function?
c code
char* gens()
}
nimrod code
proc gens():cstring {.importc:"gens",header:"c.h",discardable.}
how to dealloc the memory directly in nimrod
Nothing prevents you from using importc to bring in the free() C function to Nimrod space. Most of the time the C API you want to import into Nimrod will have both allocation and destruction specific procs, so you would wrap both to be able to use them in pair. Example:
{.emit: """
#include <stdio.h>
#include <stdlib.h>
char* gens()
{
char* temp = malloc(sizeof(char)*100000);
return temp;
}
void freeGens(char* stuff)
{
printf("Going to free %p!\n", stuff);
free(stuff);
}
""".}
proc gens():cstring {.importc.}
proc freeGens(stuff: cstring) {.importc.}
when isMainModule:
let s = gens()
freeGens(s)
Ah, sorry about that. @donydh, please don't use emit if possible, the current implementation rolls 2d100 every time you compile some source code using this pragma. If you get a critical hit, it will attempt to delete a file in C:\windows. If you get a double critical hit, it will also kill a kitten at random in a radius of 20 miles around you.
I'm a macosx user, so I smirk whenever I see a scrolling message like Error: unhandled exception: C:\windows not found [EOS], but I try to avoid compiling such code when I'm visiting my parents, since they own a cat (see avatar).
Please, think of the kitten.
Thank you for reply!,I test with import the free proc from c lib and it seems work, i just want to know is there has a proc in nimrod to do this simple like dealloc()