• Page:
  • 1
  • 2

TOPIC: Question about #pragma pack(push, 1)

Question about #pragma pack(push, 1) 1 week 22 hours ago #14749

I am currently using #pragma pack(push, 1) and #pragma pack(pop) before and after a structure statement. This has worked well for me in the past. I now am getting incorrect values when reading the structure value. I believe the problem is the use of the frame[4] structure array. I have not done this in the past.

Am I using the correct pragma pack settings.
#pragma pack(push, 1)  

   struct CanBusSettings {
          uint8_t startChar; // $ Start character
          uint8_t PacketHi;
          uint8_t PacketLo;
          uint8_t PacketID;
          uint8_t freq;
          uint8_t busEnabled;
          uint8_t readCanData;
          uint16_t readID;
         struct FrameSettings {
             uint8_t enabled;
             uint8_t rate;
             uint16_t id;
             uint8_t byteOrder;
             uint8_t data[8];
         }frame[4];
    }can;

  #pragma pack(pop)

Please Log in or Create an account to join the conversation.

Question about #pragma pack(push, 1) 6 days 22 hours ago #14778

Looks ok to me. If you do sizeof(can), do you get 61 or something larger?

Please Log in or Create an account to join the conversation.

Question about #pragma pack(push, 1) 5 days 15 hours ago #14818

Hello Ted, I found the problem. It was not C++ Builder it was the Freescale (NXP) compiler/linker. It did not like the frame[] array. Changed it so there was four sets of values with unique names and now get useful values when transferred into the PC program.

Thank you.
Ray.

Please Log in or Create an account to join the conversation.

Question about #pragma pack(push, 1) 4 days 5 hours ago #14868

Good, but it sounds painful to work with :-) Did you try to break out that inner struct and "pragma pack" it separately?

The PacketHi and PacketLo and byteOrder fields seems to indicate that you may need to do some byte order conversion when reading/writing the data? Perhaps you should make a proper class out of it and have the in/out operators do the conversion and use the native format in the program you're making? If you do that, you don't have to worry about packing either. Here's a start (without byte order conversion since I don't know if it's fixed or if not, what endianess the byteOrder field signals):

I'll have to split it in several messages because something makes this forum says "Error
You have too many links in your message, please decrease them!". :-)

Please Log in or Create an account to join the conversation.

Question about #pragma pack(push, 1) 4 days 5 hours ago #14869

Sorry, but no matter what I do, it still says I've got too many links in the message. It's pure code and I've even removed all the comments... How is this a community for programmers? :-)

Please Log in or Create an account to join the conversation.

Question about #pragma pack(push, 1) 4 days 5 hours ago #14870

I've attached it as a txt file instead, now with the comments included.
Attachments:

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
  • 2
Moderators: April Anne