C++和C语言区别(二)附二级指针的动态内存申请

2023-05-16

结构体区别

1.定义结构体与C语言一致

2.定义变量可省略关键字struct

3.C++结构体中允许函数存在(C++在没有写构造函数和权限限定的时候,用法和C语言一致)

(1)函数可以直接在结构体中实现,也可在结构体中声明,在结构体外实现,

(2)结构体中函数可以直接访问结构体中数据

(3)学会调用:

         对象(结构体变量).成员

         对象指针->成员

         (*对象指针).成员

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

struct MM
{
	//属性,特性    数据成员
	char name[20];
	int age;
	void print()//行为方法      成员函数
	{
		cout << name << "\t" << age << endl;
	}
	void printData();//结构体中声明
	int& putage()
	{
		return age;
	}
};

void MM::printData()//在结构体外实现
{
	cout << name << "\t" << age << endl;
}

//结构体中的变量必须要通过结构体变量(结构体指针)访问
//C++结构体中的函数可以直接访问属性
int main()
{
	MM a = {"tatata",21};//等效于struct MM a;
	a.print();
	(&a)->printData();
	MM* p = &a;
	p->printData();
	p->putage() = 22;
	p->printData();
	p->age = 23;
	p->print();
    MM arr[3];
	return 0;
}

动态内存申请

1.C语言的动态内存申请

(1)malloc 不带初始化,calloc 带初始化的,realloc 重新申请,free 释放

2.C++的动态内存申请

(1)new 申请,delete 释放

(2)单个动态内存申请

(3)数组动态内存申请

(4)结构体动态内存申请

#include <iostream>
#include <cstring>
using namespace std;

void testonememory()
{
	//申请不做初始化
	int* pint = new int;
	*pint = 123;
	cout << *pint << endl;
	//申请并做初始化   ()给单个数据初始化
	char* pchar = new char('a');
	cout << *pchar << endl;
	delete pint;
	pint = nullptr;
	delete pchar;
	pchar = nullptr;
}

void testarraymemory()
{
	//一维数组不带初始化   长度可以是变量
	int* pint = new int[3];//等效产生了int pint[3]数组
	char* pchar = new char[20];
    //pchar="Iloveyou";只是改变了指针的指向,并没有初始化指针指向的内存段
	strcpy(pchar, "Iloveyou");
	cout << pchar << endl;
	delete[] pint;//数组的释放
	pint = nullptr;
	delete[] pchar;
	pchar = nullptr;
	//带初始化的一维数组   用{}
	int* pInt = new int[3]{1, 2, 3};
	for (int i = 0; i < 3; i++)
	{
		cout << pInt[i] << " ";
	}
	cout << endl;
	delete[] pInt;//指针再次使用
	pInt = nullptr;
}
struct MM
{
	char* name;
	int age;
	void print()
	{
		cout << name << "\t" << age << endl;
	}
};
void teststructmemory()
{
	//new 一个对象   结构体初始化只能用{}
	MM* pstruct = new MM;
	//结构体指针中,要做二次申请,才能进行赋值和strcpy
	pstruct->name = new char[20];
	strcpy(pstruct->name, "丽丝");
	pstruct->age = 21;
	pstruct->print();
	delete[] pstruct->name;
	pstruct->name = nullptr;
	delete pstruct;
	pstruct = nullptr;
}
int main()
{
	testonememory();
	testarraymemory();
	teststructmemory();
	return 0;
}

内存池

1.malloc 内存是在堆区,new内存是自由存储区

#include <iostream>
using namespace std;

void testmemory()
{
	char* memorySum = new char[1024];
	int* pNum= new(memorySum) int[3]{1, 2, 3};
	//char* pstr = new(pNum + 3) char[20]{"Iloveyou"};//等效下面这句话
	char* pstr = new(memorySum + 12) char[20];
	strcpy(pstr, "Iloveyou");
	for (int i = 0; i < 3; i++)
	{
		cout << pNum[i] << " ";
	}
	cout << endl << pstr << endl;
	delete[] memorySum;
	memorySum = nullptr;
}
int main()
{
	testmemory();
	return 0;
}

string类型

1.string创建

(1)带初始化和不带初始化

(2)通过另一个函数创建

#include <iostream>
#include <string>
using namespace std;

void createstring()
{
	std::string str1;//未用using 语法省略
	str1 = "Iloveyou";
	cout << str1 << endl;
	string str2 = "Iloveyou";
	cout << str2 << endl;
	string str3("Iloveyou");
	cout << str3 << endl;
	string str4(str3);
	cout << str4 << endl;
	string str5 = str4;
	cout << str5 << endl;
}

2.string基本操作

(1)拷贝

(2)赋值

(3)链接

(4)比较

#include <iostream>
#include <string>
using namespace std;

void operatorString()
{
	string str1 = "Ilove";//赋值
	string str2 = "You";
	string str3 = str2;//拷贝
	cout << str3 << endl;
	string str4 = str1 + str3;//连接
	cout << str4 << endl;
	if (str1 > str3)//比较
	{
		cout << str1 << "大" << endl;
	}
	else
	{
		cout << str3 << "大" << endl;
	}
}

3.C++string与C语音string.h

(1)C++中是一个自定义类型(类)

(2)C++中的string不能用到C语言的字符串处理函数

(3)C++转换为C语言中的char*:c_str()     data()   

(4)直接把数字转为相应的字符串

#include <iostream>
#include <string>
using namespace std;
#include <cstring>

void compareCandCpp()
{
	string str1 = "Iloveyou";
	printf("%s\n", str1.c_str());
	printf("%s\n", str1.data());
	
	string str2 = to_string(1234);
	cout << str2 << endl;
}

注:empty()          size()

二位数组的动态内存申请

#include <iostream>
#include <assert.h>
using namespace std;
int** createArray2(int row, int cols);//声明

int main()
{
	int** p =createArray2(5, 5);
	for (int i = 0; i < 5; i++)//内存检测
	{
		for (int j = 0; j < 5; j++)
		{
			p[i][j] = i*j;
			cout << p[i][j] << "\t";
		}
		cout << endl;
	}
	return 0;
}

int** createArray2(int row, int cols)
{
	int** PArray = new int*[row];//二级指针动态申请
	assert(PArray);//断言保护
	for (int i = 0; i < row; i++)
	{
		PArray[i] = new int[cols];//一级指针动态申请
		assert(PArray[i]);
	}
	return PArray;
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++和C语言区别(二)附二级指针的动态内存申请 的相关文章

随机推荐

  • 深度学习环境入门之手写数字识别

    在自己的windows环境下配置好了深度学习的环境 xff0c 本文主要记录一下用深度学习的环境下实现一个简单的手写数字识别的模型训练和使用 1 在pycharm中配置conda环境 xff1a 环境配置好以后 xff0c 可以开始手写数字
  • 算法之KMP算法 全新思路介绍!

    KMP算法是一个经典的字符串匹配算法 xff0c 也是一种常用的字符串匹配算法 在KMP算法没出现之前 xff0c 大家在字符串匹配的时候 xff0c 都是两个for循环嵌套完成字符串之间的匹配 这种算法称作 BF算法 xff08 暴力求解
  • c++ linux utf-8 编码 中文汉字分割(超简单代码)

    UTF 8 编码对于英文字母 xff0c 占用一个字节 xff1b UTF 8 编码对于中文字母 xff0c 占用多个字节 xff0c 最大占用6个字节 xff0c 其中第一个字节二进制的最高位连续1的个数来表示占用字节的个数 xff0c
  • 算法之并查集

    并查集 xff0c 顾名思义 xff0c 就是合并不同的集合 xff0c 并查集是一种集合合并和查找算法 这是一种思想很奇妙的算法 xff0c 学会它 xff0c 在你后续的程序学习中可以有很多的可以用的地方 什么是并查集 xff1f 举个
  • 算法之主成分分析PCA详解(包含理论推导和代码)

    1 PCA介绍 主成分分析算法 xff08 Principal Component Analysis xff09 简称PCA xff0c 是一种常用的统计方法 该方法对高维的数据进行筛选 xff0c 选出最具有代表性最重要的的几维数据 xf
  • linux 命令行进行桌面图标的打开

    近期在处理一个需求 xff0c 需要在代码中打开桌面的某个图标 xff0c 因此 xff0c 做了一些搜索 xff0c 最终发现 xff0c 有两个比较好用的命令 xff0c 下面来讲解一下 1 gtk launch 在linux系统一般已
  • 算法之滑动窗口寻找最长无重复字符串

    今天无聊的时候刷了一道leetcode的题目 xff0c 给定字符串 xff0c 查找最长无重复字符串 xff0c 具体题目信息如下 xff1a 给定一个字符串 s xff0c 请你找出其中不含有重复字符的 最长子串 的长度 示例 1 输入
  • 算法之图解单纯形算法C++

    在之前的算法博客中 xff0c 结合案例和算法的图形表示 xff0c 获得了较多同学的好评 xff0c 例如之前写的迪杰斯特拉算法这篇博客 xff0c 能够让很多新同学和老同学通过直观的方式去理解算法求解的过程 xff0c 这样理解起来会比
  • linux opencv打开两个USB摄像头

    在ubuntu linux系统下 xff0c 摄像头设备是通过 dev videox来表示的 xff0c 如果只有一个摄像头 xff0c 则会在 dev目录下显示 video0和video1两个设备 xff0c 其中 xff0c video
  • 实现手机app和微信小程序远程控制加人体探测自动化控制51单片机打开流水灯(ESP8266 STC89C52RC http请求转串口通信系统)

    首先你有这样的8266 这种8266自身带2个按键和烧录芯片方便调试 xff0c 综合性价比较高 有一个51单片机 或者其他芯片都行 还有就是需要这种人体红外探测模块和led灯 有了这2个芯片我们开始吧 xff01 1 先看一段视频效果演示
  • 图像算法之图像平移

    在图像处理领域中 xff0c 图像变换是最基本的算法 xff0c 本文主要详细分享一下图像平移算法的原理和代码实现 xff0c 废话不多说 xff0c 直接上干货 1 图像平移的原理 在计算机图像中 xff0c 主要是用的是 像素直角坐标系
  • OpenCV之Mat的详细介绍

    在opencv中 xff0c Mat作为图像的存储容器 xff0c 是非常基础也是非常重要的知识 xff0c 本文主要介绍Mat相关的操作 1 图像复制 再开始将拷贝之前 xff0c 先给大家分享一下浅拷贝和深拷贝 浅拷贝 xff1a 拷贝
  • windows下Linaro-arm-linux编译开启NEON的Opencv源码详细过程

    环境配置 1 windows安装cmake 下载地址 xff1a https cmake org download 按照上图中的下载cmake的安装包 xff0c 安装注意事项如下 xff1a 安装路径切记不能包含空格 xff01 xff0
  • Windows 工具之net

    net 是windows平台下用户管理的工具 xff0c 可以来添加 xff0c 删除和修改用户 1 创建用户 xff1a win 43 R 输入cmd xff0c 然后打开命令行窗口 xff0c 输入下面的命令 xff1a net use
  • windows平台相关命令收集

    1 获取IP地址命令 xff1a ipconfig 2 获取MAC地址命令 xff1a ipconfig all 3 查看所有端口信息 netstat nal findstr 8080
  • C/C++中比较好用的HTTP开源库

    以下是一些常用的C C 43 43 开源HTTP库及其对应的开源协议和链接 xff1a 1 libcurl xff1a 使用MIT X开源协议 xff0c 支持多种协议和多种数据传输方式 xff0c 被广泛应用于各种开发场景 官网链接 xf
  • 为Ubuntu网页设置稳定的数据隧道

    网站作为互联网世界中的一个个门户 xff0c 是我们对外表达的窗口 xff0c 无数个人和企业通过这个窗口传递着信息 xff0c 这个传递信息的窗口必须稳定持续存在 xff0c 才能让更多访客看到 xff0c 为我们带来更多的流量 而使用c
  • VisualStudio配置与Matlab混合编译问题总结

    VisualStudio配置与Matlab混合编译问题总结 写这篇博客记录一下自己在配置过程中遇到的种种问题 xff0c 也做一个小小总结 xff0c 试图把整个过程讲明白 xff0c 希望对面临同样问题的人有所帮助 网上此类教程很多 xf
  • C++与C的区别(作业)

    头文件的区别 1 C 43 43 中创建源文件后缀为 cpp xff0c C语言中创建源文件后缀为 c xff0c 头文件保持不变 xff0c 后缀为 h 2 包含自己的头文件 xff0c C语言与C 43 43 无区别 包含库目录 xff
  • C++和C语言区别(二)附二级指针的动态内存申请

    结构体区别 1 定义结构体与C语言一致 2 定义变量可省略关键字struct 3 C 43 43 结构体中允许函数存在 xff08 C 43 43 在没有写构造函数和权限限定的时候 xff0c 用法和C语言一致 xff09 xff08 1