-
The so-called dynamic memory allocation refers to the method of dynamically allocating or allocating memory in the process of program execution. Dynamic memory allocation does not require pre-allocation of storage space like static memory allocation methods such as arrays, but is allocated by the system on the fly according to the needs of the program, and the size of the allocation is the size required by the program.
The benefits of dynamic memory can be summed up in 8 words: time allocation, no release!
-
The so-called dynamic memory allocation is a memory address controlled by the programmer, where the memory space can be allocated for use, in order to prevent the program from occupying too much memory, the programmer will release this space after using the memory space. Ensure that the entire program runs quickly. The benefits of dynamic memory can be summed up in 8 words:
Time allocation, no release!
-
I'm going to tell you this in layman's terms: dynamic memory refers to dynamically allocating memory.
Let's say you have a mobile phone with a total memory of 64M. 24m has already been used inside, so there are still 40m for you to distribute. For example, if you use 20m for SMS on your mobile phone, *** use 20m, or you don't like too many text messages, you want to listen to ***, then you can delete some of the text messages, then you can use the deleted memory for other needs to use memory, such as storing *** files or something.
So what about some mobile phones, it is a fixed memory, for example, the mobile phone SMS is fixed to 5m, which means that when your SMS is full of 5m, you can no longer save SMS. Do you understand?
-
Depending on the amount of data, even adjust the size of the space used for the memory station.
Advantages: Saves resources, the program runs fast and efficient.
-
Programs are stored in segments in memory.
**Paragraph: Stores the number of the sentence transformation; The program cannot be modified while it is running;
Global section: used to record the storage location of global and static variables; It doesn't change as the program runs;
Stack: the storage location for local variables, block variables, formal parameters, and return values; As the program runs, its size will constantly change; When the function is called, the space is opened, and the space is reclaimed at the end of the function call; The last-in-first-out principle is followed between different calling functions.
Heap: a dynamically distributed storage location;
1. The shortcomings of traditional arrays (static arrays).
1: The length of the array must be specified in advance, and it must be a constant integer, not the variable int a[5];
2: Traditional array programmers cannot be released by programmers, but can only be released by the system. (and can only be released at the end of the function where the array is located).
3: The length of the array cannot be dynamically expanded or contracted during the function run.
4: A function defines a traditional array, at the end of the A function, is not usable in the B function because it has already been released. That is, traditional arrays cannot cross functions.
Distinguish: static storage and static development of memory.
2. Why dynamically allocate memory?
It is used to solve the four defects of traditional arrays.
3. Examples of dynamic memory allocation and the construction of dynamic arrays.
Method: malloc function to open up space in the heap.
1: Malloc is dynamically carved out by programmers in the stack.
3: The allocated memory space should be divisible by the number of bytes occupied by the type.
4: Contains header files.
5: You can only use free(p) to free up the dynamically opened memory space that p points to.
6: For the operation of dynamic memory space, use *p to operate.
7: You can point to this dynamic space with multiple pointers.
8: When there are multiple pointers only to this dynamic space, only one pointer can be used, and an error will be reported if the pointer is released multiple times.
9: You can use the dynamically opened memory pointer as a function parameter.
Question: Is the allocation type of p dynamic or static? When free(p) is called, will p's memory space be freed?
Example: Dynamically construct a one-dimensional array:
Function: Re-open up a byte of dynamic memory from the original dynamic memory, and if this number is larger than the previous one, the previous data will be saved. If it is smaller than the original, keep the previous data.
4. Comparison of static memory and dynamic memory.
Statically carved memory: Opened up in the stack, allocated by the compiler, and automatically released by the system.
Dynamically opened memory: opened up in the heap, opened up by the programmer, and automatically released by the programmer.
5. The problem of using memory across functions.
-
1. Allocate from static storage area. Memory is allocated when the program is compiled, and this memory exists for the entire duration of the program. For example, global variables, static variables;
2. Create on the stack. When the function is executed, the storage units of the internal local history variables of the function can be created on the stack, and these storage units are automatically released when the function execution is changed to end the limb round. Stack memory allocation operations are built into the processor's instruction set;
3. Allocation from the heap, also known as dynamic memory allocation. The program uses malloc or new to request as much memory as it needs when it runs, and the programmer is responsible for when to free up the memory with free or delete.
-
Dynamic memory allocation is when a program is running, and the programmer allocates memory to variables such as arrays or structures through statements. Usually at the beginning of the run, the programmer does not know how many elements the array will have, but is entered by the user at runtime. Dynamically allocated (malloc) memory can be canceled when it is not needed.
It can be increased (realloc) when it is necessary to increase the unit again.
Static variables and statically allocated memory are two unrelated things.
Static variables are static variables, and the lifetime is the same as that of a program, which is equivalent to a global quantity. Especially in the static variable in the function, the general variable, when you exit the function, it is gone, the static variable still exists, and the value assigned last time is still there.
Dynamic Variables - There are no "dynamic variables".
-
What are the main differences between static storage? Dynamic storage.
-
The main purpose of dynamically allocating memory is to improve the efficiency of memory usage.
When the amount of data cannot be determined, dynamic memory allocation is generally used. Of course, it is also possible to estimate a value in advance, and it is also possible to define an array, but how big the pre-estimated value needs to be, how big it is, waste resources, how to use it if it is small, how can it be no more, no less, of course, it is better to have a data and give a place. Right?
It is not a good habit for you to use new to indicate new pointers, and it is easy to create ambiguity. It can be changed, for example, using pnew is also better than using new directly. The reason why we say dynamic is that we use it again and again, so in the program, we often make an application before use, then allocate it, then store the data, and then connect it to the linked list.
This is generally the case.
2。Now there is no data stored in the general head pointer, it is an empty structure, only the pointer field is connected to the next node. Of course, you can also put in data without errors, but it will be inconsistent with most people's programming habits.
3。It's also for safety, people form habits, as you write scanf("%d", &new->score), which is easy to create illusions, so it is more intuitive to use non-pointer variables. If you really want to use it, it's best to write as:
scanf("%d",&(new->score)).
Therefore, in some places, it is not that it is not allowed to write like that, but that kind of writing is easy to cause misunderstanding.
-
1. Dynamic allocation of memory is generally used in arrays, linear tables, stacks, strings of these data structures, when you write a program you need to organize a bunch of data, and there are insertion and deletion operations, it is best to use dynamic allocation of memory, more memory space. int *p is not dynamically assigned, p is just a pointer (a variable that points to an address), but pointers are often used with molloc functions, which confuses you. Defining a node is only a definition, and there is no unit in memory yet, and C++ uses the new keyword to apply.
2. The head pointer head points to the head node L, and the data is not stored in L, and the pointer field of L points to the first node (the landlord needs to figure out the head node and the first node). head next is usually used only to point to the first node. 3. new is a keyword, how can you define a new?
-
1. int *p, just defines a pointer variable, when defining this pointer variable, a piece of memory is allocated, but the address is stored in this memory. With new, you can roll many blocks of memory at the same time, such as arrays.
2. The header of the linked list is just a pointer, which only stores the address where the first student information is stored, that is, it refers to the first student information.
3. The memory allocated with new is allocated in the heap, and the type of variable you allocate with new is what type of big scum, but you can't quote it as you said.
The pinyin of the inner character is n i 内 pinyin n i,n is a first level character in the general standard of Chinese.This character was first found i
The dynamic acquisition of digital TV IP timeout means that when connecting to the network through the DHCP server, the TV cannot obtain the IP address in the maximum time, resulting in the inability to access the Internet, and the normal broadcast function of digital TV will also be affected. >>>More
Tasteful, knowledgeable, reasonable.
Here's how.
If the QQ space is not displayed (not updated), it is recommended to try to clear the IE cache and reopen it to view. If you clear the IE cache and still do not display (do not update) the space dynamics, please refer to the following information: >>>More
If there is no sound in the Apple 8p live wallpaper, it is recommended to set it in the following ways: >>>More