Indispensable Windows Native (9), windowsnative

Source: Internet
Author: User

Indispensable Windows Native (9), windowsnative

[Download source code]


Indispensable Windows Native (9)-C language: dynamic memory allocation, linked list, bit domain



Author: webabcd


Introduction
Indispensable C language for Windows Native

  • Dynamic Memory Allocation
  • Linked List
  • Bit domain



Example
CMemory. h

#ifndef _MYHEAD_MEMORY_#define _MYHEAD_MEMORY_ #ifdef __cplusplus  extern "C"#endif  char *demo_cMemory();#endif  


CMemory. c

/** Dynamically allocate memory, linked list, bit domain */# include "pch. h "# include" cMemory. h "# include" cHelper. h "void alloc_demo1 (); void alloc_demo2 (); void chain_demo (); void bitfield_demo (); char * demo_cMemory () {// dynamically allocates the memory base 1 alloc_demo1 (); // dynamically allocate the memory base 2 alloc_demo2 (); // linked list chain_demo (); // bitfield_demo (); return "Check the code and comment it out";} // dynamically allocate the memory base 1 void alloc_demo1 () {// malloc-allocate a contiguous memory area of the specified size, and return the first address of the region. The return value type is void *( You must specify the specific type) // calloc-allocates n blocks, each of which is a contiguous memory area of the specified size and returns the first address of the region, the return value type is void * (the user must specify the specific type) // free-Release the memory area allocated by the malloc or calloc function (required) char * s = "I am webabcd"; // open a 100-byte continuous memory area and forcibly convert it to a character array type, the Return Value of the function is the first address of the array char * p1 = (char *) malloc (100); // copy the content in s to the memory region strncpy (p1, s, strlen (s) + 1); // release the memory space free (p1) opened by malloc. // The following section is incorrect, because the opened memory space is not enough, although the value assignment is normal, but the error/* char * P2 = (char *) malloc (1); strncpy (p2, s, strlen (s) + 1); free (p2 ); // here an error is reported * // The following section is incorrect, because although the memory space is dynamically opened for p3, a constant is assigned to p3, that is to say, p3 does not use our dynamically opened memory space/* char * p3 = (char *) malloc (100); p3 = "webabcd"; free (p3 ); // here an error is reported * // open a 100-block continuous memory area with a length of 1 byte, and forcibly convert it to the character array type, the Return Value of the function is the first address of the array char * p4 = (char *) calloc (100, 1); // copy the content in s to the memory region strncpy (p4, s, strlen (s) + 1); // release the file opened by calloc, p4 Memory space free (p4);} // dynamically allocates the memory base 2 void alloc_demo2 () {char * s = "I am webabcd"; char * p = (char *) malloc (100 * sizeof (char); // The Request for memory may fail. If the request fails, NULL ('\ 0') is returned '), this judgment is required if (p = NULL) {printf ("Memory Allocation Error! "); Exit (1); // exit (0)-tell the system that I exited normally; if the value is not 0, tell the system that I exited abnormally} strncpy (p, s, strlen (s) + 1); // be sure to release (release the dynamically allocated memory space referred to by the pointer) free (p); // after the release, NULL Pointer (NULL pointer itself) is a good habit to prevent the wild pointer p = NULL;}/** concepts about linked list ** what is a data field: the region used to store actual data in the node structure is called the "data field" * What is a pointer field: Define a member item in the node structure to store the first address of the next node, the member used to store the address is called the "pointer field". ** what is a linked list? Store the first address of the second node in the pointer field of the first node, store the first address of the third node in the pointer area of the second node, and connect it until the last node. * Since the last node has no subsequent node connection, its pointer field is generally assigned a value of '\ 0 '. Such a connection method is called a "linked list" in the data structure ". * Each node in the linked list is divided into two fields: data domain and pointer domain. * Pointers to the entire linked list are generally referred to as "header nodes", which are actually the first address of the first node. * /// Define a struct named employee (used to demonstrate "one-way Linked List") struct employee {int num; char * name; struct employee * next; // pointer field, used to store the first address of the next node // struct employee * prev; // if you need a "two-way linked list", you can store the first address of the previous node in this pointer domain }; void chain_demo () {// create a chain table struct employee * creat (int n) with 100 employee nodes; struct employee * employee_chain = creat (100 ); // if you do not need it, do not forget to free the linked list struct employee * temp; while (employee_chain) {temp = employee_chain; Employee_chain = employee_chain-> next; free (temp) ;}// create a linked list with the specified number of knots (n is the specified number of nodes) struct employee * creat (int n) {struct employee * head = NULL, * p1 = NULL, * p2 = NULL; for (int I = 0; I <n; I ++) {p2 = (struct employee *) malloc (sizeof (struct employee); p2-> num = I; p2-> name = "webabcd"; if (I = 0) head = p1 = p2; else p1-> next = p2; p2-> next = NULL; p1 = p2;} return head;} // bit domain (so-called bit domain, that is, data can be stored by bit.) Void bitfield_demo () {// Note: All the results listed below are the results of my environment. For example, here int occupies 4 bytes // 1. If the types of adjacent bit domain members are the same, and the sum of the bit lengths of each member is not greater than the sizeof size of the type, the subsequent member stores struct bf in close proximity to the previous Member. // This domain type occupies 4 bytes {int a: 1; // The length of the BIT is 1, it is stored in the 4-byte 1st-bit int B: 3; // Its length is 3, and it is stored in the 4-byte 2nd-bit int c: 7; // The length of the bits is 7. It is stored in the 4-byte 5th-11th-bit} bf1; // 2. If the adjacent domain members have the same type, and the sum of the bit lengths of each member is greater than the sizeof size of the type. If the latter Member is close to the former member, the size of the sizeof type is exceeded, this member will be stored from the new storage unit (instead of the previous Member storage) struct // this bit of domain type occupies 12 bytes {int a: 31; // The length of a bit is 31. Stored in 1st 4-byte 1st-bit int B: 2; // length of 2, it is stored in 2nd to 1st bits (not close to the previous Member storage) int c: 31; // The length is 31, it is stored in 3rd to 1st bits (not close to the previous Member storage)} bf2; // 3. If the types of adjacent domain members are different, the specific implementations of each compiler are different. VC adopts the non-compression mode (different bit-domain members are stored in different bit-Domain-type spaces ); GCC and others both adopt compression methods (refer to 1st knowledge points and 2nd knowledge points) struct // this bit of domain type occupies 8 bytes {char c: 2; int I: 4; // different from the type of the previous Member, it will be stored in the new space (if it is GCC, it will be stored next to it)} bf3; // 4. If a bit of domain members are interspersed with non-bit domain members, the data is not compressed. It will not be adjacent to the storage of struct before this non-bit Domain Member. // This bit domain type occupies 12 bytes {int a: 1; // The length of the BIT is 1, it is stored in a 1st-bit char x of 1st 4 bytes; // non-bit domain member, it is stored in a 2nd 4-byte int B: 3; // The length of the BIT is 3, it is stored in 3rd-to 1st-bit int c: 7 of 3rd 4 bytes; // The length is 7, and it is stored in 3rd-to 4th-bit bf4 of 10th-4 bytes; // 5. airspace-used to force the subsequent bit domains to occupy 8 bytes from the new space storage struct // This bit domain type {int a: 1; // The bit length is 1, it is stored in 1st 4-byte 1st-bit int: 0; // airspace, forcing the subsequent bit domain to store int B: 3 from the new space; // The length of the BIT is 3, it is stored in 2nd to 1st-bit int c of 3rd 4 bytes.: 7; // The length of the bits is 7. It is stored in the 2nd-to-4th-bit} bf5 of 10th 4 bytes; // 6. placeholder domain-only placeholder, cannot use struct // This field type occupies 4 bytes {int a: 1; // The length is 1, and it is stored in the 4-byte 1st-bit int: 2; // placeholder field, which is only applicable and unavailable. The length of BITs is 2. It occupies 4 bytes of 2nd bits to 3rd bits int B: 3; // The length of BITs is 3, it is stored in 4-byte 4th-bit int c: 7; // Its length is 7, and it is stored in 4-byte 6th-7th} bf6 ;} /** considerations for Memory leakage * 1. memory allocation is not successful, but it is used * 2. Memory Allocation is successful, but it is referenced before initialization. * 3. The memory allocation is successful and initialized, but the operation has crossed the memory boundary * 4. The memory has been released, cause memory leakage * 5. The memory is released but will continue to be used. It is ** important to pay attention to the memory ** 1. If the function uses a dynamically allocated memory, it will not automatically die after the function ends, it must be manually free. That is to say, all dynamic alloc memory must be manually free * 2. After the memory is dynamically allocated, if the corresponding pointer is assigned NULL, the memory will not be released (in modern languages, there is the concept of referencing counters. If no reference is made to a region, it will be released or released by GC.) *** about the memory region type ** 1. STACK: store local variables and parameters in the stack * 2. character constant area: used to store character constants, such as char * p = "webabcd "; "webabcd" is stored in the character constant area * 3. Global Area: used to store global variables and static variables * 4. Heap: in the heap, the storage is mainly based on the dynamically allocated memory space. ** this example describes the dynamic memory allocation. ** 1. What is static memory allocation: it refers to the way to allocate a fixed storage space while the program is running (it can be understood as follows: the compiler will compile a code similar to int I; this code generates a memory allocation scheme and writes it to the compiled file. It is allocated according to the specified scheme at runtime. * 2. What is dynamic allocation: it is a way to dynamically allocate storage space as needed during the program running */



OK
[Download source code]

Related Article

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.