What is the difference between heap and stack in iOS?
The main differences between stack and stack are as follows:
1. Management Method: For stacks, it is automatically managed by the compiler without manual control. For heaps, the release work is controlled by the programmer and memory leak (memory leakage) is easily generated ).
2. Application size:
Stack is a data structure extended to a low address and a continuous memory area. This statement indicates that the address at the top of the stack and the maximum stack capacity are pre-defined by the system, which is a constant determined at the time of compilation. If the requested space exceeds the remaining space of the stack, overflow will be prompted ). Therefore, the space available from the stack is small.
The heap is a data structure extended to the high address and a non-sequential memory area. This is because the system stores the idle memory address by the linked list, which is naturally discontinuous, And the traversal direction of the linked list is from the low address to the high address. The heap size is limited by the valid virtual memory in the computer system. It can be seen that the space obtained by the heap is flexible and large.
3. Fragmentation issues:
For the heap, frequent new and delegate will inevitably lead to memory space disconnections, resulting in a large number of fragments and reduced program efficiency. For the stack, this problem does not exist, because the stack is an advanced and outgoing queue, they are so one-to-one correspondence that it is impossible to have a memory block popped up from the middle of the stack.
4. allocation method:
There are two stack allocation methods: static allocation and dynamic allocation. Static allocation is done by the compiler, such as local variable allocation. Dynamic Allocation is implemented by the alloca function, but the stack dynamic allocation is different from the heap dynamic allocation. Its Dynamic Allocation is released by the compiler without manual implementation.
The heap is dynamically allocated without static allocation.
5. Allocation Efficiency:
The stack is the data structure provided by the machine system. The computer will provide support for the stack at the underlying layer, allocate a special register to store the stack address, and all the output stacks of the Pressure stack have special command execution, this determines the high efficiency of the stack. The heap is provided by the C/C ++ function library, and its mechanism is very complicated.
Difference Between Stack and stack in java
The Java heap is a runtime data zone and class (the object allocates space from it. These objects are created using commands such as new, newarray, anewarray, and multianewarray. They do not need program code to be explicitly released. The heap is responsible for garbage collection. The advantage of the heap is that the memory size can be dynamically allocated, and the lifetime does not have to be told in advance because the heap dynamically allocates memory at runtime, the Java Garbage Collector automatically collects the unused data. However, the slow access speed is due to the need to dynamically allocate memory during runtime.
The advantage of stack is that the access speed is faster than that of stack, second only to register, and stack data can be shared. However, the disadvantage is that the data size and lifetime in the stack must be fixed, and there is a lack of flexibility. The stack mainly stores some basic class variables (, int, short, long, byte, float, double, boolean, char) and object handles.
A very important feature of stacks is that data in stacks can be shared. Suppose we define both:
Int a = 3;
Int B = 3;
The compiler first processes int a = 3. First, it creates a reference with the variable a in the stack, and then finds whether the value 3 in the stack exists. If no value is found, store 3 and point a to 3. Then process int B = 3. After the referenced variable of B is created, B is directed to 3 because there is already 3 in the stack. In this way, both a and B point to 3 at the same time.
At this time, if a is set to 4 again, the compiler will re-search whether there are 4 values in the stack. If not, it will store 4 and make a point to 4; if yes, direct a to this address. Therefore, changing the value of a does not affect the value of B.
Note that the sharing of data is different from the sharing of two objects pointing to one object at the same time, because the modification of a does not affect B, which is completed by the compiler, it facilitates space saving. A variable referenced by an object modifies the internal state of the object, which affects the variable referenced by another object.
String is a special packaging data. Available:
String str = new String ("abc ");
String str = "abc ";
The first method is to use new () to create an object, which is stored in the heap. Each call creates a new object.
The second is to first create a String class object in the stack to reference the variable str, and then find whether the stack contains "abc". If not, store "abc" into the stack and point str to "abc". If "abc" already exists, direct str to "abc ".
Use the equals () method to compare the values in a class. Use the = method to test whether the references of the two classes point to the same object. The example below illustrates the above theory.
String str1 = "abc ";
String str2 = "abc ";
System. out. println (str1 = str2); // true
It can be seen that str1 and str2 point to the same object.
String str1 = new String ("abc ");
String str2 = new String ("abc ");
System. out. println (str1 = str2); // false
The new method is used to generate different objects. Each time one is generated.
Therefore, the first method is used to create multiple "abc" strings. Only one object exists in the memory. this method is advantageous and saves memory space. at the same time, it can improve the running speed of the program to a certain extent, because the JVM will automatically decide whether there is ...... remaining full text>
What is the difference between stack and stack?
I. prerequisites-program memory allocation
The memory occupied by a c/C ++ compiled program is divided into the following parts:
1. stack: the stack is automatically allocated and released by the compiler, storing the parameter values of functions and the values of local variables. The operation method is similar to the stack in the data structure.
2. heap: Generally, it is assigned and released by the programmer. If the programmer does not release the heap, it may be recycled by the OS at the end of the program. Note that it is different from the heap in the data structure. The allocation method is similar to the linked list.
3. Global (static): stores global variables and static variables. initialized global variables and static variables are stored in one area, uninitialized global variables and uninitialized static variables are in another adjacent area. -The system is released after the program ends.
4. Text Constant Area-constant strings are placed here. The program is released by the System
5. program code area-stores the binary code of the function body.
Ii. Example Program
This is written by a senior. It is very detailed.
// Main. cpp
Int a = 0; global initialization Zone
Char * p1; uninitialized globally
Main ()
{
Int B; stack
Char s [] = "abc"; stack
Char * p2; stack
Char * p3 = "123456"; 123456 \ 0 is in the constant zone, and p3 is in the stack.
Static int c = 0; Global (static) initialization Zone
P1 = (char *) malloc (10 );
P2 = (char *) malloc (20 );
The allocated 10-byte and 20-byte areas are in the heap area.
Strcpy (p1, "123456"); 123456 \ 0 is placed in the constant area, and the compiler may optimize it into a place with the "123456" that p3 points.
}
Ii. Theoretical knowledge of heap and stack
2.1 Application Method
Stack:
Automatically assigned by the system. For example, declare a local variable int B in the function; the system automatically opens up space for B in the stack.
Heap:
The programmer needs to apply and specify the size. In c, the malloc Function
For example, p1 = (char *) malloc (10 );
Use the new operator in C ++
For example, p2 = (char *) malloc (10 );
But note that p1 and p2 are in the stack.
2.2
System Response after application
STACK: as long as the remaining space of the stack exceeds the applied space, the system will provide the program with memory. Otherwise, an exception will be reported, prompting stack overflow.
Heap: First, you should know that the operating system has a linked list that records idle memory addresses. When the system receives a program application,
The linked list is traversed to find the heap node with the first space greater than the requested space. Then, the node is deleted from the idle node linked list and allocated to the program, for most systems, the size of the allocation will be recorded at the first address in the memory space, so that the delete statement in the code can correctly release the memory space. In addition, because the size of the heap node is not necessarily equal to the applied size, the system automatically places the excess part in the idle linked list.
2.3 Application size limit
STACK: in Windows, a stack is a data structure extended to a low address and a continuous memory area. This statement indicates that the stack top address and the maximum stack capacity are pre-defined by the system. In WINDOWS, the stack size is 2 MB (OR 1 MB, in short, it is a constant determined during compilation. If the requested space exceeds the remaining space of the stack, overflow will be prompted. Therefore, the space available from the stack is small.
Heap: the heap is a data structure extended to the high address and a non-sequential memory area. This is because the system uses the linked list to store the idle memory address, which is naturally not consecutive, And the traversal direction of the linked list is from the low address ...... The Rest Of The full text>