CAT/CAT sorting: Memory stack and memory Stack
Share cats and cats
Original address: http://blog.csdn.net/u013357243? Viewmode = contents
Storage of memory data in memory
On a computer, the data of running applications is stored in the memory.
Different types of data are stored in different memory regions, including:
1: The stack is automatically allocated and released by the compiler, and stores the parameter values and local variables of the function in half.
2: heap is allocated and released by the programmer. If the programmer does not release the heap, the heap may be recycled by the operating system when the program ends.
3: The Global (static) and static variables are stored together. The initialized global and static variables are stored in one area, uninitialized global variables and static variables are in another adjacent area, and are released by the system after the program ends.
The above three are very important. The following three sections 4: Text Constant Area: stores the always-bright string, and the system releases the string after the program ends. 5: program code area: stores the binary code of the function. 6: Register removal: used to save the top pointer and command pointer of the stack (we basically do not use ...).
Data in the stack memory Zone
The memory in the stack area is a great deal of pressure on the stack.
Features: Advanced and later. The system automatically recycles data. It is automatically allocated by the system, and the speed is fast. But programmers cannot control it.
Int sum (int x, int y) {NSLog (@ "x: % p, Y: % p", & x, & y); int result = x + y; NSLog (@ "% p", & result); return result;} void demo1 () {// variable in the stack, I essentially corresponds to the "tag" of "memory address" // pointer * indicates the content pointing to the content Space & indicates the address int I = 10; I = I + 5; NSLog (@ "==== % d", * (& I); int j = 20; NSLog (@ "I: % p, J: % p ", & I, & j); int result = sum (I, j); NSLog (@ "% d % p", result, & result );}
Memory
Data in heap memory Zone
1: due to the limited space in the stack memory, (iOS = 1 M, MAC = 8 M) in iOS applications, the objects are all created in the heap.
2: The heap area includes the system memory and virtual memory (the cache is opened on the disk), which are shared by all running applications.
3: the operating system is responsible for memory allocation in the heap area.
Use a linked list to maintain all allocated memory records.
4: Because the heap area is shared by all applications, the operating system
The allocated memory area is recorded anonymously, regardless of the memory address and size.
5: to access data in the heap area, you must use a pointer. the pointer type determines the method to access the data in the middle.
6: When a memory area is no longer used, the program must notify the operating system to recycle the memory area to ensure that the memory area is used again by other programs. Otherwise, this area will never be allocated again, which is the legendary "Memory leakage ".
7: if a region has been released and you are still trying to access it, the system will prompt "bad memory access", which is the legendary "Wild pointer access ".
8: Compared with the stack area, the efficiency in the heap area is much lower, and memory fragments are prone.
9: Compared with the stack area, the access method in the heap area is more flexible, and the memory occupied by objects can be larger.
Int main (int argc, const char * argv []) {@ autoreleasepool {// all variable types in the heap are Anonymously Accessed // attributes of accessible objects, or call the method, "depending on the type of the specified variable" // all the variables in the heap are accessed through the pointer id p = [[Person alloc] init]; [p setName: @ "zhangsan"]; NSLog (@ "% @ % p", [p name], p); // 1. point p to NULL (Address) => 0 // 2. release p // ** in OC/C ++, nil can call any method and will not report an error. // nil is a NULL object 0 p = nil pointed to by the pointer; NSString * str = p; NSLog (@ "% ld % p", str. length, p) ;}return 0 ;}
The operating system is responsible for memory allocation in the heap area.
Use a linked list to maintain all allocated memory records.
Linked List:
The code above raises a new problem:
What is the relationship between nil NULL and Nil?
Differences between nil and null and NSNull
Nil is an object, while NULL is a value. I understand that nil sets the object to null, while NULL sets the basic type to null, I personally feel a bit like in attributes, the basic type is assigned to the assign NSString type and the copy type is generally assigned to the object, while the retain type is generally used for the object. In addition, the nil call method does not generate crash or throw an exception.
Read a paragraph
nil -> Null-pointer to objective- c objectNIL -> Null-pointer to objective- c classnull-> null pointer to primitive type or absence of data.
Check usage
NSURL *url = nil;Class class = Nil;int *pointerInt = NULL;
Nil is an object pointer with NULL values, Nil is a class pointer with NULL values, and NULL is a basic data type with NULL values. These can be understood as the difference between nil, Nil, and NULL.
Ps: Create an iOS communication learning group: 304570962
You can add a cat QQ: 1764541256 or znycat
Let's study hard together.
Http://blog.csdn.net/u013357243? Viewmode = contents