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?
Structs arn't working.
Re:Structs arn't working.
__attribute__((packed)) -> try google.
Re:Structs arn't working.
Ah what the heck, I feel like it...
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.
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.Exabyte256 wrote: Like this:
typedef struct {
unsigned char
unsigned char
...
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.typedef struct {
unsigned char
unsigned char
unsigned long
...
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.
Re:Structs arn't working.
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).
Thanks for your help, I'll go lookup packed structs on 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!Candy wrote: __attribute__((packed)) -> try google.
Thanks for your help, I'll go lookup packed structs on google...
Re:Structs arn't working.
...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.
Re:Structs arn't working.
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!