c - using flexible array member with mmap -
i'm having trouble accessing global struct pointer i'm initalizing mmap. attempting access members of struct in functions outside of 1 declared in throw segfaults.
the struct:
typedef struct foo { uint32_t size; bar_t array[0]; } foo_t;
the initialization:
foo_t* foo; // global static void* init_function(...) { fd = open(filename, o_creat | o_wronly, 0644); write(...); lseek(...); write(...); foo = mmap(0, big_size, prot_read | prot_write, map_shared, fd, 0); close(fd); foo->size = 0; }
what causes segfault:
static int another_function(...) { if (foo->size == 0) {...} //foo->size causes }
big_size defined value should adequately large needs.
any ways, apologize (...)s, issues showing up. i've looked mmap docs , variable length methods no luck. in advance!
you don't show everything, seems assign global variable, don't initialize contents of mapped region. should @ least give foo->size = 0
somewhere, if file newly created, or alternatively should use ftruncate
instead of fseek write
sequence warrant blocks (including size
) 0
filled.
Comments
Post a Comment