data structure question
Hi
why somebody wants to write such thing
struct abc {
__u16 a;
__u16 b;
char abc[0];
} __attribute__((packed));
Specialy for zerod array of chars.
Quoted 7 lines · gumbold
why somebody wants to write such thing
struct abc { __u16 a; __u16 b; char abc[0]; } __attribute__((packed));Specialy for zerod array of chars.
A structure which ends with a zero-length array is intended for use as the "header" for a variable-sized block of data, e.g.:
struct abc *new_abc(__u16 a, __u16 b, const char *string)
{
int len = strlen(string);
struct abc *p = malloc(sizeof(struct abc) + len + 1);
p->a = a;
p->b = b;
memcpy(p->abc, string, len + 1);
return p;
}Signature · 1 line
Glynn Clements <[email protected]>
Quoted 17 lines · Glynn Clements
Quoted 9 lines · gumbold
why somebody wants to write such thing
struct abc { __u16 a; __u16 b; char abc[0]; } __attribute__((packed));Specialy for zerod array of chars.
A structure which ends with a zero-length array is intended for use as the "header" for a variable-sized block of data, e.g.:
Quoted 14 lines
struct abc *new_abc(__u16 a, __u16 b, const char *string)
{
int len = strlen(string);
struct abc *p = malloc(sizeof(struct abc) + len + 1);
p->a = a;
p->b = b;
memcpy(p->abc, string, len + 1);
return p;
}So if my compiler can't handle such code, can i change it to struct abc {
__u16 a;
__u16 b;
char abc[1];
} __attribute__((packed));
Quoted 25 lines · gumbold
Quoted 11 lines
Quoted 7 lines
why somebody wants to write such thing
struct abc { __u16 a; __u16 b; char abc[0]; } __attribute__((packed));Specialy for zerod array of chars.
A structure which ends with a zero-length array is intended for use as the "header" for a variable-sized block of data, e.g.:
Quoted 13 lines
struct abc *new_abc(__u16 a, __u16 b, const char *string) { int len = strlen(string); struct abc *p = malloc(sizeof(struct abc) + len + 1); p->a = a; p->b = b; memcpy(p->abc, string, len + 1); return p; }
Quoted 6 lines
So if my compiler can't handle such code, can i change it to
struct abc { __u16 a; __u16 b; char abc[1]; } __attribute__((packed));
Yes, although you would need to adjust the size calculation if you need to calculate the exact size of the overall block.
Zero-length arrays aren't valid in C89, but are in C99. Also, gcc has supported them as an extension since long before it started supporting C99.
Signature · 1 line
Glynn Clements <[email protected]>
Quoted 40 lines · Glynn Clements
Quoted 27 lines · gumbold
Quoted 12 lines
Quoted 9 lines
why somebody wants to write such thing
struct abc { __u16 a; __u16 b; char abc[0]; } __attribute__((packed));Specialy for zerod array of chars.
A structure which ends with a zero-length array is intended for use as the "header" for a variable-sized block of data, e.g.:
Quoted 15 lines
struct abc *new_abc(__u16 a, __u16 b, const char *string) { int len = strlen(string); struct abc *p = malloc(sizeof(struct abc) + len + 1); p->a = a; p->b = b; memcpy(p->abc, string, len + 1); return p; }Quoted 8 lines
So if my compiler can't handle such code, can i change it to
struct abc { __u16 a; __u16 b; char abc[1]; } __attribute__((packed));
Quoted 9 lines
Yes, although you would need to adjust the size calculation if you need to calculate the exact size of the overall block.
Zero-length arrays aren't valid in C89, but are in C99. Also, gcc has supported them as an extension since long before it started supporting C99.
So char abc[0] actualy pointer to chat? It should be 4 byte long on x86. I can't see it with sizeof.
Quoted 1 line · gumbold
So char abc[0] actualy pointer to chat?
No, it's an array of char.
Quoted 1 line
It should be 4 byte long on x86. I can't see it with sizeof.
It's zero bytes long.
If you had:
struct abc {
__u16 a;
__u16 b;
char abc[4];
};then sizeof(struct abc) would be 8 and the offset of abc from the beginning of the structure would be 4.
With:
struct abc {
__u16 a;
__u16 b;
char abc[0];
};sizeof(struct abc) would be 4 and the offset of abc from the beginning of the structure would still be 4 (i.e. p->abc refers to the first byte after the structure).
Signature · 1 line
Glynn Clements <[email protected]>
Quoted 37 lines · Glynn Clements
Quoted 3 lines · gumbold
So char abc[0] actualy pointer to chat?
No, it's an array of char.
Quoted 3 lines
It should be 4 byte long on x86. I can't see it with sizeof.
It's zero bytes long.
If you had:
struct abc { __u16 a; __u16 b; char abc[4]; };then sizeof(struct abc) would be 8 and the offset of abc from the beginning of the structure would be 4.
With:
struct abc { __u16 a; __u16 b; char abc[0]; };
Quoted 6 lines
sizeof(struct abc) would be 4 and the offset of abc from the beginning of the structure would still be 4 (i.e.
p->abcrefers to the first byte after the structure).
I understand. But if you doing `p->abc = (char *)malloc(111)' so you copy 4 bytes to the place started from p->abc, but it is out of struct abc memory (it is only 4 byte). Should be error?
Quoted 37 lines · Glynn Clements
Quoted 3 lines · gumbold
So char abc[0] actualy pointer to chat?
No, it's an array of char.
Quoted 3 lines
It should be 4 byte long on x86. I can't see it with sizeof.
It's zero bytes long.
If you had:
struct abc { __u16 a; __u16 b; char abc[4]; };then sizeof(struct abc) would be 8 and the offset of abc from the beginning of the structure would be 4.
With:
struct abc { __u16 a; __u16 b; char abc[0]; };
Quoted 6 lines
sizeof(struct abc) would be 4 and the offset of abc from the beginning of the structure would still be 4 (i.e.
p->abcrefers to the first byte after the structure).
Ok. i understand everything. thank you very much for help.
Quoted 11 lines · gumbold
Quoted 11 lines
then sizeof(struct abc) would be 8 and the offset of abc from the beginning of the structure would be 4.
With:
struct abc { __u16 a; __u16 b; char abc[0]; };
Quoted 5 lines
Quoted 3 lines
sizeof(struct abc) would be 4 and the offset of abc from the beginning of the structure would still be 4 (i.e.
p->abcrefers to the first byte after the structure).I understand. But if you doing `p->abc = (char *)malloc(111)'
That would be an error. p->abc is an array, not a pointer.
Arrays and pointers aren't the same. They are often confused, for several reasons:
1. An array is automatically converted to a pointer to its first element whenever it occurs as an expression. This doesn't apply to lvalues (e.g. in declarations or as the argument to sizeof).
2. In a declaration, the syntax foo[...] declares an array named foo, but in the expression foo[...], foo is a pointer (it might be an array which has been converted to a pointer according to point 1 above, but it's still a pointer).
3. In a function prototype, pointer arguments can be declared using either "T foo[]" or "T *foo".
Signature · 1 line
Glynn Clements <[email protected]>