Common dynamic memory errors

There are many common dynamic memory errors, the author only cites a few typical examples for your reference! !

1. Dereference NULL (null pointer) ! !

Please see the code:

#include <stdio.h>
#include <stdlib.h>

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

Obviously, this code is wrong! ! There is no judgment after the malloc function fails to open up memory space! !

The malloc function opens up a space of 20 bytes, points p to the 20 bytes, p points to the starting position of the 20 bytes, and p acts as an integer pointer, dereferences it, and accesses 4 bytes! Assign 5 to *p, the first element is 5! However , although the code looks good, it seems a lot of rigorous! ! The reason is: the return value of the malloc function is not sure whether it is: NULL (null pointer), when malloc fails to open up space, it will return NULL (null pointer), and dereference the null pointer, an error will occur! !

Please see the correct writing code format below:

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
	int* p = (int*)malloc(20);
	if (*p == NULL)
	{
		//如果p为NULL(空指针)将进行怎样的操作??
		perror("malloc");
		return 1;
	}
	else
	{
		*p = 5;
	}
	free(p);
	p = NULL;

	return 0;
}

In the above correct code, we can see the change of the specific content through debugging! ! Here, the author will not do too much analysis! !

2. Out -of-bounds access to dynamic memory development !

Please see my code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int* p = (int*)malloc(20);
	if (p == NULL)
	{
		return 1;
	}
	//使用
	int i = 0;
	for (i = 0; i < 20; i++)
	{
		*(p + i) = i;
	}

	//打印
	for (i = 0; i < 5; i++)
	{
		printf("%d ", *(p + i));
	}
	//释放
	free(p);
	p = NULL;
	return 0;
}

In the above code: the malloc function opens up 20 bytes to store 5 integers, but in the for loop, out-of-bounds access is performed! So, the program crashes! !

 

 3. For non-dynamically developed memory, use free to release it !

int main()
{
	int num = 10;
	int* p = &num;

	//……代码的其他内容!

	//释放
	free(p);
	p = NULL;
}

In the above code: int num = 10; is a local variable, which is created when the program is entered, and destroyed when the program is exited, no free release is required!

At the end of this code: using free to release will cause the program to crash! !

 4. Use free to release part of a dynamically allocated memory !

Please see my code:


#include <stdio.h>
#include <stdlib.h>

int main()
{
	int* p = (int*)malloc(40);
	if (p == NULL)
	{
		return 1;
	}
	int i = 0;
	for (i = 0; i < 5; i++)
	{
		*p = i;
		p++;
	}
	//释放
	free(p);
	p = NULL;
}

The above code will also cause the program to crash! !

First of all, 40 bytes are opened up, Int type, which can store 10 for integer data, but, in the for loop, because for (i = 0; i < 5; i++); uses 5! ! , with the progress of p++, at this time, p has pointed to the position of the fifth element, not the starting position! !

 However, in the release of free(p), the space behind cannot be released, causing the program to crash! ! !

When free is released, p no longer points to the starting position of dynamic memory development! ! So you need to use another pointer to traverse! !

Summary: For the space opened up by dynamic memory, be sure to record its starting position. If the starting position is not recorded, continue to ++ (go back), which will eventually lead to this space cannot be released! !

5. Dynamically open up memory and forget to release (memory leak) !

Please see my code:


#include <stdio.h>
#include <stdlib.h>

int* get_memory()
{
	int* p = (int*)malloc(40);
	//其他使用代码!!

	return p;  //返回动态内存空间的起始地址!!
}

int main()
{
	int* ptr = get_memory();

	//使用

	//…………其他代码

	//忘记释放了!!

	return 0;
}

In the part of the function body:    The role of return p: is to return the starting address of the dynamic memory space ! !

But there is no free at the end, so there is an error! !

The correct free code is:

free(ptr);
ptr = NULL;

Here, what is freed is: the space pointed to by ptr! !

Forgetting to release the dynamically opened space that is no longer used will cause memory leaks! ! Remember, the space opened up dynamically must be released and released correctly ! !

Guess you like

Origin blog.csdn.net/weixin_64308540/article/details/127350974