Home > Back-end >  Byte aligned with a domain
Byte aligned with a domain

Time:09-27

The problem is this:

There is a structure DateTime:
Struct DateTime
{
Unsigned int year;
Unsigned int the month: 4;
Unsigned int day: 5;
Unsigned int hour: 5;
Unsigned int minute: 6
Unsigned int seconde: 6;
} a DateTime.

According to the books on the "law of byte alignment," I am so calculate its size:

4 byte + (4 + 5 + 5 + 6 + 6) 32 + 26=58 * * bit=bit

7 * * 8 bit=56 & lt; 58 & lt; 8 * 8=64 * bite -- -- -- -- -- - & gt; Get 8 byte.

So the sizeof () calculated for 8,
Printed in the xocde indeed,


Involves the anonymous bit-field is as follows:
Struct DateTime
{
Unsigned int year;
Unsigned int the month: 4;
Unsigned int day: 5;
Unsigned int: 0;
Unsigned int hour: 5;
Unsigned int minute: 6
Unsigned int seconde: 6;
} a DateTime.



Books on said: length of 0 bit-field members, forced by a member from a complete machine word (word), began to distribution,

Xcode sizeof calculated at this time on the same size and account books: for 12.



My question is, how to calculate the 12; Because my idea is:
The first thought:
Anonymous above a domain analysis:

4 * byte + (4 + 5) * bit=32 + 9=41 * bit;

With the above rules should be 6 byte.

Anonymous below:

A total of 17 * bit should get 3 byte.

So should be 6 + 3=9 * byte; .

The second idea:
If the "length of 0 bit-field members, forcing a member from a complete machine word (word), began to assign" understand this sentence is:

Anonymous bit-field members of less than 64 * bit above make up for the 64 * bite, so the end result should be 8 + 3=11 * byte.





-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --




Both ideas are wrong, the correct solution is?



CodePudding user response:

Struct DateTime
{
Unsigned int year;
Unsigned int the month: 4;
Unsigned int day: 5;
Unsigned int: 0;
Unsigned int hour: 5;
Unsigned int minute: 6
Unsigned int seconde: 6;
} a DateTime.
Your book knowledge are older, 32-bit machine and you said was right not int on the 32 bit that is what you said 4 byte, hardware knowledge from the corresponding hardware environment is not clear

CodePudding user response:



reference 1st floor gouyanfen response:
struct DateTime
{
Unsigned int year;
Unsigned int the month: 4;
Unsigned int day: 5;
Unsigned int: 0;
Unsigned int hour: 5;
Unsigned int minute: 6
Unsigned int seconde: 6;
} a DateTime.
Your book knowledge are older, 32-bit machine and you said was right not int on the 32 bit that is what you said 4 byte, hardware knowledge left says to not be clear about the corresponding hardware environment



Oh, I this should be a 64 - bit machines - (2.3 GHz dual-core Intel Core i5, Mac OS catalina); Calculation results of type int is 4 (sizeof),

CodePudding user response:

reference 1st floor gouyanfen response:
struct DateTime
{
Unsigned int year;
Unsigned int the month: 4;
Unsigned int day: 5;
Unsigned int: 0;
Unsigned int hour: 5;
Unsigned int minute: 6
Unsigned int seconde: 6;
} a DateTime.
Your book knowledge are older, 32-bit machine and you said was right not int on the 32 bit that is what you said 4 byte, hardware knowledge left says to not be clear about the corresponding hardware environment



I am using Xcode

CodePudding user response:

reference intmainfunc1 reply: 3/f
Quote: refer to 1st floor gouyanfen response:

Struct DateTime
{
Unsigned int year;
Unsigned int the month: 4;
Unsigned int day: 5;
Unsigned int: 0;
Unsigned int hour: 5;
Unsigned int minute: 6
Unsigned int seconde: 6;
} a DateTime.
Your book knowledge are older, 32-bit machine and you said was right not int on the 32 bit that is what you said 4 byte, hardware knowledge left says to not be clear about the corresponding hardware environment



I am using Xcode

64 - bit machine, you run a 32-bit application, he is calculated according to 32 bit, so the int is a 4 byte 32 bit

CodePudding user response:

reference 4 floor gouyanfen response:
Quote: reference intmainfunc1 reply: 3/f

Quote: refer to 1st floor gouyanfen response:

Struct DateTime
{
Unsigned int year;
Unsigned int the month: 4;
Unsigned int day: 5;
Unsigned int: 0;
Unsigned int hour: 5;
Unsigned int minute: 6
Unsigned int seconde: 6;
} a DateTime.
Your book knowledge are older, 32-bit machine and you said was right not int on the 32 bit that is what you said 4 byte, hardware knowledge left says to not be clear about the corresponding hardware environment



I am using Xcode

64 - bit machine, you run a 32-bit application, he is calculated according to the 32-bit, so int is 4 byte 32 bit

And, if the default structure realize automatic byte alignment, because of the addressing, if you actually specify address space does not meet the need of automatic adjustment

CodePudding user response:

reference 4 floor gouyanfen response:
Quote: reference intmainfunc1 reply: 3/f

Quote: refer to 1st floor gouyanfen response:

Struct DateTime
{
Unsigned int year;
Unsigned int the month: 4;
Unsigned int day: 5;
Unsigned int: 0;
Unsigned int hour: 5;
Unsigned int minute: 6
Unsigned int seconde: 6;
} a DateTime.
Your book knowledge are older, 32-bit machine and you said was right not int on the 32 bit that is what you said 4 byte, hardware knowledge left says to not be clear about the corresponding hardware environment



I am using Xcode

64 - bit machine, you run a 32-bit application, he is calculated according to the 32-bit, so int is 4 byte 32 bit


Well, yes, as you say, int is calculated according to the 4, I also is, indeed, a 64 - bit machine, in the case of a 64 - bit, in t is calculated according to the 4 byte, this is a fact,,,,,,,,,,,,,,

But I ask the question, I calculated, also indeed is the int according to four to consider, in line with the actual, but I just don't understand is how to get 12,,,,nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related