Thread: Unexpected value of sizeof(struct)

  1. #1
    Registered User bremenpl's Avatar
    Join Date
    Apr 2013
    Posts
    57

    Angry Unexpected value of sizeof(struct)

    Hello there,
    Recently i have encouraged a bug i cannot understand. When taking sizeof my structure, i am getting a result that is 1 greater than expected. For some reason, it is calculating size of unsigned short as 3 instead of 2 (but only when calculating whole struct, sizeof(short) shows 2 as supposed to). Here i am pasing a link to the code compiled on ideaone so you can see for yoursalfe (for the beggining i thought it is my compiler).

    http://ideone.com/lAYwUY

    The sizeof struct is 176 when it is supposed to be 175. in the code when i try to memcpy this struct to a table of bytes i get a 0 just before the last uint16_t variable. It is not visible when checking the struct in debug mode though, everything is aligned well.

    I would aprichiate any help here.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I haven't bothered to look at your code, but the answer is simple.

    The difference between sizeof(any struct type) and the sum of sizeof(members of that struct type) is due to padding between members.

    Have a look here for a more detailed explanation.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User bremenpl's Avatar
    Join Date
    Apr 2013
    Posts
    57
    I see, thank you for the link.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-06-2012, 04:44 PM
  2. sizeof a struct
    By stanlvw in forum C++ Programming
    Replies: 7
    Last Post: 04-09-2009, 01:58 PM
  3. Why sizeof() this struct is 12 ?
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 04-08-2008, 04:23 AM
  4. sizeof struct
    By k_w_s_t_a_s in forum C Programming
    Replies: 9
    Last Post: 05-29-2003, 04:36 PM
  5. sizeof struct
    By k_w_s_t_a_s in forum C++ Programming
    Replies: 3
    Last Post: 05-11-2003, 12:05 PM

Tags for this Thread