常见的动态内存错误
一、 对NULL指针的解引用操作
void test()
{
int *p = (int *)malloc(INT_MAX/4);
*p = 20;//如果p的值是NULL,就会有问题
free(p);
}
二、对动态开辟空间的越界访问
代码如下(示例):
void test()
{
int i = 0;
int *p = (int *)malloc(10*sizeof(int));
if(NULL == p)
{
exit(EXIT_FAILURE);
}
for(i=0; i<=10; i++)
{
*(p+i) = i;//当i是10的时候越界访问
}
free(p);
}
三、 对非动态开辟内存使用free释放
void test()
{
int a = 10;
int *p = &a;
free(p);//ok?
}
四、使用free释放一块动态开辟内存的一部分
void test()
{
int *p = (int *)malloc(100);
p++;
free(p);//p不再指向动态内存的起始位置
}
五、对同一块动态内存多次释放
void test()
{
int *p = (int *)malloc(100);
free(p);
free(p);//重复释放
}
六、 动态开辟内存忘记释放(内存泄漏)
void test()
{
int *p = (int *)malloc(100);
if(NULL != p)
{
*p = 20;
}
}
int main()
{
test();
while(1);
}
忘记释放不再使用的动态开辟的空间会造成内存泄漏。
七、几个经典的笔试题
7.1题目一:
void GetMemory(char *p)
{
p = (char *)malloc(100);
}
void Test(void)
{
char *str = NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
}
str还是空指针,形参是实参的拷贝,给p开辟了100个字节的空间,不会对str有影响,所以hello world无法拷贝到str的空间,无法被打印,程序会出错
7.2题目二:
char *GetMemory(void)
{
char p[] = "hello world";
return p;
}
void Test(void)
{
char *str = NULL;
str = GetMemory();
printf(str);
}
在进入函数时创建了字符数组,数组里的内容是hello world,但是当return p后函数就销毁了,数组里的内容也销毁了,此时的p是一个野指针,指向的内容是随机值
7.3题目三:
void GetMemory(char **p, int num)
{
*p = (char *)malloc(num);
}
void Test(void)
{
char *str = NULL;
GetMemory(&str, 100);
strcpy(str, "hello");
printf(str);
}
这个题目咋一看没有错误,将str的地址传参,给str开辟了100个字节的空间,顺利的打印出hello,但是有个很严重的错误就是,没有将内存释放,造成了内存泄漏,应将free(str),str = NULL;
7.4题目四:
void Test(void)
{
char *str = (char *) malloc(100);
strcpy(str, "hello");
free(str);
if(str != NULL)
{
strcpy(str, "world");
printf(str);
}
}
这里free有点早,而且遗漏了将str置成空指针这一步骤,一旦free了str后,str指向的那块空间里的内容就不存在了,但是str仍然保存了那块空间的地址,所以将会打印出world