Hi all,
I have a question regarding FFI and alignment. I call a C function from Rust, which returns a pointer to a struct cmach_lua_t
. This one:
typedef struct {
lua_State *L;
size_t mem_used;
size_t mem_limit;
void *execlimit_error;
} cmach_lua_t;
The signature of the C function is:
cmach_lua_t *cmach_lua_new()
Now I would assume that such an interface is not unusual. My question is: Do I have any guarantee that the pointer returned by such a C function is properly aligned? I guess generally: no. Which would mean I cannot safely dereference that pointer, right?
However, let's assume the function internally does:
cmach_lua_t *cmach_lua_new() {
cmach_lua_t *S;
/* … */
S = calloc(1, sizeof(cmach_lua_t));
if (!S) return NULL;
/* … */
return S;
}
Will the calloc then assure the structure is properly aligned? And is the alignment done by calloc
sufficient to meet Rust's alignment rules? Couldn't they theoretically (or practically) differ?