所以我有这两个结构。只需要担心这三个变量;name_size、name 和 xattrs[0]。
typedef struct dxattr {
unsigned int name_size; /* length of name string in bytes */
unsigned int value_offset; /* offset of value in value blocks */
unsigned int value_size; /* length of value string in bytes */
char name[0]; /* reference to the name string */
} dxattr_t;
typedef struct xcb {
unsigned int value_blocks[5]; /* blocks for xattr values */
unsigned int no_xattrs; /* the number of xattrs in the block */
unsigned int size; /* this is the end of the value list in bytes */
dxattr_t xattrs[0]; /* then a list of xattr structs (names and value refs) */
} xcb_t;
首先我更新 xattrs 中的索引 0
xcb_t *xcb = (xcb_t*)malloc( sizeof(xcb_t) );
xcb->xattrs[0].name_size = 5;
memcpy( xcb->xattrs[ 0 ].name, "keith", 5 );
xcb->xattrs[ 0 ].name[ 5 ] = 0; //null terminator
printf("xcb->xattrs[0].name = %s\n", xcb->xattrs[0].name );
printf("xcb->xattrs[0].name_size = %d\n", xcb->xattrs[0].name_size );
输出是;
xcb->xattrs[0].name = keith
xcb->xattrs[0].name_size = 5
然后我尝试更新第二个索引。
memcpy( xcb->xattrs[ 1 ].name, "david", 5 );
xcb->xattrs[ 1 ].name[ 5 ] = 0; //null terminator
printf("xcb->xattrs[0].name = %s\n", xcb->xattrs[0].name );
xcb->xattrs[1].name_size = 5;
printf("xcb->xattrs[0].name = %s\n", xcb->xattrs[0].name );
更新“name_size”变量后,先前索引中的“name”值立即被删除。
xcb->xattrs[0].name = keith
xcb->xattrs[0].name =
知道为什么会这样吗?