[C language foundation] field pointer and null pointer

Introduction to the most grounded C language wild pointer in the whole network. Here we make some brief introduction to the knowledge points of wild pointer and null pointer. The author is a beginner. Blogging is also a process for the author to learn. It is inevitable that there are some contents in the article that are not well understood or inappropriate. Please make corrections. I hope you will give more support and give roses to others with lingering fragrance!

1: Field pointer

Concept: a wild pointer refers to a memory address that is unknown (random, incorrect, and unrestricted).
Note: pointer variable is also a variable. If it is a variable, it can be assigned arbitrarily. However, it is meaningless to assign any value to the pointer variable, because such a pointer becomes a wild pointer, and the area pointed to by this pointer is unknown (the operating system does not allow the operation of the memory area pointed to by this pointer).
Note: the wild pointer will not directly cause an error, and the operation of the memory area pointed to by the wild pointer will cause a problem.
Code example:

int a = 100;
int *p;
p = a; //Assign the value of a to the pointer variable p, P is the wild pointer, ok, there will be no problem, but it doesn't make sense

p = 0x12345678; //Assign a value to the pointer variable p, P is the wild pointer, ok, there will be no problem, but it doesn't make sense

*p = 1000;  //Assignment to the field pointer is not allowed

Assign the value of a to the pointer variable p, P is the wild pointer, ok, there will be no problem, but it doesn't make sense.
Assign a value to the pointer variable p, P is the wild pointer, ok, there will be no problem, but it doesn't make sense.

1.1: cause of formation of wild pointer

1. Pointer uninitialized: the pointer variable will not automatically become a NULL pointer when it is just created. Its default value is random and the space it refers to is random.
Code example:

int main()
{
	int * p;
	*p = 20;
	return 0;
}

(personal understanding: the pointer variable is randomly assigned by the operating system, does not point to a specific space, and has no foothold)
2. Pointer out of bounds access: the range pointed to by the pointer exceeds the reasonable range, or the pointer or reference to the stack memory is returned when calling the function, because there are functions in the stack and will be released at the end of the function.
Code example:

int main()
{
	int arr[10] = {0};
	int *p = arr;
	for(int i = 0; i <= 11; i++)
	{
		*(P++) = i;//When the range pointed by the pointer exceeds the range of array arr, p becomes a wild pointer.
	}
	return 0;
}

3. The pointer is not set to NULL after release: sometimes the pointer is not assigned NULL after free or delete, which will make people think it is legal. In fact, they just release the memory referred to by the pointer, but they don't forget the pointer itself. At this point, the pointer points to invalid memory. The pointer after release shall be set to NULL immediately to prevent "wild pointer".
Code example:

int main()
{
	int *p = NULL;
	p = malloc(10 * sizeof(int));
	if (!p)
	{
		return;
	}
	//Successfully open up memory, you can operate memory.
	free(p);
	p = NULL;
	return 0;
}

(personal understanding: we stayed in a hotel the day before and checked out the next day. Although we know the house number of the room, aunt cleaning has cleaned the room. We don't know what the room is like, so we can't operate it.)

2.1: avoid wild pointer

1. Initialize pointer;
Code example:

int main()
{
	int *p = NULL;
	int a = 10;
	p = &a;
	*p = 20;
	return 0;
}

2. Avoid pointer crossing;
Code example:

int main()
{
	int arr[10] = {0};
	int *p = arr;
	for(int i = 0; i < 10; i++)
	{
		*(P++) = i;//Strictly abide by the effective scope.
	}
	return 0;
}

3. Avoid returning the address of local variables;
Code example:

int * test()
{
	int a = 20;
	return &a;
}
int main()
{
	int *p = NULL;
	p = test();
	printf("%d\n", *p);
	return 0;
}

This is related to the scope of variables. Local variables exist in the stack area. When the called function ends, the memory space of local variables in the stack area is released. It is unreasonable to access this space again.
4. Set the opened pointer to NULL after release;
When the memory space pointed to by pointer P is released, the value of pointer P is not set to NULL. free just frees up memory space, but does not set the value of pointer p to NULL.
Code example:

int main()
{
	int *p = NULL;
	p = malloc(10 * sizeof(int));
	if (!p)
	{
		return;
	}
	//Successfully open up memory, you can operate memory.
	free(p);
	p = NULL;//Avoid wild pointers
	return 0;
}

5. Develop good programming habits;
Good programming habits can avoid many problems. The road is blocked and long, but the line is coming!!!

2: Null pointer

*NULL is a macro constant with a value of 0: #define NULL ((void) 0)

Meaning: in order to mark that the pointer variable does not point to any variable (idle and available), in C language, NULL is usually assigned to this pointer, which marks that this pointer is a NULL pointer and does not point to any space.

Note: dereference the pointer to get the value it points to. However, by definition, a NULL pointer does not point to anything, because dereferencing a NULL pointer is an illegal operation, so before dereferencing, you must ensure that it is not a NULL pointer.
Code example:

void test(){
	char *p = NULL;
	**//Copy the contents of the memory area pointed to by p**
	strcpy(p, "1111"); //err

	char *q = 0x1122;
	//Copy the contents of the memory area pointed to by q
	strcpy(q, "2222"); //err		
}

OK!!! Audience masters, here is just the introduction of wild pointer and null pointer. If friends think it has a little effect, I hope friends can give little rookie some support! In the follow-up, we will continue to bring better blog posts to our friends. We also hope that our friends can continue to pay attention. Little rookie is committed to sharing more of his learning experience and personal understanding with you. I hope you like it and make corrections.

Tags: C C++ R Language

Posted on Tue, 16 Nov 2021 09:29:41 -0500 by ann