Miscellaneous on Programming

Source: Internet
Author: User

Byte order

Byte sequence refers to the storage order of data that occupies more than one byte in the memory. Generally, there are two types of byte sequence: small and large.Small-end byte orderLow-byte data is stored in the memory address and high-byte data is stored in the internal address;Large-end byte orderHigh-byte data is stored at the low address, and low-byte data is stored at the high address.

The PC based on the X86 platform is in the small byte order, while some embedded platforms are in the large byte order. Therefore, for int, uint16, uint32, and other data of more than 1 byte type, the storage sequence should be changed on these embedded platforms. We usually think that the byte order transmitted in the air isNetwork byte orderFor the Standard Order, considering the consistency with the Protocol and the interconnectivity with other similar platform productsHost byte orderTo the network byte order, and to the host byte order at the receiving data packet.

16-bit conversion functions: ntohs and htons
   Ntohs(Network to host short) is to convert the network byte order to the host byte order. The returned value is a 16-bit integer, that is, a two-byte integer short Int. You can also write uint16_t.
   Htons(Host to network short) is to convert the host byte order to the network byte order, and the return value is also a 16-bit integer short Int.

32-bit conversion functions ntohl and htonl.
  Ntohl(Network to host long) is to convert the network byte order to the host byte order. The returned value is a 16-bit integer, that is, an integer of two bytes long Int. You can also write uint32_t.
  Htonl(Host to network long) is to convert the host byte sequence to the network byte sequence, and the return value is also a 16-bit integer long Int.

  Inet_addrThe function can convert an IP address in dotted-decimal format to a long integer in bytes of the network.Inet_ntoaThe function converts the IP address of the long integer decimal value in the byte sequence of the network to the point-to-decimal value.

 

Heap Stack)

In the computer field, stack is a concept that cannot be ignored, and stack is two data structures. A stack is a data structure in which data items are arranged in order. Data items can be inserted and deleted only at one end (called the top.

Stack (operating system): the compiler automatically allocates and releases the stack, stores function parameter values, and local variable values. The operation method is similar to the stack in the data structure. The stack uses a level-1 cache, which is usually in a bucket When called, and is immediately released after the call is completed. Heap (operating system): Generally, it is assigned and released by the programmer. If the programmer does not release the program, it may be recycled by the OS at the end of the program. The allocation method is similar to the linked list. The heap is stored in the second-level cache. The life cycle is determined by the garbage collection algorithm of the Virtual Machine (not to be recycled once an orphan object ). Therefore, the call speed of these objects is relatively low. Heap (Data Structure): queues are prioritized. The heap can be considered as a tree. For example, the heap sorting stack (Data Structure) is first-in/last-out) the memory occupied by a C/C ++ compiled program is divided into the following parts:
  1. Stack Zone-- The compiler automatically assigns release, stores the function parameter name, and the local variable name, which is faster, but cannot be controlled by programmers. In Windows, the stack is a data structure extended to a low address and a continuous memory area. As long as the remaining stack space is larger than the requested space, the system will provide the program with memory. Otherwise, an exception will be reported, prompting stack overflow.
  2. Heap Area-- Release is distributed by programmers, which is generally slow and prone to memory fragments. If the programmer does not release the program, it may be recycled by the OS at the end of the program. The heap is a data structure extended to the high address and a non-sequential memory area. This is because the system uses the linked list to store the idle memory address, which is naturally discontinuous, And the traversal direction of the linked list is from the low address to the high address. The heap size is limited by the valid virtual memory in the computer system. It can be seen that the space obtained by the heap is flexible and large.
  3. Global zone (static zone)-- The storage of global variables and static variables is put together, and the initialized global variables and static variables are in one area, uninitialized global variables and uninitialized static variables are in another adjacent area. The program is released by the system.
  4. Text Constant Area-- The constant string is placed here, and is released by the system after the program ends.
  5. Code Area-- Stores the binary code of the function body.
// Main. cppint a = 0; // The Global initialization zone char * P1; // The Global uninitialized Zone Main () {int B; // stack chars [] = "ABC "; // stack char * P2; // stack char * P3 = "123456"; // 123456 \ 0 is in the constant zone, and P3 is in the stack. Static int C = 0; // global (static) initialization zone p1 = (char *) malloc (10); P2 = (char *) malloc (20 ); // The allocated 10 and 20 bytes are in the heap zone .}

 

Function call conventions

The function call Convention means that when a function is called, the function parameters will be passed to the called function and the return value will be returned to the called function. The call convention of a function is to describe how a parameter is transmitted and who balances the stack. Of course, there are also returned values.

Several Types: __stdcall ,__ cdecl ,__ fastcall ,__ thiscall ,__ nakedcall ,__ Pascal

   The vast majority of Win32 API functions use the _ stdcall call convention.. Winapi is actually just an alias for _ stacall ( # Define winapi _ stdcall). Use _ stdcall as the modifier in front of the function. In this case, the function uses the _ stdcall call convention, int _ stdcall sumexample (int A, int B ). The executable file generated using the _ stdcall call convention is smaller than that of _ cdecl, because the code for Stack cleaning is generated every time the function is called, stack cleanup (stack balancing) is performed by the called function. A function has variable parameters. Like my wsprintf function, like the previous prinf function, we must use the _ cdecl call convention, because only the caller knows that the number of parameters is in each function call, therefore, only the caller can clear the stack.

Miscellaneous on Programming

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.