IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New gcc question
One of my colleagues has been beating his head against the wall over this for about 3 days now, so I offered to query the vast power of the LRPD to see if there is a soultion.

He has a structure that looks something like this:
\ntypedef struct\n{\n    unsigned char   a_field;\n    unsigned char   a_nother_field;\n} HEADER_STRUCT;\n


This is the structure of a communications header of some sort. So far, so good. Now, he wants to put that structure into another structure that he can overlay on top of a communications stream, whose first two bytes are this header, and the next umpteen bytes contain the actual data...something like this:
\ntypedef struct\n{\n    HEADER_STRUCT        header;\n    ACTUAL_DATA_STRUCT   data;\n} MESSAGE_STRUCT;\n


The problem is that, no matter what combination of #pragma pack or attribute (align)
tags he uses, the size of the HEADER_STRUCT adamantly remains 4. The question: What set of magic incantations is necessary to properly pack the MESSAGE_STRUCT so that the size of the header field is 2 and not 4.

(Stated another way, how do you get gcc to exhibit the same behavior that Visual C-pus-pus exhibits with a simple /Zp1 switch?)

thanks in advance for any help.
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New Re: gcc question
This is what I get. No special options. Running on Solaris.

\n$ cat p.c\ntypedef struct\n{\n    unsigned char   a_field;\n    unsigned char   a_nother_field;\n} HEADER_STRUCT;\n\n\nint main() {\n    HEADER_STRUCT hs;\n    printf ("Sizeof hs = %d\\n", sizeof(hs));\n    return 0;\n}\n$ gcc p.c -o p\n$ ./p\nSizeof hs = 2\n


Could it be that he is getting padding at the _end_ of the structure? The characters may still be packed in the first two bytes.
--
-- Jim Weirich jim@weirichhouse.org [link|http://onestepback.org|http://onestepback.org]
---------------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)
New Yes, the padding is at the end of the struct
Gcc insists on seeing the HEADER_STRUCT as some multiple of an int in size (this is a 32-bit machine, of course). So when HEADER_STRUCT is used within another structure, the sizeof() the field that is the HEADER_STRUCT reports 4 as its size.

BTW, when we do exactly what you're doing, we get that the size of the structure is 4. On Linux, using gcc 3.10.
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New Using gcc-3.3 on Debian: Sizeof hs = 2
-YendorMike

[link|http://www.hope-ride.org/|http://www.hope-ride.org/]
New OK...minor mistake
On further review, the problem is not appearing on Linux. It is appearung on a Nios processor. We have a cross-compiler (based on the gcc 3.10 compiler) that generates code for the Nios soft-core processor. This is the pig that is padding the sub-structures in spite of all attempts to the contrary.

(Just compiled and ran Jim's test program myself on the Linux machine, and recreated Jim's ... and Yendor's ... results.)
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New Knowing nothing about the processor in particular...
...doesn't prevent me from speculating?

Perhaps the processor accesses everything on a long word boundary? This might result in an optimizer pushing all structures to 4 byte boundaries.
New Damn, yer good!
Yes, the processor is big-endian, which implies boundary alignment. However, you can mis-align stuff legally. What the compiler does is use repeated byte accesses to load up a temporary variable (the is aligned), then use the temporary variable. My suspicion is that that rather clever piece of derring-do is not extended to handling nested structures.

Probably too complicated for the GNU-ers...
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New Re: Damn, yer good!
You need the proper GNUisms. The linux kernel is full of this stuff. Try this.

typedef struct
{
unsigned char a_field;
unsigned char a_nother_field;
} HEADER_STRUCT;

typedef struct
{
HEADER_STRUCT header;
int data;
} __attribute__((packed)) MESSAGE_STRUCT;


int main() {
HEADER_STRUCT hs;
MESSAGE_STRUCT ms;
printf ("Sizeof hs = %d\\n", sizeof(hs));
printf ("Sizeof ms = %d\\n", sizeof(ms));
return 0;
}

my output:

Sizeof hs = 2
Sizeof ms = 6

Probably too complicated for the GNU-ers...


And this comment was uncalled for.

Dave "LordBeatnik"
New jb's agin' gcc
-drl
New Ross, Ross, Ross...How you oversimplify...
I'm not "agin'" gcc so much as I am profoundly disappointed in it.

You see, I had this Pollyanna-ish idea that GNU, being all Open Source and all, and having the opportunity to cull development talents from the Best and Brightest of the development Community, would be a Shining Star...a Beacon of Light illuminating the farthest reaches of C and C++ mastership....conforming to the latest of ANSI/ISO C and C++ standards while having myraid capabilities to tailor its behavior to whatever Obscure Dialect of C or C++ you happened to confront it with...to provide a bar that even Borland would not be able to scale....

Then I was introduced to 2.95.2.

Ya know...all they would have had to do was supply a compliant STL....

So you see, I'm not really against GNU per se. Now Micros~1...them I'm against!




(I hope we've cleared that up...)
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New Played with 3.x?
Significantly better code generation, allegedly.


Peter
[link|http://www.debian.org|Shill For Hire]
[link|http://www.kuro5hin.org|There is no K5 Cabal]
[link|http://guildenstern.dyndns.org|Blog]
New Well, somewhat...
The only 3.x I've been exposed to was the Nios variant, the problems with which I've discussed in this thread. Granted, the alignment problems are probably parochial, and not the fault of GNU. On the issues of ANSI compliance, and the STL...the jury's still out.

Sure would like to get a coherent set of documentation on it, tho (and no, the man page nor the D'oxygen pile do not qualify as "coherent documentation"!).
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New Been there. Done that.
Didn't work!
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New Out of curiosity?
What's the length of the data structure if there's only a one byte field defined? Guessing that it's still 4.
New Yup. 4. (Coincidently, == sizeof(int))
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

     gcc question - (jb4) - (14)
         Re: gcc question - (JimWeirich) - (13)
             Yes, the padding is at the end of the struct - (jb4) - (12)
                 Using gcc-3.3 on Debian: Sizeof hs = 2 -NT - (Yendor)
                 OK...minor mistake - (jb4) - (10)
                     Knowing nothing about the processor in particular... - (ChrisR) - (9)
                         Damn, yer good! - (jb4) - (8)
                             Re: Damn, yer good! - (lordbeatnik) - (7)
                                 jb's agin' gcc -NT - (deSitter) - (3)
                                     Ross, Ross, Ross...How you oversimplify... - (jb4) - (2)
                                         Played with 3.x? - (pwhysall) - (1)
                                             Well, somewhat... - (jb4)
                                 Been there. Done that. - (jb4) - (2)
                                     Out of curiosity? - (ChrisR) - (1)
                                         Yup. 4. (Coincidently, == sizeof(int)) -NT - (jb4)

Your configuration just allows perl programs to error out faster.
69 ms