C language notes

Four memory areas:
    In C language, functions are global by default; using the static keyword can set the function to be static.
    Functions decorated with static can only be used in this file.
    
    extern int age; //There is a variable of type int, the name is age, which has been defined in other files, and can be used directly here.
    int age; //A variable of type int is defined, the name is age.
    
    Each An application program has an independent memory area.
    
    Code area (code): Stores executable binary code. It will not change during program execution.
    Heap area: The area dynamically requested by the program. Its size during program execution It is dynamic.
    Stack area: stores local variables. It is a first-in, last-out memory structure. For local variables, when to push the stack and when to pop out of the stack, it does not need program control and is implemented by the C compiler.
          The size of the stack It is dynamic.
          Stack overflow: When the stack space is full, but the variables are still pushed into the stack, this is called stack overflow. The stack space is very small.
    Global area (static area): Store global variables and static variables.
    
    For 32-bit Operating system, the maximum management memory is 4GB, of which 1GB is used by the operating system itself, and the remaining 3GB is used by the application.
    
    The allocation and release of heap memory:
        malloc allocates memory; //void *malloc(size_t size) ;
        free release memory; //void free(void *ptr);
        Applying for space in the heap will not be automatically released, and must be released using free.
        malloc and free are used in pairs.
        
Heap, stack and memory mapping:
    When defining an array, the size must be constant, not variable.
    The stack grows from a high address to a low address. First in, last out.
    
    When the operating system manages memory, the smallest unit is not a byte, but a memory page.


Find the largest and second largest elements in the array:
    Step 1: Assume that the first two elements in the array are the first and second largest elements, max and smax;
    Step 2: Start traversing the comparison from the second element, when When there is an element greater than max, smax=max, max=the largest element.
    Step 3: If the current element is less than max and greater than smax, then smax is equal to this element.
        void max_and_smax(int ​​*arr, int len)
        {
            int max,smax,i;
            if(arr[0]>arr[1]){
                max = arr[0];
                smax = arr[1];
            }else{
                max = arr[1];
                smax = arr[0];
            }

            for(i=2;i<len;i++){
                if(arr[i]>max){
                    smax = max;
                    max = arr[i];
                }else if((arr[i]>smax) && (arr[i])<max){
                    smax = arr[i];
                }
            }

            printf("max = %d, smax = %d\n",max,smax);
        }


Structure definition and member initialization:
    the memory alignment mode of the structure;
    the structure is a rectangle in memory, not an irregular shape.

The array in the function parameter, the compiler will treat it as a pointer. This is a feature of the C language.

The nature of the data type: an alias for a fixed-size memory block.

What is the nature of a variable? is an alias for a contiguous memory space.

A pointer is also a data type.

Character reverse order and reverse order printing;

An introduction to the const modifier. const modifies the variable without changing the scope of the variable.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325391434&siteId=291194637