D
David Lee
A project which I'm involved in, which is supposed to be reasonably clsoe
to the ANSI-C standard, compiles under 'gcc' but not under Sun Studio.
There are two issues:
1. Parts of the code are handling typical network packets, comprising
header and data, so use declarations of the form:
struct pkt {
int header_thing;
int another_header_thing;
char data[0]; /* lots of data here */
};
with the idea that 'struct pkt' (and 'sizeof(struct pkt)' etc.) are aimed
at processing the header, while the code handling the (non-zero-sized)
'data' would be handed a pointer and then do its own processing. (The
associated packet 'malloc' "sizeof(struct pkt) + <length of data>", which
seems reasonably clean.)
This fails to compile under SunStudio because of the 'data[0]'. K&R
ANSI-C, section A8.6.2 seems quite clear that such failure is correct:
"if the constant expression is present, it must have integral type, and
value greater than 0".
(In other words, that 'gcc' allowing 'data[0]' seems to be a 'gcc'
extension.)
Reducing 'data[0]' to 'data[]' allows it to compile. But I can see the
attraction of 'data[0]' in clearly expressing an intention.
Q1a: Is 'data[0]' allowed by other C standards (C99, etc.)?
Q1b: In K&R ANSI-C, wehre is the meaning of the reduced 'data[]' for such
a context defined? (To confirm that "sizeof(struct pkt)" would be the
same for 'data[0]' and 'data[]'.)
2. A transfer vector of functions declared:
struct OPS ops = {
send: socket_send,
recv: socket_recv,
};
(where "OPS", "socket_send", "socket_recv", etc. are declared earlier).
Those preamble "send:" and "recv:" look strange to me. Simply removing
them allows the code to compile and run everywhere (and to warn if the
declaration and definition deviate from each other). Keeping them causes
compilation failure on SunStudio.
What C standard (if any) permits this (gcc-accepted) syntax?
Thanks in advance!
--
: David Lee I.T. Service :
: Senior Systems Programmer Computer Centre :
: UNIX Team Leader Durham University :
: South Road :
: http://www.dur.ac.uk/t.d.lee/ Durham DH1 3LE :
: Phone: +44 191 334 2752 U.K. :
to the ANSI-C standard, compiles under 'gcc' but not under Sun Studio.
There are two issues:
1. Parts of the code are handling typical network packets, comprising
header and data, so use declarations of the form:
struct pkt {
int header_thing;
int another_header_thing;
char data[0]; /* lots of data here */
};
with the idea that 'struct pkt' (and 'sizeof(struct pkt)' etc.) are aimed
at processing the header, while the code handling the (non-zero-sized)
'data' would be handed a pointer and then do its own processing. (The
associated packet 'malloc' "sizeof(struct pkt) + <length of data>", which
seems reasonably clean.)
This fails to compile under SunStudio because of the 'data[0]'. K&R
ANSI-C, section A8.6.2 seems quite clear that such failure is correct:
"if the constant expression is present, it must have integral type, and
value greater than 0".
(In other words, that 'gcc' allowing 'data[0]' seems to be a 'gcc'
extension.)
Reducing 'data[0]' to 'data[]' allows it to compile. But I can see the
attraction of 'data[0]' in clearly expressing an intention.
Q1a: Is 'data[0]' allowed by other C standards (C99, etc.)?
Q1b: In K&R ANSI-C, wehre is the meaning of the reduced 'data[]' for such
a context defined? (To confirm that "sizeof(struct pkt)" would be the
same for 'data[0]' and 'data[]'.)
2. A transfer vector of functions declared:
struct OPS ops = {
send: socket_send,
recv: socket_recv,
};
(where "OPS", "socket_send", "socket_recv", etc. are declared earlier).
Those preamble "send:" and "recv:" look strange to me. Simply removing
them allows the code to compile and run everywhere (and to warn if the
declaration and definition deviate from each other). Keeping them causes
compilation failure on SunStudio.
What C standard (if any) permits this (gcc-accepted) syntax?
Thanks in advance!
--
: David Lee I.T. Service :
: Senior Systems Programmer Computer Centre :
: UNIX Team Leader Durham University :
: South Road :
: http://www.dur.ac.uk/t.d.lee/ Durham DH1 3LE :
: Phone: +44 191 334 2752 U.K. :