Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
Pointer, C language developers said very look, the use of pointer, many people said dare not face TA, unlike Java, garbage automatic collection function, we do not have to worry about so many memory leaks and other problems, that C language, Pointers are divided into “wild pointer”, “lost pointer”. Are you more confused, this article together to conquer the TA!
- Found my cover seems to pay tribute to the road crazy “Sauron “, just like our protagonist today, can not find the north” lost “pointer HHH
Dangling pointer/stray pointer definition
- A dangling pointer (also known as a stray pointer) is called when the object it points to is released or reclaimed, but no changes are made to the pointer so that it still points to the reclaimed memory address.
Wild pointer definition
- A wild pointer is a pointer that has not been initialized (so we don’t know where it will point to)
int* p;
// Use it without initialization
printf("%p", p);
Copy the code
The compiler will tell you something is wrong!
Common mistakes:
1. Pointer not initialized when defined:
- When a pointer is defined, it points to a random region if the program does not initialize it.
This type of pointer is what we call a wild pointer
// Define a pointer (no initialization)
int *p ;
// Then use the pointer
*p =12;
Copy the code
2. Pointer is not null after release:
- When we create space with malloc(), we check to see if the return value is null
- If it is empty, the opening fails.
- If not null, the pointer points to the first address of the opened memory space.
- A pointer to memory that is freed with free() or delete is “junk” memory. It would be a dangling/stray pointer if we didn’t empty it or do any other assignment.
free
Free () can only free dynamically allocated memory, not arbitrary memory. The following is incorrect:
int a[10];
free(a);
Copy the code
Note: Free () does not change the value of the NP variable itself (a pointer to a structure) after calling free()It still points to the same memory space
You can view it as, np was pointing to a house, and now it’s free, and it’s throwing everyone out of the house, and NP is still pointing to the house
However, the memory is invalid and cannot be used. They look the same if you print them out, but if you access them againThe data that you access after free is really weird
Avoid methods
It is recommended that the value of NP be set to NULL after the free operation, for example:
free(np);
np=NULL;
Copy the code
3. Define a variable in a method and return the address of the variable
- Let’s start with an example
#include"stdio.h"
int* tempAddress(a) {
int temp1[] = { 1.2.3 };
return temp1;
}
int* tempAddress2(a) {
int temp2[] = { 4.5.6 };
return temp2;
}
int main(a) {
/ / for the first time
int* temp = tempAddress();
printf("%d\n", *temp);
// Call a completely different function
tempAddress2();
// reprint
printf("%d\n", *temp);
}
Copy the code
Aye? Why is the output so strange after calling a completely different function when it worked the first time?
The stack
- Remember, each time a method is executed, a stack frame is allocated to the method. When this function returns, the area allocated for the stack frame may have been freed, but the data has not been cleaned up
- However, any subsequent function calls may take up the space that was just freed. When temp is reprinted, it becomes our newly modified value
The heap
Maybe if you see some of the students here, you have a question? What’s the difference between free and the collection of this stack? Why is it that free can’t be accessed directly, while the collection of the method stack just reclaims the region, but the data hasn’t been cleaned up
In fact, we usually use free with malloc, what is malloc, malloc is that we programmers manually create a space in the heap, can manage their own, unlike the stack, out of the function will be automatically recycled
Let’s see if we can solve that problem with malloc
int* tempAddress(a) {
//int a[] = {1,2,3};
int* a = (int*)malloc(sizeof(int) * 3);
a[0] = 1;
return a;
}
int* tempAddress2(a) {
//int b[] = {4,5,6};
int* b = (int*)malloc(sizeof(int) * 3);
b[0] = 4;
return b;
}
int main(a) {
int* temp = tempAddress();
printf("%d\n", *temp);
tempAddress2();
printf("%d\n", *temp);
Copy the code
- Isn’t it delightful? We can manually control this area by ourselves. If we want to recycle it, we can also recycle it by ourselves
Stack issues to be aware of
Insufficient memory space, memory leakage. Procedure
Malloc is good for a while, malloc is good for a while, and as we continue to develop Malloc, what might be the problem?
- The most obvious, of course, is our memory allocation problem, as we malloc number, space memory is getting bigger and bigger, will inevitably cause the heap memory infinite expansion,. Running for a long time will result in less and less available memory, and the program will become more and more sluggish. If we don’t release manually, we won’t release until the end of the program. There can be problems such as memory leaks (which we mentioned earlier)
Stack overflow/buffer overflow
An overflow of the call stack caused by using too much memory is also a type of buffer overflow
- It usually happens recursively. A stack overflow is likely to result from Infinite recursion, but it can also simply be too many stack levels.
The most common case is infinite recursion
int fun(a)
{
// Recursion does not have a terminating condition
return fun();
}
Copy the code
Write in the last
- We’ll cover recursion in a later column on data structures (merge sort, quicksort, etc.) and garbage collection in different languages, but we’ll talk more about it when Melo learns more about garbage collection in Java!