1

I am developing program that will run only on windows, from windows xp to windows 10. using MFC. Do i have to be concerned about endianess issue. There will be many struct that will be converted to byte array and written and read from file.

typedef struct
{
   int              Userid;
   __int64          StartDate;
   __int64          EndDate;
   int              DaysLeft;
   WCHAR            Email[256];
   WCHAR            Serial[256];
   WCHAR            UserApplied[229];
   WCHAR            Country[256];
   WCHAR            Address[256];
}RecordInfo;

What are consequences of endianess (big/little). Is there any guide for writing endian-independent code in c++. Or if it is only for single platform (like windows only) there is no concern about endian issue.

Rahul
  • 441
  • 1
  • 6
  • 16
  • 1
    Found [this](http://stackoverflow.com/questions/6449468/can-i-safely-assume-that-windows-installations-will-always-be-little-endian) for you. – Steeve Dec 02 '16 at 08:38
  • 1
    All Windows systems are little-endian, so no issue there for a Windows-only program. Also different compilers have different rules for data-alignment and padding for structures. Create all the programs with Visual Studio to avoid these problems. – Barmak Shemirani Dec 02 '16 at 08:53
  • @BarmakShemirani thank you – Rahul Dec 02 '16 at 10:45
  • 1
    I'd be more worried about padding between `Userid` and `StartDate`. Easiest fix here: Move `UserId` _after_ `DaysLeft`. – MSalters Dec 02 '16 at 10:59
  • @MSalters what is the sequence to arrange the struct so padding issue don't arise. as you have suggested to move userid after daysleft. I have many struct in my program, so arranging all struct to correct sequence is required. – Rahul Dec 02 '16 at 12:19
  • @Rahul: as a rule of thumb, decreasing alignment, but keep in mind that you may have existing data already stored using the old layout. In that case, figure out what padding you actually do have in those files, and add explicit named fields in the structure. E.g. you might end up with `std::int32_t padding_1` between `Userid` and `StartDate`. – MSalters Dec 02 '16 at 12:26
  • MFC has a serialization framework built in. Why have you decided against using this platform- and architecture-independent infrastructure? – IInspectable Dec 05 '16 at 01:17
  • i really don't know about built in serialization framework. I am just few week in mfc. I will be glad to know more about serialization framework – Rahul Dec 05 '16 at 12:35

0 Answers0