json - Understanding pointer casting on struct type in C -
i'm trying understanding pointer casting in case.
# https://github.com/udp/json-parser/blob/master/json.c#l408 #define json_char char typedef struct _json_object_entry { json_char * name; unsigned int name_length; struct _json_value * value; } json_object_entry; typedef struct _json_value { struct { unsigned int length; json_object_entry * values; #if defined(__cplusplus) && __cplusplus >= 201103l decltype(values) begin () const { return values; } decltype(values) end () const { return values + length; } #endif } object; } (*(json_char **) &top->u.object.values) += string_length + 1;
due see top->u.object.values
has address of first element of values ( type : json_object_entry ), , address of values, casting char, .. , here i'm lost. don't understand purpose of this.
// notes : 2 pass parser wonders this.
thanks
author here (guilty charged...)
in first pass, values
hasn't yet been allocated, parser cheats using same field store amount of memory (length) that's going required when it's allocated in second pass.
if (state.first_pass) (*(json_char **) &top->u.object.values) += string_length + 1;
the cast json_char
add multiples of char
length, rather multiples of json_object_entry
.
it bit (...ok, more bit...) of dirty hack re-using field that, save adding field json_value
or using union (c89 unions can't anonymous, have made structure of json_value
bit weird).
there's no ub here, because we're not using values
array of structs @ point, subverting type system , using integer.
Comments
Post a Comment