Author Topic: Does anyone know how set struct aligment to 1 byte in Dev-C++?  (Read 7359 times)

Akari

  • Moderator
  • *
  • Posts: 766
    • View Profile

sfx1999

  • *
  • Posts: 1142
    • View Profile
IIRC, the default compiler setting aligns to 32-bit. It gives better performance, IIRC. Anyway, you can get a set of commands to use by using gcc --help in a command prompt.

Synergy Blades

  • Guest
Had this problem with sizeof() giving incorrect size of structures.

Code: [Select]
#pragma pack(1)
..fixed it.

sfx1999

  • *
  • Posts: 1142
    • View Profile
Isn't it faster to align 32-bit though?

Akari

  • Moderator
  • *
  • Posts: 766
    • View Profile
Had this problem with sizeof() giving incorrect size of structures.

Code: [Select]
#pragma pack(1)
..fixed it.

Thanks, it helps.

Quote
Isn't it faster to align 32-bit though?

This is needed for writing binary files to disk. Save screenshot for example =)

FF7ExpNeg1

  • *
  • Posts: 16
    • View Profile
Writing the structs directly will pose a problem for systems in big endian though.

DreamMaster

  • Guest
It depends on whether or not you want the savegames to maintain binary compatibility with the original savegames. If not, it doesn't matter if the system is big endian, since the data would be read back in the same byte alignment format as it was written in.

Of course, endian issues do matter when reading in the game datafiles, which is why ScummVM, for instances, has macros like READ_LE_UINT16 and FROM_LE_16 to read values in and swap the bytes if compiled on a big endian system.

ChaosControl

  • *
  • Posts: 741
  • ยค
    • View Profile
IIRC, the default compiler setting aligns to 32-bit. It gives better performance, IIRC. Anyway, you can get a set of commands to use by using gcc --help in a command prompt.
Sorry to go kinda off-topic, but what does IIRC mean if I may ask?

dziugo

  • *
  • Posts: 1470
    • View Profile
    • A new copy of FF7 thanks to Salk. Pack (zip/rar/etc) your saved game before sending it to me.
If I Remember Correctly

mirex

  • *
  • Posts: 1645
    • View Profile
    • http://mirex.mypage.sk
I've been using #pragma pack for some time as I was working with binary files. This is enough if you work only on one platform, for example on windows. But if you want to create an portable app, which will work also on for example MAC motorla hardware with different endian number packing, then you have to read structures number-by-number depending on the endian type, and then you don't have to care about packing, because you're reading nubmers one by one.

Oh by the way, IMO proper way of usage is:
Code: [Select]
// store current packing mode
#pragma pack( push )

// set new packing mode
#pragma pack( 1 )

.... define structures

// restore previous packing mode
#pragma pack( pop )

Well the packed structs without endian checking lasted me for 4 years of my coding so maybe it will be enough for you too. ;)

If you want to take care about endian dependency, you can use my class here, which takes care of the endian-switching when reading the file:
http://forums.qhimm.com/index.php?topic=5531.msg72479#msg72479