How do memory cells are released in C and how do they work?

Updated on technology 2024-03-19
4 answers
  1. Anonymous users2024-02-06

    c Management memory can be roughly understood as two types, one allocated on the stack and one allocated on the heap.

    Temporary variables, dynamic variables, are allocated on the stack, and after running, they pop up directly on the stack and are gone.

    When the memory allocated on the heap is released, it can basically be understood that the pointer does not point here. It also loses control of this piece of memory. In fact, the so-called release. It's easy to misunderstand the literal meaning.

    Some machines have some operating systems, which will empty this memory when it is released, but this practice is not efficient, but it is safe, and few machines do this, most of them are so-called releases, which just don't let you control this memory.

  2. Anonymous users2024-02-05

    The C language uses the free function to release dynamically allocated memory units.

    1. Free function:

    prototype: void free(void *ptr);

    Function: Release the dynamic memory allocated by the malloc (or calloc, realloc) function to the pointer variable;

    Header file. Or.

    2. In order to avoid releasing the pointer memory that has been released, or not releasing the memory, it is best to assign the initial value null when defining the pointer in the C language, and assign null immediately after release, and check the pointer value when releasing and then decide to release to avoid releasing errors, for example:

    int *a = null

    int *b = (int*) malloc(sizeof(int) *10);

    a= b;After performing a large number of operations

    if(a != null)

    if(b != null)

  3. Anonymous users2024-02-04

    It will be released, and the operating system will help you release it, but generally the programmer must release it with free() before the end of the program

  4. Anonymous users2024-02-03

    1 From the design of the C language itself, it will not be released.

    The so-called dynamic memory is the memory applied by the malloc series functions, and the memory will not be released unless free is used in the program.

    From this point of view, even if the process ends, the segment of memory will be consumed. This phenomenon is known as memory leak.

    2 Most operating systems can be released intelligently.

    Because dynamic memory is used by the process, the memory applied to the operating system controller, so the operating system kernel can record which memory is used by which process, so that in order to reduce the harm of memory leakage, the operating system kernel has implemented the mechanism of automatic self-use memory allocated by the process after the process exits.

    3 It is still necessary to avoid only assigning and not releasing ** in writing.

    As a ** writer, you should follow the rules of self-release without memory required by the C language, so that the program takes up as few resources as possible. Otherwise, it's a waste of memory resources.

Related questions
11 answers2024-03-19

Valid variable names for the C language:

First, it can only contain numbers, letters, and underscores. >>>More

7 answers2024-03-19

do is used to loop.

For example. int i=1,sum=0; >>>More

8 answers2024-03-19

The semicolon is the sign of the end of the statement, but the semicolon is not used after the loop, if, and subfunctions such as long long a (int b), and everything else is used, but two semicolons cannot be added, although it will not cause an error, but it may affect the result.

11 answers2024-03-19

srand(int) is used to set the seed, and then returns a random value each time rand(). >>>More

6 answers2024-03-19

Understand the following rules: 1) Overloading an operator does not change the priority of the operator. >>>More