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 world
	printf(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 world
	printf(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++内存开辟空间等的介绍

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/604959.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

三层交换机与防火墙连通上网实验

防火墙是一种网络安全设备&#xff0c;用于监控和控制网络流量。它可以帮助防止未经授权的访问&#xff0c;保护网络免受攻击和恶意软件感染。防火墙可以根据预定义的规则过滤流量&#xff0c;例如允许或阻止特定IP地址或端口的流量。它也可以检测和阻止恶意软件、病毒和其他威…

element-ui table sortable排序 掉后端接口方式

实例: 官方解释:如果需要后端排序&#xff0c;需将sortable设置为custom&#xff0c;同时在 Table 上监听sort-change事件&#xff0c;在事件回调中可以获取当前排序的字段名和排序顺序&#xff0c;从而向接口请求排序后的表格数据。 1.table上要加 sort-change"sortCha…

XTuner微调LLM:1.8B、多模态和Agent

XTuner微调大语言模型&#xff0c;我们的介绍主要分为以下六个方面。 首先我们讲一下Finetune&#xff1a;分为两种Finetune范式和一条数据的一生来讲解。 为什么要微调&#xff1f;我们的大语言模型为基座模型&#xff0c;要应用到某种特定的场景&#xff0c;需要微调做相应适…

STC8增强型单片机开发——C51版本Keil环境搭建

一、目标 了解C51版本Keil开发环境的概念和用途掌握C51版本Keil环境的安装和配置方法熟悉C51版本Keil开发环境的使用 二、准备工作 Windows 操作系统Keil C51 安装包&#xff08;可以从Keil官网下载&#xff09;一款8051单片机开发板 三、搭建流程 环境搭建的基本流程&#xf…

做外贸用什么邮箱比较好?

外贸公司在推进公司业务时需要频繁进行跨国沟通&#xff0c;选择一款专业且功能强大的企业邮箱作为业务沟通工具至关重要。外贸企业邮箱需要满足5个基本内容&#xff0c;国际收发能力、安全稳定性、专业形象展示、功能完备性、客户服务与技术支持。本文将探讨做外贸时适合使用的…

C++语法|如何写出高效的C++代码(一)|对象使用过程中背后调用了哪些方法(构造和析构过程)?

文章目录 再探拷贝构造函数和重载复制运算符实例化新对象和赋值操作强转为类类型指针和引用时临时对象的构造和析构过程 考考你问题答案 再探拷贝构造函数和重载复制运算符 实例化新对象和赋值操作 首先我们写一个类&#xff0c;实现它的拷贝构造并重载赋值运算符。 class T…

封装Springboot基础框架功能-03

在些模块中汇总了一些web开发常用的配置和功能。 模块源码结构 Restful API常用定义 QueryParam请求参数 Data public class QueryParam {private String key;private String value; }RestfulController实现 RestfulController.java&#xff0c;主要汇总一些常用的restful的…

【C++】C++11--- 列表初始化|关键字

目录 前言 列表初始化 创建对象时的列表初始化 单参数隐式类型转换 多参数的隐式类型转换 new表达式中使用列表初始化 列表初始化适用于STL 容器 模板类initializer_list 关键字auto 关键字decltype 关键字nullptr 前言 C标准10年磨一剑&#xff0c;第二个真正意义上…

探索网站支付系统的奥秘,从Vue3和Spring Boot开始(入门级项目实战+在线教程)附赠项目源码!

你是否曾经在购物时&#xff0c;对着电脑屏幕前的“支付成功”四个字感到好奇&#xff1f;这背后的秘密究竟是什么&#xff1f; 今天&#xff0c;让我们一起揭开支付系统的神秘面纱&#xff0c;探索其背后的技术实现。 在这个基于Vue3和Spring Boot的支付项目实战中&#xff…

ChatGLM-Math:强化数学能力

大型语言模型&#xff08;LLM&#xff09;在文本摘要、问答和角色扮演对话等语言任务上表现出色&#xff0c;在数学推理等复杂问题上也具有应用潜力。 但目前提高 LLM 数学问题解决能力的方法&#xff0c;往往会导致其他方面能力的下降。例如RLHF的方法&#xff0c;虽然可以提…

Linux的虚拟机操作

一、linux系统 我们知道的系统用到的大多数是Windows系统。 Windows个人用到的有&#xff1a;win7 win10 win11 winxp 服务器用到的有&#xff1a;windows server 2003、2008、2013...........等等 linux也有系统。 centos 7 有5、6、8等等 redhat ubuntu kail 二、了…

Apple强大功能:在新款 iPad Pro 和 iPad Air 中释放 M4 芯片潜力

Apple 的最新强大功能&#xff1a;在新款 iPad Pro 和 iPad Air 中释放 M4 芯片的潜力 概述 Apple 推出配备强大 M4 芯片的最新 iPad Pro 和 iPad Air 型号&#xff0c;再次突破创新界限。新一代 iPad 有望彻底改变我们的工作、创造和娱乐方式。凭借无与伦比的处理能力、令人惊…

kubebuilder(6)webhook

operator中的webhook也是很重要的一块功能。也是相对比较独立的模块&#xff0c;所以放在后面讲。 webhook是一个callback&#xff0c;注册到k8s的api-server上。当某个特定的时间发生时&#xff0c;api server就会查询注册的webhook&#xff0c;并根据一些逻辑确认转发消息给…

练习题(2024/5/8)

1 二叉树的最近公共祖先 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。 百度百科中最近公共祖先的定义为&#xff1a;“对于有根树 T 的两个节点 p、q&#xff0c;最近公共祖先表示为一个节点 x&#xff0c;满足 x 是 p、q 的祖先且 x 的深度尽可能大&#xff08;…

第2章.STM32开发C语言常用知识点

目录 0. 《STM32单片机自学教程》专栏总纲 2.1. STM32嵌入式开发C语言编程的不同 2.2. C语言常用知识点 2.2.1 位操作 2.2.2 define 宏定义 2.2.3 条件编译 2.2.3.1 #ifdef 2.2.3.2 #ifndef 2.2.3.3 #if !defined 2.2.4 extern 变量声明 2.2.5 typedef 类型别名 …

微信用户可以通过哪些渠道查找和关注到公众号

什么是公众号 公众号&#xff0c;作为微信生态中不可或缺的一部分&#xff0c;已经成为连接用户与品牌、企业与个人的重要桥梁。它不仅是一个信息发布平台&#xff0c;更是一个集互动、服务、营销于一体的综合性工具。公众号通过发布高质量的内容&#xff0c;吸引用户关注&…

Leetcode—976. 三角形的最大周长【简单】(ranges::sort函数)

2024每日刷题&#xff08;122&#xff09; Leetcode—976. 三角形的最大周长 实现代码 class Solution { public:int largestPerimeter(vector<int>& nums) {ranges::sort(nums);for(int i nums.size() - 1; i > 1; i--) {if(nums[i - 1] nums[i - 2] > nu…

项目经理【过程】原则

系列文章目录 【引论一】项目管理的意义 【引论二】项目管理的逻辑 【环境】概述 【环境】原则 【环境】任务 【环境】绩效 【人】概述 【人】原则 【人】任务 【人】绩效 【过程】概念 【过程】原则 一、质量管理水平、质量管理的发展 1.1 质量管理水平 1.2 质量管理的发展 …

亲测快捷高效的编写测试用例方法

前言 测试用例是任何测试周期的第一步&#xff0c;对任何项目都非常重要。如果在此步骤中出现任何问题&#xff0c;则在整个软件测试过程中都会扩大影响。如果测试人员在创建测试用例模板时使用正确的过程和准则&#xff0c;则可以避免这种情况。 在本篇文章中将分享一些简单而…

【大学物理】双语合集听课笔记

7.5 angular momentu(角动量)_哔哩哔哩_bilibili 6.4Energy in Rotation Motion 有质量有速度的物体有动能&#xff0c;是不是很有道理 international system&#xff08;from French systeme international&#xff0c;acronym&#xff0c;SI&#xff09;of ineria kg*m^2 转…
最新文章