C++复习day02

embedded/2024/9/24 10:21:07/

一、库函数的模拟实现

1.memcpy的使用和模拟

首先先来了解一下memcpy函数的作用是什么?

void * memcpy ( void * destination, const void * source, size_t num );
  • 函数memcpy从source的位置开始向后复制num个字节的数据到destination指向的内存位置。
  • 这个函数在遇到 ‘\0’ 的时候并不会停下来。
  • 如果source和destination有任何的重叠,复制的结果都是未定义的
    先举一个没有重叠的例子:
#include <iostream>
#include <cstring>int main()
{int arr1[] = { 1,2,3,4,5,6,7,8 ,9,10 };int arr2[20]{};memcpy(arr2, arr1, sizeof arr1);for (int i = 0; i < 20; ++i)std::cout << arr2[i] << ' ';//1 2 3 4 5 6 7 8 9 10 0 0 0 0 0 0 0 0 0 0return 0;
}

再举一个重叠的例子:

#include <iostream>
#include <cstring>int main()
{int arr[20] = { 1,2,3,4,5,6,7,8 ,9,10 };memcpy(arr + sizeof(int), arr, sizeof(int)* 10);for (int i = 0; i < 20; ++i)std::cout << arr[i] << ' ';//1 2 3 4 1 2 3 4 5 6 7 8 9 10 0 0 0 0 0 0return 0;
}

很明显,这种结果并不是我们想要的结果,所以memcpy看来不适合用于有重叠的拷贝。有重叠的拷贝就应该使用memmove来操作
下面来模拟一下memcpy函数

#include <iostream>
#include <cstring>
#include <assert.h>void* my_memcpy(void* dst, void* src, size_t count)
{void* ret = dst;assert(dst);assert(src);for (size_t i = 0; i < count; ++i){*(char*)dst = *(char*)src;dst = (char*)dst + 1;src = (char*)src + 1;}return ret;
}
int main()
{int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };int arr2[20] = { 0 };my_memcpy(arr2, arr1, sizeof arr1);int i = 0;for (i = 0; i < 20; i++){printf("%d ", arr2[i]);}//1 2 3 4 5 6 7 8 9 10 0 0 0 0 0 0 0 0 0 0return 0;
}
2.memmove的使用和模拟
void * memmove ( void * destination, const void * source, size_t num );
  • 和memcpy的差别就是memmove函数处理的源内存块和⽬标内存块是可以重叠的
  • 如果源空间和⽬标空间出现重叠,就得使⽤memmove函数处理。
    memmove的使用:
#include <iostream>
#include <cstring>
#include <assert.h>int main()
{int arr[] = { 1,2,3,4,5,6,7,8,9,10 };memmove(arr + 2, arr, sizeof(int) * 3);for (int i = 0; i < 10; ++i)std::cout << arr[i] << ' ';//1 2 1 2 3 6 7 8 9 10return 0;
}

memmove的实现
很明显倒叙拷贝就可以了,下面给出代码:

#include <iostream>
#include <cstring>
#include <assert.h>
void* my_memmove(void* dst, void* src, size_t count)
{void* ret = dst;//判空assert(dst);assert(src);if (dst < src){//直接正序拷贝就可以了for (size_t i = 0; i < count; ++i){*(char*)dst = *(char*)src;dst = (char*)dst + 1;src = (char*)src + 1;}}else {//需要倒叙拷贝//求出结束的位置void* ddst = (char*)dst + count;void* ssrc = (char*)src + count;for (size_t i = 0; i < count; ++i){*(char*)dst = *(char*)src;dst = (char*)dst - 1;src = (char*)src - 1;}}return ret;
}
void test1()
{int arr[] = { 1,2,3,4,5,6,7,8,9,10 };my_memmove(arr + 1, arr + 2, sizeof(int) * 5);for (int i = 0; i < 10; ++i)std::cout << arr[i] << ' ';
}
void test2()
{int arr[] = { 1,2,3,4,5,6,7,8,9,10 };my_memmove(arr + 1, arr + 2, sizeof(int) * 5);for (int i = 0; i < 10; ++i)std::cout << arr[i] << ' ';
}int main()
{test1();//1 3 4 5 6 7 7 8 9 10std::cout << std::endl;test2();//1 3 4 5 6 7 7 8 9 10return 0;
}
3.strstr的使用和模拟
char *strstr( const char *string, const char *strCharSet );

来演示一下strstr的用法

#include <iostream>
#include <cstring>
#include <assert.h>int main()
{const char* s = "hello world";std::cout << strstr(s, "world") << std::endl;//worldreturn 0;
}

下面来实现一下strstr函数

这里的strstr暂时不用kmp算法实现,等到回头复习算法的时候会再来写这个strstr的kmp实现方式
#include <iostream>
#include <cstring>
#include <assert.h>
#include <cstdio>char* my_strstr(const char*s1, const char*s2) //非kmp算法
{//s1是被查找的字符串,s2是要查找的字符串assert(s1 && s2);size_t len = strlen(s1);size_t j = 0;for (size_t i = 0; i < len; ++i){if (s1[i] == s2[j]){size_t ii = i;while (s2[j] && s1[ii] == s2[j]){++j, ++ii;}if (s2[j] == '\0')return (char*)(s1 + i);elsej = 0;}}return NULL;
}
int main()
{char s1[20], s2[20];gets_s(s1);gets_s(s2);char* result = my_strstr(s1, s2);if (result){std::cout << result << std::endl;}else {std::cout << "NULL" << std::endl;}//worldreturn 0;
}
4.strlen,strcpy(较为简单,考察的不多)
size_t strlen( const char *string );
char *strcpy( char *strDestination, const char *strSource );
#include <iostream>
#include <cstring>
#include <assert.h>
#include <cstdio>size_t my_strlen(const char* s)
{const char* cur = s;while (*cur)cur++;return cur - s;
}
int main()
{std::cout << my_strlen("hello") << std::endl;//worldreturn 0;
}
#include <iostream>
#include <cstring>
#include <assert.h>
#include <cstdio>char* my_strcpy(char* dst, const char* src)
{char* start = dst;while (*dst)dst++;size_t i = 0;while (dst[i] = src[i])++i;return start;
}
int main()
{char s1[20] = "hello ";char s2[10] = "world";my_strcpy(s1, s2);std::cout << s1 << std::endl;return 0;
}

二、自定义类型

1.结构体内存对齐

结构体就是一些值的集合
直接上重点,来介绍一下内存对齐
内存对齐的对齐规则:

1. 结构体的第⼀个成员对⻬到和结构体变量起始位置偏移量为0的地址处
2.  其他成员变量要对⻬到某个数字(对⻬数)的整数倍的地址处。**对齐数** = 编译器默认的⼀个对⻬数 与 该成员变量⼤⼩的较⼩值。- VS 中默认的值为 8-  Linux中 gcc 没有默认对⻬数,对⻬数就是成员⾃⾝的⼤⼩
3. 结构体总⼤⼩为最⼤对⻬数(结构体中每个成员变量都有⼀个对⻬数,所有对⻬数中最⼤的)的
整数倍。
4.如果嵌套了结构体的情况,嵌套的结构体成员对⻬到⾃⼰的成员中最⼤对⻬数的整数倍处,结构
体的整体⼤⼩就是所有最⼤对⻬数(含嵌套结构体中成员的对⻬数)的整数倍。
//练习
//练习1
struct S1
{char c1;int i;char c2;
};
printf("%d\n", sizeof(struct S1));//练习2
struct S2
{char c1;char c2;int i;
};
printf("%d\n", sizeof(struct S2));//练习3
struct S3
{double d;char c;int i;
};
printf("%d\n", sizeof(struct S3));//练习4-结构体嵌套问题
struct S4
{char c1;struct S3 s3;double d;
};
printf("%d\n", sizeof(struct S4));
2.联合体(共用体)
像结构体⼀样,联合体也是由⼀个或者多个成员构成,这些成员可以不同的类型。
但是编译器只为最⼤的成员分配⾜够的内存空间。联合体的特点是所有成员共⽤同⼀块内存空间。所
以联合体也叫:共⽤体。
给联合体其中⼀个成员赋值,其他成员的值也跟着变化。
#include <stdio.h>union Un
{char c;int i;
};
int main()
{union Un u = { 0 };u.c = 'a';printf("%d\n", u.i); // 97printf("%d\n", sizeof u); //4return 0;
}

http://www.ppmy.cn/embedded/105399.html

相关文章

电脑连接公司服务器记住了账户密码,怎么换账户呢?

今天&#xff0c;有同事找到我&#xff0c;说共享连不上去了&#xff0c;我去试了下&#xff0c;知道了原因&#xff1a;由于我将之前使用这台电脑的人的账户在后台禁用了&#xff0c;所以这台电脑连不上服务器了&#xff0c;也不是连不上&#xff0c;而是之前是记住了账户密码…

Python使用总结之Flask-SocketIO介绍

Python使用总结之Flask-SocketIO介绍 一、Flask-SocketIO简介 Flask-SocketIO 是一个基于 Flask 的扩展库&#xff0c;用于在 Flask 应用中实现 WebSocket 通信。WebSocket 是一种双向通信协议&#xff0c;允许服务器和客户端之间在不重新建立连接的情况下进行实时数据交换。这…

【SpringBoot】自动配置原理

Spring Boot 自动配置原理是 Spring Boot 提供简化 Spring 应用程序开发的核心特性之一。 它使得开发者无需编写大量的配置代码就能让应用程序具有很多常见的功能。 以下是 Spring Boot 自动配置的基本原理和工作流程&#xff1a; 通过 SpringBootConfiguration 引入了 Enabl…

net、udp、tcp

Makefile的main.c文件中的全局变量SONG song,要在fun.c文件里面写成extern SONG song 编译方法 第一次编写 或 网络编程 物理层的网线规定有八根,颜色不一样,功能不一样,光猫把光信号转换成电信号,光纤10Gb WiFi叫无线局域网,一般也就50米左右,手机流量叫蜂窝网络,…

数据库水平分表方案

数据库分表有很多策略&#xff0c;如下&#xff1a; 数据库分表是处理大型数据库中数据量过大的一种常见策略&#xff0c;它可以提高查询性能、减少锁竞争、降低维护成本等。以下是一些常见的数据库分表方案&#xff1a; 1. **垂直分表&#xff08;Vertical Partitioning&…

【Python机器学习】NLP词频背后的含义——隐性语义分析

隐性语义分析基于最古老和最常用的降维技术——奇异值分解&#xff08;SVD&#xff09;。SVD将一个矩阵分解成3个方阵&#xff0c;其中一个是对角矩阵。 SVD的一个应用是求逆矩阵。一个矩阵可以分解成3个最简单的方阵&#xff0c;然后对这些方阵求转置后再把它们相乘&#xff…

gitlab 包含模型文件,比较大,怎么上传

当你的 GitLab 项目包含较大的模型文件或其他大文件时&#xff0c;直接上传可能会遇到一些限制。你可以使用以下几种方法来处理&#xff1a; 方法 1&#xff1a;调整 Git 的文件大小限制 调整 GitLab 的限制&#xff1a; 如果你有权限管理 GitLab 实例&#xff0c;你可以调整 …

UDP英译汉网络词典

这里我们用UDP实现一个简单的英译汉小词典。我们还是仿照前一篇的UDP编程&#xff0c;将各自的组件封装起来&#xff0c;实现高内聚低耦合。 一. 字典翻译功能实现 首先我们将我们的字典知识库放在txt文本中。 apple: 苹果 banana: 香蕉 cat: 猫 dog: 狗 book: 书 pen: 笔 ha…