Structs arn't working.

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Exabyte256

Structs arn't working.

Post 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?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Structs arn't working.

Post by Candy »

__attribute__((packed)) -> try google.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Structs arn't working.

Post 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.
Every good solution is obvious once you've found it.
Exabyte256

Re:Structs arn't working.

Post 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...
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Structs arn't working.

Post 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.
Every good solution is obvious once you've found it.
Exabyte256

Re:Structs arn't working.

Post 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!
Post Reply