Big-end mode, small-end Mode

Source: Internet
Author: User

Big end Mode, RefersThe low data level (that is, the few digits after the smaller values) is stored in the high memory address, while the high data level is stored in the low memory address.Such a storage mode is similar to processing data as strings: The address increases from small to large, and the data is placed from high to low;
Small-end Mode, RefersThe low data level is stored in the low memory address, while the high data level is stored in the high memory address.This storage mode effectively combines the high and low addresses with the data bit permissions. The high address part has a high weight, and the low address part has a low weight, which is consistent with our logic method.
Why are there differences between large and small models? This is because in computer systems, we use bytes. Each address unit corresponds to one byte, and each byte is 8 bit. However, in C language, apart from 8-bit char, there are 16-bit short and 32-bit long (depending on the specific compiler). In addition, for a processor whose digits are greater than 8 bits, for example, for a 16-bit or 32-bit processor, because the register width is greater than one byte, there must be a problem if multiple bytes are arranged. Therefore, the large-end storage mode and the Small-end storage mode are created.For example, if the address of a 16-bit short X in the memory is 0x0010 and the value of X is 0x1122, 0x11 is a high byte and 0x22 is a low byte. In the big-end mode, place 0x11 in the low address, that is, 0x0010, and 0x22 in the high address, that is, 0x0011. The opposite is the small-end mode.. Our commonly used x86 structure is a small-end model.
While Keil C51 is the big-end mode. Many arm and DSP are in small-end mode. Some arm processors can also choose big-end mode or small-end mode from hardware.
The following code can be usedTest whether your compiler is a large-end mode or a small-end mode: 

Short int X;
Char x0, x1;
X = 0x1122;
X0 =(Char *) & X) [0]; // Low address Unit
X1 =(Char *) & X) [1]; // High address Unit
If X0 = 0x11, it is the big end. If X0 = 0x22, it is the small end... the above program can also be seen,Data addressing uses low-byte addresses.

 

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

C language interview questions

 

Question 1: Write a c function. If the processor is big_endian, 0 is returned. If the processor is little_endian, 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 order in which union members are stored is that all members are stored from low addresses.By using this feature, the interviewer can easily read and write the memory in little-Endian or big-Endian mode. If anyone could give this answer on the spot, it would be a talented programmer.

 

2.

Char STR [] = "hello ";

Char * P = STR;

Int n = 10;

Calculate

Sizeof (STR) = 6

Sizeof (p) = 4

Sizeof (n) = 4

Void func (char STR [1, 100])

{

Calculate

Sizeof (STR) = 4

}

Void * P = malloc (100 );

Calculate

Sizeof (p) = 4

Test in TC by yourself:Char STR [100] = {'1', '2', '3', '4', '5', '6',}; printf ("sizeof (STR) = % d/N ", sizeof (STR); =" 100 printf ("sizeof (STR [100]) = % d/N ", sizeof (STR [100]); = "1 void func (char STR [100]) {printf (" sizeof (STR) = % d/N ", sizeof (STR); = "2 [related to compiler] printf (" sizeof (STR [100]) = % d/N ", sizeof (STR [100]); = "1} Main (INT argc, char * argv []) {char STR [100] = {'1 ', '2', '3', '4', '5', '6',}; func (STR );}

 

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:C ++ supports function overloading, while C does not. 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, the C ++ compiler generates names such as _ foo_int_int.C ++ provides the C connection exchange 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. BecauseGetmemory does not support dynamic memory. Str in the test function is always null.

Strcpy (STR, "Hello World ");ProgramCrash.

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. BecauseGetmemory returns a pointer to "stack memory". The address of this pointer 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.. BecauseFree (STR); then, STR becomes a wild pointer,If (STR! = NULL) the statement does not work. 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.