Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Answering your questions as posed (ignoring your very nice picture of the structure)</p> <blockquote> <p>It's like wild guess but main rule for struct is to define variables with bigger size at the beginning?</p> </blockquote> <p>Always put the stuff that requires the most alignment first. I wouldn't put a <code>char[99]</code> first for instance. In general this works out as pointers, 64 bit native types, 32 bit native types, etc, but you have to be very careful if your structure contains members that are other structures.</p> <blockquote> <p>As I understand it's not working with stand-alone variables. Like <code>char str[] = "Hello";</code></p> </blockquote> <p>I don't really understand this. If you define a char array on the stack, it has char alignment. If you define a char array followed by an int, there'll probably be padding on the stack, you just cant find it.</p> <blockquote> <p>Padding byte, what code it has? Is it somewhere at ASCII table? Sorry, couldn't find it.</p> </blockquote> <p>It has neither code nor data. It is compiler inserted padding and may contain any value, which may or may not be different between different instances of the structure in same or different runs of the program.</p> <blockquote> <p>2 structures with all members represented at memory by different addresses and they can be placed not sequentially at memory?</p> </blockquote> <p>I do not understand this. Are you asking if the compiler can insert padding between structures? If not, please clarify, because this answer will not be much help;</p> <p>When the compiler creates a structure, it has to make it possible for you to sanely create an array of such structures. Consider this:</p> <pre><code>struct S { int wibble; char wobble; }; S stuff[2]; </code></pre> <p>If the compiler doesn't insert 3 bytes of padding after wobble, accesses to <code>stuff[1].wobble</code> won't be aligned properly, which will result in crashes on some hardware (and atrocious performance on other hardware). Basically, the compiler has to ensure padding at the end to ensure that the most aligned member of the structure is always correctly aligned for an array of such structures.</p> <blockquote> <p>Such structure: <code>struct ST3 { char c1; char c2; char c3;} st3;</code> Has size = 3, I understand that if we will add a member with other type into it, it will be aligned. But why it's not aligned before it?</p> </blockquote> <p>Do you mean 'Why doesn't the compiler put it in a place where it's correctly aligned'? Because the language doesn't let it. The compiler isn't allowed to reorder the members of your structure. It is only allowed to insert padding.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload