proper way to detect NULL char* on imported function?
Posted: Thu May 07, 2015 10:14 pm
I am importing a function from someones library that has this use pattern:
This function returns two things; the char* points to a string/label, and the integer retval is the value associated with it. The int index indexes you into the table.
You are intended to iterate over the table from 0 on up, and stop when it returns the NULL pointer.
I beat my head on this for an hour, I was doing:
My code worked beautifully, but would segfault when it came to the NULL pointer.
I tried various things to detect when (foo) was returning a NULL pointer. I tried (get-long (address)), I tried (address), I tried (get-long), I tried (get-long (get-long)), etc.
Finally I switched the import statement to void* instead of char*
Now I could detect NULL easily.
So, I'm using void* for now, but it would be nice to go back to using char*, char* works when I do regular (not extended) import of function foo, and the char* integration with native newlisp strings is really nice, not having to do the extra get-string step, especially because I'm dealing with a library that brings in binary data, and the strings are not \0 terminated ones.
Code: Select all
char* foo(int index, int* retval);
You are intended to iterate over the table from 0 on up, and stop when it returns the NULL pointer.
I beat my head on this for an hour, I was doing:
Code: Select all
(import MY_LIB "foo" "char*" "int" "void*")
I tried various things to detect when (foo) was returning a NULL pointer. I tried (get-long (address)), I tried (address), I tried (get-long), I tried (get-long (get-long)), etc.
Finally I switched the import statement to void* instead of char*
Code: Select all
(import MY_LIB "foo" "void*" "int" "void*")
So, I'm using void* for now, but it would be nice to go back to using char*, char* works when I do regular (not extended) import of function foo, and the char* integration with native newlisp strings is really nice, not having to do the extra get-string step, especially because I'm dealing with a library that brings in binary data, and the strings are not \0 terminated ones.