C-arrays, input and output of strings, memory allocations, three memory allocation functions

Source: Internet
Author: User


Array initialization 1. When the array is initialized, you can
1 int 3 ; 2 int Arr[len];
2. However, it is not possible to:
1 int 3 ; 2 int Arr[len] = {123};
3. But this can be:
1 int arr[3] = {123};
2 not possible reason: compiler compile time int arr[3] = {1, 2, 3} This way will be converted to:
1 int arr[3]; 2 arr[01; 3 arr[12; 4 arr[23;
and 2 of the way at compile time, can not determine the length of the array: Because the variable is executed only when the value will be obtained, so the direct error, but with the macro is not a problem array initialization method complement: Specify subscript initialization
1 int arr[] = {[11, [22, [30};
Automatic assignment of the remaining unassigned 0 how to initialize all elements of an array to 0:
1 int arr[] = {0};
Array naming conventions (recommended): Since arrays generally store multiple data, it is better to name the plural form of the noun (if it is an English noun) array address discrimination: int ARR[10] First address: 1. Arr[0]2. The address of the arr array = = = Array name = = = = = = Arr[0] = = Arr[0] The address of the low byte in the array = = (store at the lowest point) so: We can't print the array name directly, so we can't get the value of the array element because the array name is actually the address of the array, so when we print the array Calculated with the length of the%p array 1. Each element of the array has the same type, so each element of the array occupies the same amount of space as 2. Use the sizeof operator to calculate the total number of bytes consumed by the array Note: When an array is an argument to a function, the length of the array is lost when it is passed.When an array is used as an argument to a function, using sizeof to calculate the length of the parameter array within the function will always be 8 reasons: andnot really going to declare a new arrayInstead, it declares a pointer to the same name and then points to the original array, a pointer of 8 bytes, so it's 8. When an array is an argument to a function, it is not an array, but a pointer to the first address of the real parameter group, in fact, it is through the parameter pointer in the operation of the real argument group--Address Delivery. Array-specific string input and output gets () unsafe reason does not automatically add ' s ' Terminator if an array of type char has a length of 50get () then all input will be present in this array resulting in an out-of-bounds access to the upgraded version of--fgets (character array name, array length, input source) Suppose the array char ch[5]; then fgets (ch, sizeof (CH), stdin) will only put the first 4 characters of the input into the array, and in the last addition, if the input string length is less than the length of the array, the function will also receive a carriage return input source can be a file, Keyboard input fputs (): string output function 1. does not wrap 2. There is no format control of the advantages of these two functions: the ability to automatically intercept the length of the main or write and read the string to the file Const1.const modifier pointer variable, the value of the pointed variable cannot be changed
1 Const int *p = &A; 2 p = &B; 3 int Const *P1 = &A; 4 p1 = p;
2.const modifier pointer variable, pointer variable point variable value can be changed, point to cannot change
1  INT *const P2 = &A; 2 ;
3.const modifier pointers and values of pointer variables cannot be changed
1 Const int Const P3 = &A; 2 p3 = &a;  // not to 3  - // It's not possible .
  above 3 kinds of situation discrimination if the const is on the left side of the *   indicates that the value of the variable pointed to by the pointer variable cannot be changed, but the point can be changed if the const is on the right of the *   indicates that the value of the variable pointed to by the pointer variable can be changed, but the point cannot be changed There are const  on the side that indicate that the pointer variable points to the value and pointer variable pointing can not be changed  const the biggest advantage: improved efficiency. The   compiler usually does not allocate storage space for ordinary const constants, and the other is to keep them in the symbol table, which makes it a constant during compilation, without the storage and read memory operations, making it highly efficient   using const: Modifying General constants   A general constant is a constant of a simple type .  this constant is defined as the modifier const can be used before the type specifier, or it can be used in type descriptions specifier   use cases: 1. Modifier variable 2. Modifier pointer Variable 3. Modify array   computer memory allocation 1. Static storage allocation: When the program is compiled, it has been allocated, the entire running period of the program exists, the global variable, the static variable  2. Allocation on the stack  3. Heap allocation: Dynamic allocation: Memory requested by malloc or new when the program is running   Freed memory memory with free and delete 5 large-area BBS segment: BSS segment, Block Started by Symbol abbreviation, usually refers to the program to store uninitialized global variables and static variables   data segments: To store initialized global variables and static variables in the program, static memory allocations,  string constants   Code snippets: A piece of memory area of program execution code   post-execution storage area   heap: Dynamic mismatch of memory segments   size is not fixed   Stack: stack, local variables temporarily created by the user-stored program   first in first out    common memory allocation functions: Memory request malloc (length of memory required)   malloc (4*sizeof (int )) allocates contiguous memory space from the heap of memory to size bytes, if the memory allocation succeeds, returns the first address of the new space, if it fails, returns null so generally used, it is best to add a judgment, whether the allocation succeeds  calloc (A, b)     ALLOCATE a block of memory with a length of bSpace Calloc (4, sizeof (int)) Two functions allocated memory space addresses are contiguous so generally used, it is best to add a judgment, whether the allocation of success calloc compared to the advantages of malloc: Calloc after the application is completed, the byte will be automatically cleared 0 (initialization)   Memory extension realloc () function: can be extended to existing space (not rigorous) size 1. First detection, allocated memory space adjacent to the memory space is also quite satisfied with the condition of the contiguous memory space is: Direct application behind this piece of contiguous memory space is not: in the memory of the other location Find additional contiguous memory space that satisfies the condition, and then copy the existing content directly into the new application space to apply: int *p = (int *) malloc (4*sizeof (int)); expand (or Redistribute) realloc (p, 40*sizeof (int)); Return: The address of the new space (if it is re-applied)   Wild Pointer Management defines a pointer variable at the same time it is best to initialize, if there is a corresponding assignment is assigned, if there is no assignment is initialized to null, after the pointer is used to set the value of the pointer variable to null 

C-arrays, input and output of strings, memory allocations, three memory allocation functions

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.