M
Mockey Chen
I saw some library code wrote like this:
struct X{
int x;
char y[0]; /* Note: here array size is ZERO, why? */
};
Why use array size zero. I heard about the C standard
not allow array with size zero, but my gcc tell me I can
compile this code without error or warning.
One give me explaination this trick can used for dynamic
memory, for example:
size_t bufsize;
...
struct X* px = NULL;
px = (struct X*)malloc(sizeof(struct X) + bufsize);
in this way, we can gain a X and additional bufsize in once
malloc invoke. Does this correct?
Another question is whether this code portable?
Any help is appreciated.
--
Regards.
Mockey Chen
Email: (e-mail address removed)
struct X{
int x;
char y[0]; /* Note: here array size is ZERO, why? */
};
Why use array size zero. I heard about the C standard
not allow array with size zero, but my gcc tell me I can
compile this code without error or warning.
One give me explaination this trick can used for dynamic
memory, for example:
size_t bufsize;
...
struct X* px = NULL;
px = (struct X*)malloc(sizeof(struct X) + bufsize);
in this way, we can gain a X and additional bufsize in once
malloc invoke. Does this correct?
Another question is whether this code portable?
Any help is appreciated.
--
Regards.
Mockey Chen
Email: (e-mail address removed)