Page 1 of 1

Structs arn't working.

Posted: Tue Nov 08, 2005 5:11 am
by Exabyte256
I have a problem with a struct in C. It's mapped a file entry in the ISO format and compiles with the documentation, etc.

The size of the table I have to read is 34 bytes, when I look at the iso image in a hex editor, the entries are 34 bytes. Looking at the values in the struct, it's 34 bytes (not including the potential characters at the end for its name).

Like this:

typedef struct {
unsigned char
unsigned char
unsigned long
unsigned long
unsigned long
unsigned long
unsigned char
unsigned char
unsigned char
unsigned char
unsigned char
unsigned char
unsigned char
unsigned char
unsigned char
unsigned char
unsigned short
unsigned short
unsigned char
char[31]
} DirectoryStructure;

But when it comes to reading the thing, the last value (the start of the actual filename) is out of place by two bytes. This doesn't make any sense at all. To make it clearer, "SYSMAIN.BIN" comes out as "SMAIN.BIN"

What am I doing wrong?

Re:Structs arn't working.

Posted: Tue Nov 08, 2005 5:14 am
by Candy
__attribute__((packed)) -> try google.

Re:Structs arn't working.

Posted: Tue Nov 08, 2005 5:58 am
by Solar
Ah what the heck, I feel like it...
Exabyte256 wrote: Like this:

typedef struct {
unsigned char
unsigned char
...
You forgot some commas in there. If talking about a specific problem, give specific source code, not some pseudocode. It doesn't matter in this case though.
typedef struct {
unsigned char
unsigned char
unsigned long
...
The compiler will add padding bytes between the last 'char' and the first 'long', so that the 'long' values are properly aligned in memory. Most CPU families suffer severe performance penalties when accessing unaligned data.

As for how to solve that when you want to make your struct match some hard layout (like in hardware, or file systems, or other "fixed" data formats), see Candy's hint, or look up the command line option "-fpack-struct" in the GCC manual.

Re:Structs arn't working.

Posted: Tue Nov 08, 2005 7:29 am
by Exabyte256
I know I should've talked about some specific code, but I didn't have time to look up all those variable names (currently using public computers).
Candy wrote: __attribute__((packed)) -> try google.
Google's the first thing I tried. Then I tried reading tutorials on C structs and books on C. Finally after checking my code for a day going over everything step for step in a hex editor and calculator working out everything's complete position... I decided to ask here!

Thanks for your help, I'll go lookup packed structs on google...

Re:Structs arn't working.

Posted: Tue Nov 08, 2005 8:21 am
by Solar
...or look up the command line option "-fpack-struct" in the GCC manual. Since it is likely that's what you want in kernel code anyway, it's easier to use the command line option than adding an attribute to every struct declaration.

Re:Structs arn't working.

Posted: Tue Nov 08, 2005 8:51 am
by Exabyte256
Tempting, but I'd rather declare them manually. I'll take any basic optimizations I can, rather than requiring on "clever coding" for my programs to work fast!