CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2017
    Location
    Greece
    Posts
    130

    Why changing the order of struct members can change the size of it?

    I remember when I was learning assembly as a colleague (I still am by the way), in a programming lab the teachers showed as that if you have a struct in c and then see the size , change the order of the members and see the size again you might notice a different value. They said this was something to do about linear organization of the memory or something and that computers like to organize the memory in even numbers (which are divided by 2) but I can't remember actually why that was.

    Can someone explain it to me or give me a link where i can read the reason?
    Also is this something that i might be worried about as a programmer?

    Thanks!

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Why changing the order of struct members can change the size of it?

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jul 2017
    Location
    Greece
    Posts
    130

    Re: Why changing the order of struct members can change the size of it?

    Quote Originally Posted by 2kaud View Post
    I remembered now. The thing is, as a programmer should I worry about that? Using the sizeof() function I can always get the correct size of a struct or class and also using the macro offsetof(st, m) for structures i can also easily take the offset of each member, so i should not worry about the alignment i guess.

    I'm asking these question because lately I'm dealing with low lever programming (Graphics Programming) and I'm playing a lot with raw bytes, so i just want to make sure I understand everything correctly.

  4. #4
    Join Date
    Feb 2017
    Posts
    677

    Re: Why changing the order of struct members can change the size of it?

    Quote Originally Posted by babaliaris View Post
    I'm asking these question because lately I'm dealing with low lever programming (Graphics Programming) and I'm playing a lot with raw bytes, so i just want to make sure I understand everything correctly.
    Well, you need to pack data according to how the Graphics subsystem expects to find it. It means you need to know enough about the programming language and the tools you're using to accomplish the expected data layouts. There is no way around it when you have systems that communicate at a low level by passing addresses to blocks of memory in order to exchange information.

    What you need is a good tutorial and lots of examples. I've recently used DirectX 12 to display movable objects in a 3D scene. I could never have done it without the help of the book by Frank D. Luna and even so it was a daunting task.
    Last edited by wolle; October 27th, 2018 at 03:16 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured