C语言常见的动态内存错误及几个经典笔试题以及c/c++内存开辟空间等的介绍

本文主要是介绍C语言常见的动态内存错误及几个经典笔试题以及c/c++内存开辟空间等的介绍,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 前言
  • 一、常见的动态内存错误
    • 1. 对NULL指针的解引用操作
    • 2. 对动态开辟空间的越界访问
    • 3. 对非动态开辟内存使用free()
    • 4. 使用free释放一块动态开辟内存的一部分
    • 5. 对同一块动态内存多次释放
    • 6. 动态开辟内存忘记释放(内存泄漏)
  • 二、几个经典笔试题
    • 1.
    • 2.
    • 3.
    • 4.
  • 三、 c/c++内存开辟空间图
  • 总结


前言

C语言常见的动态内存错误及几个经典笔试题以及c/c++内存开辟空间等的介绍


一、常见的动态内存错误

1. 对NULL指针的解引用操作

#include <stdio.h>
#include <stdlib.h>
int main()
{int* p = (int*)malloc(40);// 没有对malloc开辟失败的拦截,所以p有可能为空指针// 程序可能崩溃*p = 20;return 0;
}

2. 对动态开辟空间的越界访问

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{int* p = (int*)malloc(40);if (NULL == p){printf("%s\n", strerror(errno));}int i = 0;for (i = 0; i <= 10; i++) // 动态内存越界访问,程序报错{*(p + i) = i;}free(p);p = NULL;return 0;
}

3. 对非动态开辟内存使用free()

#include <stdio.h>
#include <stdlib.h>
int main()
{int a = 0;int* p = &a;free(p);p = NULL;return 0;
}

4. 使用free释放一块动态开辟内存的一部分

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{int* p =(int*)malloc(40);if (NULL == p){printf("%s\n", strerror(errno));}int i = 0;for (i = 0; i < 5; i++){*p = i;p++; // p 的地址一直在改变,free(p)没有指向动态内存的起始地址// 程序崩溃}free(p);p = NULL;return 0;
}

5. 对同一块动态内存多次释放

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{int* p = (int*)malloc(40);// ....free(p); // 释放了一次,但是p没有置为空指针// 此时p为野指针// p = NULL;// .....free(p); // 此时程序报错,p是野指针return 0;
}

6. 动态开辟内存忘记释放(内存泄漏)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>void test()
{int* p = (int*)malloc(40);if (NULL == p){printf("%s\n", strerror(errno));return;}int flag = 0;scanf("%d", &flag);if (flag == 5){return;}free(p);p = NULL;
}int main()
{test();return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>int* test()
{int* p = (int*)malloc(40);if (NULL == p){return p;}return p;}int main()
{// test 函数开辟空间int* ret = test();// 忘记释放return 0;
}
所以,动态开辟的空间一定要释放,并且要正确释放。

二、几个经典笔试题

1.

char* str = "hello world";
printf(“%s\n”, str); // hello world
printf(“hello world”\n); // hello world
printf(str) // hello world
str 存放 h 的地址, 所以 printf(""hello world); 和 printf(str);是一样的效果

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void GetMemory(char* p)
{p = (char*)malloc(100);// 出了GetMemory函数 p会被销毁// 无法再找到 动态内存,所以内存泄漏
}void Test(void)
{char* str = NULL;GetMemory(str);// str 是空指针,解引用时,程序崩溃strcpy(str, "hello world");printf(str);
}int main()
{Test();return 0;
}
  1. 修改1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void GetMemory(char** p)
{*p = (char*)malloc(100);// *p 指向str,str被赋值为动态内存的地址
}void Test(void)
{char* str = NULL;GetMemory(&str);strcpy(str, "hello world"); // hello worldprintf(str);free(str);str = NULL;
}int main()
{Test();return 0;
}
  1. 修改2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* GetMemory()
{char* p = (char*)malloc(100);// *p 指向str,str被赋值为动态内存的地址return p;
}void Test(void)
{char* str = NULL;str = GetMemory();strcpy(str, "hello world"); // hello worldprintf(str);free(str);str = NULL;
}int main()
{Test();return 0;
}

2.

#include <stdio.h>
#include <string.h>
char* GetMemory(void)
{char p[] = "hello world";return p; // 出了 函数 ,p[] 就会被销毁// 所以str 接收到了地址,但是p[]的内容被销毁, 无法被找到
}
void Test(void)
{char* str = NULL;str = GetMemory(); // str 是一个野指针printf(str); // 烫烫烫烫烫烫烫烫x魪?
}int main()
{Test();return 0;
}

3.


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* GetMemory(char** p, int num)
{*p = (char*)malloc(num);
}
void Test(void)
{char* str = NULL;GetMemory(&str, 100); strcpy(str, "hello");printf(str);  // hello// 但是没有释放内存空间// 可以如下释放/*free(str);str = NULL;*/
}int main()
{Test();return 0;
}

4.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>void Test(void)
{char* str = (char*)malloc(100);strcpy(str, "hello");free(str); // 释放空间,str变成野指针if (str != NULL){strcpy(str, "world");printf(str);}
}int main()
{Test();return 0;
}

三、 c/c++内存开辟空间图

在这里插入图片描述


总结

C语言常见的动态内存错误及几个经典笔试题以及c/c++内存开辟空间等的介绍

这篇关于C语言常见的动态内存错误及几个经典笔试题以及c/c++内存开辟空间等的介绍的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/968716

相关文章

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

在Spring Boot中浅尝内存泄漏的实战记录

《在SpringBoot中浅尝内存泄漏的实战记录》本文给大家分享在SpringBoot中浅尝内存泄漏的实战记录,结合实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录使用静态集合持有对象引用,阻止GC回收关键点:可执行代码:验证:1,运行程序(启动时添加JVM参数限制堆大小):2,访问 htt

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

前端下载文件时如何后端返回的文件流一些常见方法

《前端下载文件时如何后端返回的文件流一些常见方法》:本文主要介绍前端下载文件时如何后端返回的文件流一些常见方法,包括使用Blob和URL.createObjectURL创建下载链接,以及处理带有C... 目录1. 使用 Blob 和 URL.createObjectURL 创建下载链接例子:使用 Blob

C++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

Pytest多环境切换的常见方法介绍

《Pytest多环境切换的常见方法介绍》Pytest作为自动化测试的主力框架,如何实现本地、测试、预发、生产环境的灵活切换,本文总结了通过pytest框架实现自由环境切换的几种方法,大家可以根据需要进... 目录1.pytest-base-url2.hooks函数3.yml和fixture结论你是否也遇到过

Go 语言中的select语句详解及工作原理

《Go语言中的select语句详解及工作原理》在Go语言中,select语句是用于处理多个通道(channel)操作的一种控制结构,它类似于switch语句,本文给大家介绍Go语言中的select语... 目录Go 语言中的 select 是做什么的基本功能语法工作原理示例示例 1:监听多个通道示例 2:带

如何高效移除C++关联容器中的元素

《如何高效移除C++关联容器中的元素》关联容器和顺序容器有着很大不同,关联容器中的元素是按照关键字来保存和访问的,而顺序容器中的元素是按它们在容器中的位置来顺序保存和访问的,本文介绍了如何高效移除C+... 目录一、简介二、移除给定位置的元素三、移除与特定键值等价的元素四、移除满足特android定条件的元

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序