c++:动态内存变量

news2024/7/7 18:45:40

典型的C++面向对象编程

元素
(1)头文件hpp中类的定义
(2)源文件cpp中类的实现(构造函数、析构函数、方法)
(3)主程序

案例
(1)用C++来编程“人一天的生活”
(2)“人”的属性:name、age、male
(3)“人”的方法:eat、work(coding/shopping)、sleep
(4)人的生活:eat1->work->eat2->work->sleep
实战中,一般一个cpp和一个hpp文件配对,描述一个class,class的名字和文件名相同的。
.h 文件

namespace MAN{
class testperson {
 public:
  //属性
  string name;
  int age;
  bool male;  //性别 男性 ture

  //方法
  void eat(void);
  void work(void);
  void sleep(void);

  testperson(/* args */);
  ~testperson();
};

.c 文件

using namespace MAN;
testperson::testperson(/* args */) {}

testperson::~testperson() {}

void testperson::eat(void) { cout << this->name << "-eat" << endl; }
void testperson::work(void) {
  if (this->male == 1) {
    cout << name << "-coding" << endl;
  } else {
    cout << name << "-shopping" << endl;
  }
}
void testperson::sleep(void) { cout << name << "-sleep" << endl; }

int test() {
  testperson xioahong;  //局部变量 分配在栈上
  testperson *xiaoming =
      new testperson();  // 动态内存 分配在自由内存空间,其实就是对堆上
                         // 自己管理内存

  xiaoming->name = "jiajia";
  xiaoming->age = 99;
  xiaoming->male = true;

  xiaoming->eat();
  xiaoming->work();
  xiaoming->eat();
  xiaoming->work();
  xiaoming->sleep();
  return 0;
}

代码:

testperson xiaoming = new testperson();

报错:

error: conversion from ‘testperson*’ to non-scalar type ‘testperson’
requested 1071 | new testperson();version from ‘testperson*’ to
non-scalar type ‘testperson’ requested 1071 | new testperson();

修改:

testperson *xiaoming = new testperson();

  • C++面向对象式编程总结
    (1)整个工作分为2大块:一个是建模和编写类库,一个是使用类库来编写主程序完成任务。
    (2)有些人只负责建模和编写类库,譬如开发opencv的人。
    (3)有些人直接调用现成类库来编写自己的主任务程序,譬如使用opencv分析一张图片中有没有电动车
    (4)难度上不确定,2个都可能很难或者很简单。

  • C++学习的三重境界
    (1)学习C++第一重境界就是语法层面,先学会如何利用C++来建模、来编程,学习语法时先别解决难度大问题。
    (2)学习C++第二重境界是解决问题层面,学习如果理解并调用现成类库来编写主程序解决问题。
    (3)学习C++第三重境界是编写类库和sample给别人用,需要基础好且有一定架构思维。

在构造和析构函数中使用动态内存

析构函数的使用
(1)析构函数在对象对销毁时自动调用,一般有2种情况
(2)用new分配的对象,用delete显式析构
(3)分配在栈上的对象,当栈释放时自动析构
(4)普通情况下析构函数都是空的,因为不必做什么特别的事情

class testperson {
 public:
  //属性
  string name;
  int age;
  bool male;  //性别 男性 ture

  //方法
  void eat(void);
  void work(void);
  void sleep(void);

  testperson(/* args */);
  ~testperson();
};
testperson::testperson(/* args */) { cout << name << "1" << endl; }

testperson::~testperson() { cout << name << "2" << endl; }

void testperson::eat(void) { cout << this->name << "-eat" << endl; }
void testperson::work(void) {
  if (this->male == 1) {
    cout << name << "-coding" << endl;
  } else {
    cout << name << "-shopping" << endl;
  }
}
void testperson::sleep(void) { cout << name << "-sleep" << endl; }

int test0625006() {
  testperson *xiaohong1 =
      new testperson();  // 动态内存 分配在自由内存空间,其实就是对堆上
                         // 自己管理内存
  xiaohong1->name = "jiajia";
  xiaohong1->age = 99;
  xiaohong1->male = true;

  xiaohong1->eat();
  xiaohong1->work();
  xiaohong1->eat();
  xiaohong1->work();
  xiaohong1->sleep();
  delete xiaohong1;  //添加才会执行析构函数

  testperson xiaohong;  //局部变量 分配在栈上
  xiaohong.name = "meimei";
  xiaohong.age = 99;
  xiaohong.male = true;

  xiaohong.eat();
  xiaohong.work();
  xiaohong.eat();
  xiaohong.work();
  xiaohong.sleep();

  return 0;
}
  • 在class中使用动态内存变量
    (1)什么情况下用动态内存?需要大块内存,且需要按需灵活的申请和释放,用栈怕爆、用全局怕浪费和死板时
    (2)在class person中增加一个int *指针,用于指向一个int类型元素的内存空间
    (3)在构造函数中分配动态内存
    (4)在析构函数中回收动态内存
    (5)将动态内存从int变量升级到int数组变量
    (6)实战中C++常用的动态内存往往是容器vector那些,课程第3部分会讲到
class testperson {
 public:
  //属性
  string name;
  int age;
  bool male;  //性别 男性 ture
  int *pint;


  //方法
  void eat(void);
  void work(void);
  void sleep(void);

  testperson(/* args */);
  ~testperson();
};
testperson::testperson(/* args */) {
  this->pint = new int(99);//分配、初始化

  cout << name << "1" << endl;
}

testperson::~testperson() {
  cout << name << "2" << endl;
  delete this->pint;//回收
}

void testperson::eat(void) { cout << this->name << "-eat" << endl; }
void testperson::work(void) {
  if (this->male == 1) {
    cout << name << "-coding" << endl;
  } else {
    cout << name << "-shopping" << endl;
  }
  cout << "this->pint:" << *(this->pint)  << endl;//使用
}
void testperson::sleep(void) { cout << name << "-sleep" << endl; }

int test0625006() {
  testperson *xiaohong1 =
      new testperson();  // 动态内存 分配在自由内存空间,其实就是对堆上
                         // 自己管理内存
  xiaohong1->name = "jiajia";
  xiaohong1->age = 99;
  xiaohong1->male = true;

  xiaohong1->eat();
  xiaohong1->work();
  xiaohong1->eat();
  xiaohong1->work();
  xiaohong1->sleep();
  delete xiaohong1;  //添加才会执行析构函数

  testperson xiaohong;  //局部变量 分配在栈上
  xiaohong.name = "meimei";
  xiaohong.age = 99;
  xiaohong.male = true;

  xiaohong.eat();
  xiaohong.work();
  xiaohong.eat();
  xiaohong.work();
  xiaohong.sleep();

  return 0;
}

申请更多的空间

class testperson {
 public:
  //属性
  string name;
  int age;
  bool male;  //性别 男性 ture
  int *pint;
  char *pchar;

  //方法
  void eat(void);
  void work(void);
  void sleep(void);

  testperson(/* args */);
  ~testperson();
};
testperson::testperson(/* args */) {
  this->pint = new int(99);     //分配、初始化
  this->pchar = new char[20];  //分配、初始化
  cout << name << "1" << endl;
}

testperson::~testperson() {
  cout << name << "2" << endl;
  delete this->pint;   //回收
  delete[] this->pchar;  //回收
}

void testperson::eat(void) { cout << this->name << "-eat" << endl; }
void testperson::work(void) {
  if (this->male == 1) {
    cout << name << "-coding" << endl;
  } else {
    cout << name << "-shopping" << endl;
  }
  cout << "this->pint:" << *(this->pint) << endl;  //使用
  for (size_t i = 0; i < 20; i++) {
    *(this->pchar) = 'a'+ i;
    cout << "this->pchar:" << *(this->pchar) << endl;  //使用
  }
}
void testperson::sleep(void) { cout << name << "-sleep" << endl; }

int test0625006() {
  testperson *xiaohong1 =
      new testperson();  // 动态内存 分配在自由内存空间,其实就是对堆上
                         // 自己管理内存
  xiaohong1->name = "jiajia";
  xiaohong1->age = 99;
  xiaohong1->male = true;

  xiaohong1->eat();
  xiaohong1->work();
  xiaohong1->eat();
  xiaohong1->work();
  xiaohong1->sleep();
  delete xiaohong1;  //添加才会执行析构函数

  testperson xiaohong;  //局部变量 分配在栈上
  xiaohong.name = "meimei";
  xiaohong.age = 99;
  xiaohong.male = true;

  xiaohong.eat();
  xiaohong.work();
  xiaohong.eat();
  xiaohong.work();
  xiaohong.sleep();

  return 0;
}

在这里插入图片描述

用valgrind工具查看内存泄漏
(1)valgrind工具介绍:参考:https://blog.csdn.net/u012662731/article/details/78652651
(2)安装:

sudo apt-get install valgrind

(ubuntu16.04 X64)sudo apt-get install valgrind
(3)编译程序:主要是添加-g参数便于调试时有行号 g++ person.cpp main.cpp -g -o apptest
(4)使用:valgrind --tool=memcheck --leak-check=full --show-reachable=yes --trace-children=yes ./app

  • valgrind和Cmake工程结合
    1、在CMakeLists.txt文件添加
SET(CMAKE_BUILD_TYPE "Debug")

2、使用下面语句生成了日志3_g,将test换成你自己的工程名称

valgrind --leak-check=yes --log-file=3_g ./build/test

总结:

学会分文件创建一个类,并且使用起来
学会使用valgrind查看日志

学习记录,侵权联系删除。
来源:朱老师物联网大课堂

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【SSL 1056】最大子矩阵 (多维DP)

题目大意 已知矩阵的大小定义为矩阵中所有元素的和。给定一个矩阵&#xff0c;你的任务是找到最大的非空&#xff08;大小至少是 1 ∗ 1 1*1 1∗1&#xff09;子矩阵。 比如&#xff0c;如下 4 ∗ 4 4*4 4∗4 子矩阵 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 -2 的最大子矩阵是 …

构建LangChain应用程序的示例代码:53、利用多模态大型语言模型在RAG应用中处理混合文档的示例

许多文档包含多种内容类型&#xff0c;包括文本和图像。 然而&#xff0c;在大多数 RAG 应用中&#xff0c;图像中捕获的信息都会丢失。 随着多模态LLMs的出现&#xff0c;比如GPT-4V&#xff0c;如何在RAG中利用图像是值得考虑的。 本篇指南的亮点是&#xff1a; 使用非结…

Airflow: 大数据调度工具详解

欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;欢迎订阅相关专栏&#xff1a; 欢迎关注微信公众号&#xff1a;野老杂谈 ⭐️ 全网最全IT互联网公司面试宝典&#xff1a;收集整理全网各大IT互联网公司技术、项目、HR面试真题. ⭐️ AIGC时代的创新与未来&a…

国产芯片方案/蓝牙咖啡电子秤方案研发

咖啡电子秤芯片方案精确值可做到分度值0.1g的精准称重,并带有过载提示、自动归零、去皮称重、压低报警等功能&#xff0c;工作电压在2.4V~3.6V之间&#xff0c;满足于咖啡电子秤的电压使用。同时咖啡电子秤PCBA设计可支持四个单位显示&#xff0c;分别为&#xff1a;g、lb、oz、…

stm32——定时器级联

在STM32当中扩展定时范围&#xff1a;单个定时器的定时长度可能无法满足某些应用的需求。通过级联&#xff0c;可以实现更长时间的定时&#xff1b;提高定时精度&#xff1a;能够在长定时的基础上&#xff0c;通过合理配置&#xff0c;实现更精细的定时控制&#xff1b;处理复杂…

Git安装以及环境配置(详细)

一、Git下载 1.官网&#xff08;但是很慢&#xff09; https://git-scm.com/ 2.镜像版&#xff08;比较推荐&#xff09; CNPM Binaries Mirror 里边多个选择合适的进行下载&#xff08;不要选带有rc0,rc1的&#xff0c;都是预发布版本&#xff09; 进入后如下&#xff0c…

【LeetCode】十一、滑动窗口:长度最小的子数组 + 定长子串的元音最大数目

文章目录 1、滑动窗口2、leetcode209&#xff1a;长度最小的子数组3、leetcode1456&#xff1a;定长子串中元音的最大数目 1、滑动窗口 如下&#xff0c;有一个数组&#xff0c;现三个元素为一组&#xff0c;求最大的和&#xff0c;自然可以while循环实现&#xff1a;i 、i1、…

无线领夹麦克风哪个品牌好,推荐口碑最好的麦克风品牌

在5G网络普及的浪潮下&#xff0c;短视频平台的兴起带动了一股全民创作的热潮。无论是城市街头还是乡间小径&#xff0c;人们纷纷拿起手机&#xff0c;记录生活中的点点滴滴。领夹式麦克风凭借其精准的拾音特性和稳定的信号传输&#xff0c;无论是在静止状态还是在移动过程中&a…

【MindSpore学习打卡】应用实践-计算机视觉-SSD目标检测:从理论到实现

在计算机视觉领域&#xff0c;目标检测是一个至关重要的任务。它不仅要求识别图像中的目标物体&#xff0c;还需要精确定位这些物体的位置。近年来&#xff0c;随着深度学习技术的飞速发展&#xff0c;各种高效的目标检测算法层出不穷。SSD&#xff08;Single Shot MultiBox De…

Elasticsearch 使用误区之二——频繁更新文档

在使用 Elasticsearch 时&#xff0c;频繁更新文档是一种常见误区。这不仅影响性能&#xff0c;还可能导致系统资源的浪费。 理解 Elasticsearch 的文档更新机制对于优化性能至关重要。 关于 Elasticsearch 更新操作&#xff0c;常见问题如下&#xff1a; ——https://t.zsxq.c…

2024年显著性检测部分论文及代码汇总(3)

ICML Size-invariance Matters: Rethinking Metrics and Losses for Imbalanced Multi-object Salient Object Detection code Abstacrt&#xff1a;本文探讨了显著性检测中评价指标的尺寸不变性&#xff0c;尤其是当图像中存在多个大小不同的目标时。作者观察到&#xff0c;…

Elasticsearch集群部署(上)

目录 前言 一. 环境准备 二. 实施部署 三. 安装配置head监控插件 &#xff08;只在第一台es部署&#xff09; 四. Kibana部署&#xff08;当前还是在第一台es部署&#xff09; 五. 安装配置Nginx反向代理 六. Logstash部署与测试 下篇&#xff1a;Elasticsearch集群部…

汽配企业MES管理系统的四大应用场景

在当今快速迭代的汽车工业领域&#xff0c;一家位于繁华工业园区的中型汽配制造企业正经历着前所未有的变革。该企业&#xff0c;作为汽车发动机零部件的重要供应商&#xff0c;其客户网络遍布国内外&#xff0c;与多家知名汽车制造商保持着紧密的合作关系。然而&#xff0c;随…

WordPress主题大前端DUX v8.7源码下载

全新&#xff1a;用户注册流程&#xff0c;验证邮箱&#xff0c;设置密码 新增&#xff1a;列表显示小视频和横幅视频 新增&#xff1a;文章内容中的外链全部增加 nofollow 新增&#xff1a;客服功能中的链接添加 nofollow 优化&#xff1a;产品分类的价格显示

JavaScript中的this指向

1. 全局环境下的this 在全局环境中&#xff08;在浏览器中是window对象&#xff0c;在Node.js中是global对象&#xff09;&#xff0c;this指向全局对象。 console.log(this window); // 在浏览器中为true console.log(this.document ! undefined); // true&#xff0c;因为…

测试引擎模拟接口实战

在上一章的内容中&#xff0c;我简单介绍了整个微服务的各个子模块&#xff0c;还封装了一些工具类。 当然&#xff0c;若还没完成上次内容的也可以点击右侧的传送门------传送门 EngineApplication 在开发测试引擎模拟接口之前&#xff0c;还需要给xxx-engine创建一个Sprin…

盛元广通打造智慧校园实验室安全管理系统

盛元广通智慧校园实验室安全管理系统以安全为重点&#xff0c;构建由学校、二级单位、实验室组成的三级联动的实验室安全多级管理体系、多类用户角色&#xff0c;内置教育部标准检查表&#xff0c;支撑实验室相关业务过程的智慧管理。实现通过PC端/手机移动端开展检查工作、手机…

上位机图像处理和嵌入式模块部署(mcu项目1:实现协议)

【 声明&#xff1a;版权所有&#xff0c;欢迎转载&#xff0c;请勿用于商业用途。 联系信箱&#xff1a;feixiaoxing 163.com】 这种mcu的嵌入式模块理论上都是私有协议&#xff0c;因为上位机和下位机都是自己开发的&#xff0c;所以只需要自己保证上、下位机可以通讯上&…

【SQL】⼀棵 B+树能存储多少条数据

B树的存储容量取决于多个因素&#xff0c;包括树的阶&#xff08;即每个节点的最大子节点数&#xff09;、键的大小和每个节点的容量。计算一棵B树能存储多少条数据&#xff0c;通常需要了解以下参数&#xff1a; 节点大小&#xff1a;一般情况下&#xff0c;节点大小等于数据…

2024Datawhale-AI夏令营——机器学习挑战赛——学习笔记

#ai夏令营#datawhale#夏令营 Day1:入门级demo运行 这个其实比较简单&#xff0c;按照操作来做就行了&#xff0c;特征工程和调参暂时都没有做&#xff0c;后续的才是重头戏。 Day2:正式比赛开始 赛题&#xff1a;数据挖掘赛道——利用机器学习方法根据给定的特征判断PROTACs…