C/C++基础补充

1. NULL和nullptr

有如下代码:

void func(int a) {
	cout << "func(int)" << endl;
}

void func(int* p) {
	cout << "func(int*)" << endl;
}

void test() {
	func(0);       // func(int);
	func(NULL);    // func(int);
	func(nullptr); // func(int*);
}

因为NULL为0的宏定义,因此如上函数重载, func(NULL)会优先调用func(int);


2. 头文件防卫式声明

防止头文件重复包含,建议头文件加上如下预编译格式:

#ifndef __HEAD__H__
#define __HEAD__H__

/* ............. */

#endif

3. 范围for

示例1:

int v[] { 1, 2, 11, 22, 33 };
for(auto x : v) {  // 遍历数组v,将v中元素拷贝到x中
    cout << x << endl;
}

int v[] { 1, 2, 11, 22, 33 };
for(auto& x : v) {  // 遍历数组v,无需拷贝
    cout << x << endl;
}

示例2:

for(auto x : { 1, 2, 11, 22, 33 }) {  // 遍历数组
    cout << x << endl;
}

4. strcpy和strcpy_s

strcpy:不检查目标字符串是否有足够的空间,可能导致目标字符串溢出;

strcpy_s:第二个参数指定目标字符串大小,会检查源字符串长度是否超过目标缓冲区长度。

// strcpy函数原型,头文件为<string.h>
char* strcpy(char* strDestination, const char* strSource);
/*
参数:
    strDestination:目标字符串地址;
    strSource:源字符串地址;
返回值:
    成功:strDestination
    失败:未定义错误,但可能导致缓冲区溢出
*/

// strcpy_s函数原型,头文件为<cstring>
errno_t strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource);
/*
参数:
    strDestination:目标字符串地址;
    numberOfElements:strDestination的长度;
    strSource:源字符串地址;
返回值:
    成功:0
    失败:非0错误码
*/

5. 动态内存分配

5.1 C:malloc和free

void* malloc(int BytesNum);
/*
参数:
    BytesNum:要分配的字节数
返回值:
    成功:被分配内存的指针;
    失败:NULL
*/

void free(void* addr);
/*
释放malloc分配的内存
*/

示例1:

int* p = NULL;
p = (int*)malloc(sizeof(int)); // 在堆中分配4字节
if(p != NULL) {
    *p = 5;
    cout << *p << endl;
    free(p);  // 释放内存
}

示例2:

char* p = NULL;
p = (char*)malloc(100 * sizeof(char)); 
if(p != NULL) {
    strcpy_s(p, 100, "hello!");
    cout << p << endl;
    free(p);
}

示例3:

int* p = (int*)malloc(sizeof(int) * 100); // 分配可放100个整数的内存空间
if(p != NULL) {
    int* q = p;
    *q++ = 1; // 等价于 *(q++) = 1; 即 *q = 1; q++(q指向第二个整数地址)
    free(p);
}

5.2 C++:new和delete

new使用方式:

// 方式1
指针变量名 = new 类型标识符;

// 方式2
指针变量名 = new 类型标识符(初始值);

// 方式3
指针变量名 = new 类型标识符[该类型的内存单元个数];

// new分配内存失败
/*
若未指定nothrow标志,分配失败会抛出std::bad_alloc异常,可用try-catch语句来捕获和处理;
若指定了nothrow标志,分配失败会返回空指针,可用if语句来检查和处理。
*/

示例:

// 示例1
int* p = new int;
if(p != NULL) {
    *p = 8;
    cout << *p << endl;
    delete p;
}

// 示例2
int* p1 = new int(10);
if(p1 != NULL) {
    cout << *p1 << endl;
    delete p1;
}

// 示例3
int *pa = new int[100];  // 开辟有100元素的整型数组空间
if(pa != NULL) {
    int* q = pa;
    *q++ = 10; // [0] = 10
    *q++ = 20; // [1] = 20;
    cout << *pa << endl; // 10
    cout << *(pa+1) << endl; // 20
    delete[] pa;
}

6. C++的struct和class

structclass
成员默认权限publicprivate
默认继承权限publicprivate

7. inline内联函数

与普通函数不同,内联函数的定义需放在头文件中。

inline可能带来代码膨胀问题,消耗内存。 


8. 函数用法补充

8.1 一个返回值类型为void的函数可以返回一个返回值类型为void的函数

示例1:

void funcA(int a) {
	cout << a << endl;
	a--;
	if (a > 0) {
		return funcA(a);
	}
}

void test() {
    funcA(3);
}

运行结果:

示例2: 

void funcB() {
	cout << "Hello" << endl;
}

void funcC() {
	return funcB();
}

void test() {
    funcC();
}

运行结果:


8.2 函数形参之前尽量加const

优点:

(1)避免无意修改形参;

(2)调用更灵活;

void testNoConst(int& i) {  }

void testConst(const int& i) {  }

void test() {
	int b = 20;
	testNoConst(b);  // 正确
	testConst(b);    // 正确

	testNoConst(1);  // 错误
	testConst(1);    // 正确

	const int a = 10;
	testNoConst(a);  // 错误
	testConst(a);    // 正确
}

待补充 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伟大的马师兄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值