C exam
(Judge big-end and small-end mode) Question 1: Write a c function. If the processor is big_endian, 0 is returned. If little_endian is used, 1 is returned.
Answer:
Int checkcpu ()
{
{
Union W
{
Int;
Char B;
} C;
C. A = 1;
Return (C. B = 1 );
}
}
Analysis:
Embedded system developers should be familiar with the little-Endian and big-Endian modes. In little-Endian mode, the number of CPUs is stored in bytes from low to high, while in big-Endian mode, the number is stored in bytes from high to low. For example, the storage method of 16-bit 0x1234 in little-Endian mode CPU memory (assuming that it starts from address 0x4000) is:
Memory Address
Zero X 4000
Zero X 4001
Store content
0x34
0x12
In big-Endian mode, the CPU memory is stored as follows:
Memory Address
Zero X 4000
Zero X 4001
Store content
0x12
0x34
32-bit-width 0x12345678 storage method in the little-Endian mode CPU memory (assuming it starts from address 0x4000:
Memory Address
Zero X 4000
Zero X 4001
Zero X 4002
Zero X 4003
Store content
0x78
0x56
0x34
0x12
In big-Endian mode, the CPU memory is stored as follows:
Memory Address
Zero X 4000
Zero X 4001
Zero X 4002
Zero X 4003
Store content
0x12
0x34
0x56
0x78
The storage order of union Union is that all members are stored from the low address. The interviewer's answer uses this feature to easily obtain the CPU reads and writes to the memory in the little-Endian or big-Endian mode. If anyone could give this answer on the spot, it would be a talented programmer.
I. How can I determine whether the CPU is a large or a small one?
Understanding the differences between the big end and the small end is very simple to implement:
# Include <stdio. h>
Int main ()
{
Union ut {
Short S;
Char C [2];
} U;
If (sizeof (short) = 2)
{
U. S = 0x0102;
If (U. C [0] = 1 & U. C [1] = 2)
{
Printf ("Big enidan \ n ");
} Else if (U. C [0] = 2 & U. C [1] = 1)
{
Printf ("little endian. \ n ");
}
Return 0;
}
}
2. Mutual conversion between large and small ends
# Include <stdio. h>
Typdedef unsigned int u_32;
Typedef unsigned short u_16;
# Define bswap_16 (x )\
() (X) & 0x00ff) <8) | \
() (X) & 0xff00)> 8 )\
)
# Define bswap_32 (x )\
(U32) (u32) (x) & 0xff000000)> 24) | \
(U32) (x) & 0x00ff0000)> 8) | \
(U32) (x) & 0x0000ff00) <8) | \
(U32) (x) & 0x000000ff) <24 )\
)
162bswap16 (162xx)
{
Return (X & 0x00ff) <8 |
(X & 0xff00)> 8
;
}
U32 bswap32 (u32 X)
{
Return (X & 0xff000000)> 24 |
(X & 0x00ff0000)> 8 |
(X & 0x0000ff00) <8 |
(X & 0x000000ff) <24
;
}
Int main (void)
{
// The key is to be able to handle the error. If 0x123490 is given, the 0x9034 value can still be obtained, and the memory size is small.
Printf ("macro conversion: % # x \ n", bswap_16 (0x123490); // Correct Conversion
Printf ("macro conversion: % # x \ n", bswap_32 (0x1234567890); // Correct Conversion
Printf ("----------------- \ n ");
Printf ("Function Conversion: % # x \ n", bswap16 (0x123490 ));
Printf ("Function Conversion: % # x \ n", bswap32 (0x1234567890 ));
Return 0;
}
The following is a post on the big end and Small End Excerpted from the Internet.
Big-Endian and little-Endian (reprinted)
Big-Endian and little-Endian are terms used to describe the byte sequences stored in computer memory.
Big-Endian is a sequence in which one end of a sequence (a more typical value in the sequence) exists (in the smallest storage address. Little-Endian is an end of a small value (a less typical value in the sequence) stored in the previous order. For example, in the big-Endian computer, two bytes are required to store the 16-digit 4f52 as 4f52 in the memory (if 4f exists in storage address 1000, for example, 52 will exist in 1001 ). In the little-Endian system, it will be stored as 524f (52 exists in storage address 1000, for example, 4f will exist in 1001 ).
Most of IBM's 370 hosts are based on the server-defined Identity Management (PMP) computers, and Motorola's microprocessor uses the big-Endian method. TCP/IP also uses the big-Endian method (so the big-Endian method is also called Network encoding ). For people, our language is a habit from left to right. This seems to be a natural way to store characters and numbers-you also want to appear in front of you in the same way. Many of us think that big-Endian is a popular storage method, as we have read. On the other hand, Intel processor (CPUs), Dec Alphas, and at least some other programs on their platform are of little-Endian.
The argument about little-Endian is that if you increase the value of a number, you may add a number on the left (more numbers are required for high non-exponential functions ). Therefore, it is often necessary to add two digits and move all numbers in the big-Endian sequence in the memory to the right. The unimportant bytes in the memory using little-Endian can exist in its original location, and the new number can exist in the high address on its right. This means that the operation of some computers can become simple and fast.
Language compilers, such as Java or Fortran, must specify the storage method used by the target code they develop. If necessary, you can use a converter to convert the storage sequence.
Note that in the byte sequence of big-Endian and little-Endian, big-Endian is used in a single bit. That is to say, the given number represented by a single string in the stored bytes is not big-Endian or little-Endian. For example, if there is a sixteen-digit 4f placed before or after a given byte geology, the order in this byte is:
01001111
From the order of this bit, it may be big-Endian or little-Endian, but CPUs and programs are almost all designed as big-Endian order. Even so, in the exchange of data, it may be an order.
Eric Raymond commented that the Internet domain name address and e-mail address are little-Endian. For example, we use big-Endian to express our website as com. Whatis. www.
Big-Endian and little-Endian are from Jonathan Swift's Gulliver's Travels. Big-Endian is the big egg that breaks the big-headed political faction, the opposition Lilliputian King asked his country to use the small one to beat the eggs.
Currently, most storage devices use byte as the smallest unit for access. When a logical sorting must be divided into several physical units, there is a problem of who puts first and then who puts it, therefore, the endian issue emerged. For different storage methods, there are two descriptions: Big-Endian and little-Endian. (These two terms come from Jonathan Swift's "garifo Travelogue". The two factions involved in the battle cannot reach an agreement on which end -- small end or big end -- should open a half-cooked egg. In that era, Swift was a constant conflict between Britain and France. Danny Cohen, an early pioneer in network protocols, used these two terms for the first time to refer to the byte sequence, later, this term was widely accepted .)
There is "It doesn't matter if we say" it's not related to word or word length ". Suppose there is a data file with n most ordered la S. If we want to read data in a certain area of memory in little-Endian format, so how should I read it? How to arrange ?" This problem is caused by the deviation in the substantive understanding of endian. endian refers to the logical unit arrangement relationship between the minimum physical unit and the minimum logical unit hour. Here, "there is a data file with n most ordered columns", and this "one data" is obviously not the smallest unit in logic, one of the "N majority" is the smallest logical unit. Therefore, the Principle Arrangement in the main table can be applied, the order between "n majority" is determined by the "n majority" host, such as the program you write. This order is determined by you.
As mentioned just now, endian refers to the logical unit arrangement relationship between the minimum physical unit and the minimum logical unit hour. The minimum physical unit we come into contact with is byte. In the field of communication, it is often bit, but the principle is similar.
Practice can give you more experience. For example, in the communication protocol of an embedded system, the protocol stack from the underlying RF driver to the upper layer needs to be implemented, there may be multiple endian problems. The bit order at the underlying layer, the byte order at the protocol layer, and the byte order at the application layer are all different concepts.
Big-Endian and little-Endian byte sorting
Meaning of byte sorting
The high byte in a word in big-Endian is placed at the low address in the word area in the memory.
A low byte in a word in little-Endian is placed at the low address in the word area in the memory.
Note that the length of a word in a table is 16 bits and that of a byte is 8 bits. If a number exceeds the length of a word, it must first be divided into several parts by word, and then each part (that is, each word is internal) use the big-Endian or little-Endian operations to process bytes.
Example:
If we write 0x1234abcd to the memory starting with 0x0000, the result is
Big-Endian little-Endian
0x0000 0x12 0xcd
0x0001 0x34 0xab
0x0002 0xab 0x34
0x0003 0xcd 0x12
Apart from the bigk series of Moto and the big endian series of Dec, the common CPUs are little endian. Arm supports both big and little, and usually uses little endian in practical applications.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/flickedball/archive/2009/04/21/4096991.aspx
2.
Char STR [] = "hello ";
Char * P = STR;
Int n = 10;
Calculate
Sizeof (STR) = 6 (2 points)
Sizeof (p) = 4 (2 points)
Sizeof (n) = 4 (2 points)
Void func (char STR [1, 100])
{
Calculate
Sizeof (STR) = 4 (2 points)
}
Void * P = malloc (100 );
Calculate
Sizeof (p) = 4 (2 points)
3. Why should I add the extern "C" to the function called by the C compiler after being compiled in the C ++ program "? (5 points)
A: function Overloading is supported in C ++ and function Overloading is not supported in C. The name of the function in the library after being compiled by C ++ is different from that in C language. Assume that the prototype of a function is void Foo (int x, int Y );
After the function is compiled by the C compiler, its name in the library is _ Foo, while the C ++ compiler generates names such as _ foo_int_int.
C ++ provides a C connection exchange with the specified symbol extern "C" to solve the name matching problem.
4. Questions about memory
Void getmemory (char * P)
{
P = (char *) malloc (100 );
}
Void test (void)
{
Char * STR = NULL;
Getmemory (STR );
Strcpy (STR, "Hello World ");
Printf (STR );
}
What are the results of running the test function?
A: The program crashes.
Because getmemory cannot transmit dynamic memory,
STR in the test function is always null.
Strcpy (STR, "Hello World"); will crash the program.
Char * getmemory (void)
{
Char P [] = "Hello World ";
Return P;
}
Void test (void)
{
Char * STR = NULL;
STR = getmemory ();
Printf (STR );
}
What are the results of running the test function?
A: It may be garbled.
Because getmemory returns a pointer to the "stack memory", the pointer address is not null, but its original content has been cleared, and the new content is unknown.
Void getmemory2 (char ** P, int num)
{
* P = (char *) malloc (Num );
}
Void test (void)
{
Char * STR = NULL;
Getmemory (& STR, 100 );
Strcpy (STR, "hello ");
Printf (STR );
}
What are the results of running the test function?
A:
(1) Output hello
(2) Memory leakage
Void test (void)
{
Char * STR = (char *) malloc (100 );
Strcpy (STR, "hello ");
Free (STR );
If (STR! = NULL)
{
Strcpy (STR, "World ");
Printf (STR );
}
}
What are the results of running the test function?
A: Tampering with content in the dynamic memory area is unpredictable and dangerous.
Because free (STR); after that, STR becomes a wild pointer,
If (STR! = NULL) the statement does not work.
C exam
(Judge big-end and small-end mode) Question 1: Write a c function. If the processor is big_endian, 0 is returned. If little_endian is used, 1 is returned.
Answer:
Int checkcpu ()
{
{
Union W
{
Int;
Char B;
} C;
C. A = 1;
Return (C. B = 1 );
}
}
Analysis:
Embedded system developers should be familiar with the little-Endian and big-Endian modes. In little-Endian mode, the number of CPUs is stored in bytes from low to high, while in big-Endian mode, the number is stored in bytes from high to low. For example, the storage method of 16-bit 0x1234 in little-Endian mode CPU memory (assuming that it starts from address 0x4000) is:
Memory Address
Zero X 4000
Zero X 4001
Store content
0x34
0x12
In big-Endian mode, the CPU memory is stored as follows:
Memory Address
Zero X 4000
Zero X 4001
Store content
0x12
0x34
32-bit-width 0x12345678 storage method in the little-Endian mode CPU memory (assuming it starts from address 0x4000:
Memory Address
Zero X 4000
Zero X 4001
Zero X 4002
Zero X 4003
Store content
0x78
0x56
0x34
0x12
In big-Endian mode, the CPU memory is stored as follows:
Memory Address
Zero X 4000
Zero X 4001
Zero X 4002
Zero X 4003
Store content
0x12
0x34
0x56
0x78
The storage order of union Union is that all members are stored from the low address. The interviewer's answer uses this feature to easily obtain the CPU reads and writes to the memory in the little-Endian or big-Endian mode. If anyone could give this answer on the spot, it would be a talented programmer.
I. How can I determine whether the CPU is a large or a small one?
Understanding the differences between the big end and the small end is very simple to implement:
# Include <stdio. h>
Int main ()
{
Union ut {
Short S;
Char C [2];
} U;
If (sizeof (short) = 2)
{
U. S = 0x0102;
If (U. C [0] = 1 & U. C [1] = 2)
{
Printf ("Big enidan \ n ");
} Else if (U. C [0] = 2 & U. C [1] = 1)
{
Printf ("little endian. \ n ");
}
Return 0;
}
}
2. Mutual conversion between large and small ends
# Include <stdio. h>
Typdedef unsigned int u_32;
Typedef unsigned short u_16;
# Define bswap_16 (x )\
() (X) & 0x00ff) <8) | \
() (X) & 0xff00)> 8 )\
)
# Define bswap_32 (x )\
(U32) (u32) (x) & 0xff000000)> 24) | \
(U32) (x) & 0x00ff0000)> 8) | \
(U32) (x) & 0x0000ff00) <8) | \
(U32) (x) & 0x000000ff) <24 )\
)
162bswap16 (162xx)
{
Return (X & 0x00ff) <8 |
(X & 0xff00)> 8
;
}
U32 bswap32 (u32 X)
{
Return (X & 0xff000000)> 24 |
(X & 0x00ff0000)> 8 |
(X & 0x0000ff00) <8 |
(X & 0x000000ff) <24
;
}
Int main (void)
{
// The key is to be able to handle the error. If 0x123490 is given, the 0x9034 value can still be obtained, and the memory size is small.
Printf ("macro conversion: % # x \ n", bswap_16 (0x123490); // Correct Conversion
Printf ("macro conversion: % # x \ n", bswap_32 (0x1234567890); // Correct Conversion
Printf ("----------------- \ n ");
Printf ("Function Conversion: % # x \ n", bswap16 (0x123490 ));
Printf ("Function Conversion: % # x \ n", bswap32 (0x1234567890 ));
Return 0;
}
The following is a post on the big end and Small End Excerpted from the Internet.
Big-Endian and little-Endian (reprinted)
Big-Endian and little-Endian are terms used to describe the byte sequences stored in computer memory.
Big-Endian is a sequence in which one end of a sequence (a more typical value in the sequence) exists (in the smallest storage address. Little-Endian is an end of a small value (a less typical value in the sequence) stored in the previous order. For example, in the big-Endian computer, two bytes are required to store the 16-digit 4f52 as 4f52 in the memory (if 4f exists in storage address 1000, for example, 52 will exist in 1001 ). In the little-Endian system, it will be stored as 524f (52 exists in storage address 1000, for example, 4f will exist in 1001 ).
Most of IBM's 370 hosts are based on the server-defined Identity Management (PMP) computers, and Motorola's microprocessor uses the big-Endian method. TCP/IP also uses the big-Endian method (so the big-Endian method is also called Network encoding ). For people, our language is a habit from left to right. This seems to be a natural way to store characters and numbers-you also want to appear in front of you in the same way. Many of us think that big-Endian is a popular storage method, as we have read. On the other hand, Intel processor (CPUs), Dec Alphas, and at least some other programs on their platform are of little-Endian.
The argument about little-Endian is that if you increase the value of a number, you may add a number on the left (more numbers are required for high non-exponential functions ). Therefore, it is often necessary to add two digits and move all numbers in the big-Endian sequence in the memory to the right. The unimportant bytes in the memory using little-Endian can exist in its original location, and the new number can exist in the high address on its right. This means that the operation of some computers can become simple and fast.
Language compilers, such as Java or Fortran, must specify the storage method used by the target code they develop. If necessary, you can use a converter to convert the storage sequence.
Note that in the byte sequence of big-Endian and little-Endian, big-Endian is used in a single bit. That is to say, the given number represented by a single string in the stored bytes is not big-Endian or little-Endian. For example, if there is a sixteen-digit 4f placed before or after a given byte geology, the order in this byte is:
01001111
From the order of this bit, it may be big-Endian or little-Endian, but CPUs and programs are almost all designed as big-Endian order. Even so, in the exchange of data, it may be an order.
Eric Raymond commented that the Internet domain name address and e-mail address are little-Endian. For example, we use big-Endian to express our website as com. Whatis. www.
Big-Endian and little-Endian are from Jonathan Swift's Gulliver's Travels. Big-Endian is the big egg that breaks the big-headed political faction, the opposition Lilliputian King asked his country to use the small one to beat the eggs.
Currently, most storage devices use byte as the smallest unit for access. When a logical sorting must be divided into several physical units, there is a problem of who puts first and then who puts it, therefore, the endian issue emerged. For different storage methods, there are two descriptions: Big-Endian and little-Endian. (These two terms come from Jonathan Swift's "garifo Travelogue". The two factions involved in the battle cannot reach an agreement on which end -- small end or big end -- should open a half-cooked egg. In that era, Swift was a constant conflict between Britain and France. Danny Cohen, an early pioneer in network protocols, used these two terms for the first time to refer to the byte sequence, later, this term was widely accepted .)
There is "It doesn't matter if we say" it's not related to word or word length ". Suppose there is a data file with n most ordered la S. If we want to read data in a certain area of memory in little-Endian format, so how should I read it? How to arrange ?" This problem is caused by the deviation in the substantive understanding of endian. endian refers to the logical unit arrangement relationship between the minimum physical unit and the minimum logical unit hour. Here, "there is a data file with n most ordered columns", and this "one data" is obviously not the smallest unit in logic, one of the "N majority" is the smallest logical unit. Therefore, the Principle Arrangement in the main table can be applied, the order between "n majority" is determined by the "n majority" host, such as the program you write. This order is determined by you.
As mentioned just now, endian refers to the logical unit arrangement relationship between the minimum physical unit and the minimum logical unit hour. The minimum physical unit we come into contact with is byte. In the field of communication, it is often bit, but the principle is similar.
Practice can give you more experience. For example, in the communication protocol of an embedded system, the protocol stack from the underlying RF driver to the upper layer needs to be implemented, there may be multiple endian problems. The bit order at the underlying layer, the byte order at the protocol layer, and the byte order at the application layer are all different concepts.
Big-Endian and little-Endian byte sorting
Meaning of byte sorting
The high byte in a word in big-Endian is placed at the low address in the word area in the memory.
A low byte in a word in little-Endian is placed at the low address in the word area in the memory.
Note that the length of a word in a table is 16 bits and that of a byte is 8 bits. If a number exceeds the length of a word, it must first be divided into several parts by word, and then each part (that is, each word is internal) use the big-Endian or little-Endian operations to process bytes.
Example:
If we write 0x1234abcd to the memory starting with 0x0000, the result is
Big-Endian little-Endian
0x0000 0x12 0xcd
0x0001 0x34 0xab
0x0002 0xab 0x34
0x0003 0xcd 0x12
Apart from the bigk series of Moto and the big endian series of Dec, the common CPUs are little endian. Arm supports both big and little, and usually uses little endian in practical applications.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/flickedball/archive/2009/04/21/4096991.aspx
2.
Char STR [] = "hello ";
Char * P = STR;
Int n = 10;
Calculate
Sizeof (STR) = 6 (2 points)
Sizeof (p) = 4 (2 points)
Sizeof (n) = 4 (2 points)
Void func (char STR [1, 100])
{
Calculate
Sizeof (STR) = 4 (2 points)
}
Void * P = malloc (100 );
Calculate
Sizeof (p) = 4 (2 points)
3. Why should I add the extern "C" to the function called by the C compiler after being compiled in the C ++ program "? (5 points)
A: function Overloading is supported in C ++ and function Overloading is not supported in C. The name of the function in the library after being compiled by C ++ is different from that in C language. Assume that the prototype of a function is void Foo (int x, int Y );
After the function is compiled by the C compiler, its name in the library is _ Foo, while the C ++ compiler generates names such as _ foo_int_int.
C ++ provides a C connection exchange with the specified symbol extern "C" to solve the name matching problem.
4. Questions about memory
Void getmemory (char * P)
{
P = (char *) malloc (100 );
}
Void test (void)
{
Char * STR = NULL;
Getmemory (STR );
Strcpy (STR, "Hello World ");
Printf (STR );
}
What are the results of running the test function?
A: The program crashes.
Because getmemory cannot transmit dynamic memory,
STR in the test function is always null.
Strcpy (STR, "Hello World"); will crash the program.
Char * getmemory (void)
{
Char P [] = "Hello World ";
Return P;
}
Void test (void)
{
Char * STR = NULL;
STR = getmemory ();
Printf (STR );
}
What are the results of running the test function?
A: It may be garbled.
Because getmemory returns a pointer to the "stack memory", the pointer address is not null, but its original content has been cleared, and the new content is unknown.
Void getmemory2 (char ** P, int num)
{
* P = (char *) malloc (Num );
}
Void test (void)
{
Char * STR = NULL;
Getmemory (& STR, 100 );
Strcpy (STR, "hello ");
Printf (STR );
}
What are the results of running the test function?
A:
(1) Output hello
(2) Memory leakage
Void test (void)
{
Char * STR = (char *) malloc (100 );
Strcpy (STR, "hello ");
Free (STR );
If (STR! = NULL)
{
Strcpy (STR, "World ");
Printf (STR );
}
}
What are the results of running the test function?
A: Tampering with content in the dynamic memory area is unpredictable and dangerous.
Because free (STR); after that, STR becomes a wild pointer,
If (STR! = NULL) the statement does not work.