The C code:
typedef struct __kstring_t {
size_t l, m;
char *s;
} kstring_t;
kstring_t * get_ptr_of_kstr(int num){
char * ch = (char *)malloc(num);
kstring_t kstr = {0, num, ch};
kstring_t *p = &kstr;
printf("In C, kstr.l: %zu\n", p->l);
printf("In C, kstr.m: %zu\n", p->m);
printf("In C, kstr.s: %p\n", p->s);
printf("In C, pointer to kstr: %p\n", p);
return p;
};
The Julia code
type KStr
l::Csize_t
m::Csize_t
s::Ptr{Cchar}
end
Problem
When I use ccall
to call get_ptr_of_kstr
and get a pointer of kstring_t
in Julia
, use unsafe_load to get its value, but the value seems wrong.
The messages are below:
In C, kstr.l: 0
In C, kstr.m: 100000
In C, kstr.s: 0x37b59b0
In C, pointer to kstr: 0x7fffe7d80b90
kstr = Ptr{HT.KStr} @0x00007fffe7d80b90
unsafe_load(kstr).s = Ptr{Int8} @0x00007fffe7d80b90
The value of unsafe_load(kstr).s
is same as kstr
. Why? how to fix it?