AnsweredHot!Is there a bug with sizeof ?

Author
Blue_Key
Junior Member
  • Total Posts : 112
  • Reward points : 0
  • Joined: 2011/12/20 04:48:22
  • Location: 0
  • Status: offline
2018/11/05 09:12:47 (permalink)
0

Is there a bug with sizeof ?

 
 
With this struct
 
typedef struct __attribute__((packed))
{
    unsigned long address __attribute__((aligned));
    unsigned long len __attribute__((packed));
    unsigned short crc __attribute__((packed));
} FlashData;

 
on a PIC32MK, the size should be 4+4+2 = 10
 
However, sizeof(FlashData) returns 16...
#1
Jim Nickerson
User 452
  • Total Posts : 5458
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re: Is there a bug with sizeof ? 2018/11/05 09:14:37 (permalink)
+1 (1)
I wonder if xc32 rounds up to 32 bits ?
#2
Blue_Key
Junior Member
  • Total Posts : 112
  • Reward points : 0
  • Joined: 2011/12/20 04:48:22
  • Location: 0
  • Status: offline
Re: Is there a bug with sizeof ? 2018/11/05 09:30:16 (permalink)
0
The data seems to be 10, since I send the stream from my computer and went to check byte by byte. If I manually set 10, my crc is correct and the code works fine, If I use sizeof, it does not.
 
Spent 4 hours on that.
#3
simong123
Lab Member No. 003
  • Total Posts : 1274
  • Reward points : 0
  • Joined: 2012/02/07 18:21:03
  • Location: Future Gadget Lab (UK Branch)
  • Status: offline
Re: Is there a bug with sizeof ? 2018/11/05 09:30:49 (permalink) ☼ Best Answerby Blue_Key 2018/11/07 01:25:12
+4 (4)
Probably due to the aligned attribute you have on address.
Don''t align the typedef, just the variable(s) you define.
Try the following

#pragma pack(push,2)
typedef struct
{
    unsigned long address ;
    unsigned long len;
    unsigned short crc;
} FlashData;
#pragma pack(pop)
FlashData __attribute__((aligned)) fd;

#4
andersm
Super Member
  • Total Posts : 2478
  • Reward points : 0
  • Joined: 2012/10/07 14:57:44
  • Location: 0
  • Status: offline
Re: Is there a bug with sizeof ? 2018/11/05 10:04:40 (permalink)
+1 (1)
If "address" is supposed to be naturally aligned, the compiler must add padding to the structure.
Flashdata array[2];
assert((&array[1].address & 3) == 0);

#5
Jim Nickerson
User 452
  • Total Posts : 5458
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re: Is there a bug with sizeof ? 2018/11/05 10:07:39 (permalink)
0
simong123
Probably due to the aligned attribute you have on address.
Don''t align the typedef, just the variable(s) you define.
Try the following

 
#pragma pack(push,2)
typedef struct
{
    unsigned long address ;
    unsigned long len;
    unsigned short crc;
} FlashData;
#pragma pack(pop)
FlashData __attribute__((aligned)) fd;



Thank you very much.
I have been struggling with this pack sequence for some time due to a compiler change some time ago.
This works as expected
#6
moser
Super Member
  • Total Posts : 401
  • Reward points : 0
  • Joined: 2015/06/16 02:53:47
  • Location: Germany
  • Status: offline
Re: Is there a bug with sizeof ? 2018/11/06 10:56:35 (permalink)
+1 (1)
Simong123 is right. address has an alignment of 8:
MPLAB® XC32 C/C++ Compiler User’s Guide
If the alignment value n is omitted, the alignment of the variable is set 8 (the largest
alignment value for a basic data type).

 
And as far as I know, in general, a struct instance will have the alignment of its widest scalar member. Some sources (e.g. http://www.catb.org/esr/structure-packing/#_structure_alignment_and_padding )  say, that compilers do this as the easiest way to ensure that all the members are self-aligned for fast access. For the given struct this is the alignment of 8 and that is why the struct grows to the next multiple of 8, which is 16.
post edited by moser - 2018/11/07 02:46:07
#7
Blue_Key
Junior Member
  • Total Posts : 112
  • Reward points : 0
  • Joined: 2011/12/20 04:48:22
  • Location: 0
  • Status: offline
Re: Is there a bug with sizeof ? 2018/11/07 01:18:38 (permalink)
0
Thanks for the info!
 
Can you explain the use of 
#pragma pack(push,2) and
#pragma pack(pop) ?
#8
Jim Nickerson
User 452
  • Total Posts : 5458
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re: Is there a bug with sizeof ? 2018/11/07 07:52:49 (permalink)
0
Blue_Key
Can you explain the use of 



Google is your friend Smile: Smile
#9
moser
Super Member
  • Total Posts : 401
  • Reward points : 0
  • Joined: 2015/06/16 02:53:47
  • Location: Germany
  • Status: offline
Re: Is there a bug with sizeof ? 2018/11/08 03:04:39 (permalink)
+1 (1)
JANickersonGoogle is your friend

 
Not anymore. My new friends are two ducks, which are going! wink: wink
#10
Jim Nickerson
User 452
  • Total Posts : 5458
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re: Is there a bug with sizeof ? 2018/11/08 07:19:21 (permalink)
+1 (1)
First I have heard about Duck...
I will try to remember to say "A web search might produce results"
#11
Jump to:
© 2018 APG vNext Commercial Version 4.5