C++高性能并行编程与优化 - 课件 - 12 从计算机组成原理看 C 语言指针(typedef) ,这些别名保证不论是什么操作系统什么架构,都是 固定的大小,例如: • typedef char int8_t; • typedef short int16_t; • typedef int int32_t; • typedef long long int64_t; • 这样不论操作系统对类型的定义如何混乱,这些标准化的类型都是确定的大小。 • 这就避免了跨平台的麻烦,而且直接 • 除了有符号的 int32_t 系列外,也提供了无符号 uint32_t 系列: • typedef unsigned char uint8_t; • typedef unsigned short uint16_t; • typedef unsigned int uint32_t; • typedef unsigned long long uint64_t; 标准化的类型: stdint stdint.h 类型 大小 是否有符号 int8_t 8 位 有 int16_t 16 位 有 int32_t 32 位 有 int64_t 64 位 有 uint8_t 8 位 无 uint16_t 16 位 无 uint32_t 32 位 无 uint64_t 64 位 无 intptr_t 和 uintptr_t :自动随系统位数决定大小 • 刚刚说过,计算机的位数决定了内存地址的大小。0 码力 | 128 页 | 2.95 MB | 1 年前3
C++20: An (Almost) Complete Overviewkeywords: concept requires constinit consteval co_await co_return co_yield char8_t New identifiers: import moduleModules7 Modules Advantages Replace header files Modules main.cpp import cppcon; int main() { std::cout << CppCon::GetWelcome(); }9 Modules C++20 doesn’t specify if and how to modularize the Standard Library in bigger modules But, all C++ headers are for example: std::experimental::generator<T>18 Coroutines Example (VC++): experimental::generatorGetSequenceGenerator( int startValue, size_t numberOfValues) { for (int i { startValue 0 码力 | 85 页 | 512.18 KB | 6 月前3
C++高性能并行编程与优化 - 课件 - 15 C++ 系列课:字符与字符串e 上期回顾: https://www.bilibili.com/video/BV1m34y157wb 课程安排 1. vector 容器初体验 & 迭代器入门 (BV1qF411T7sd) 2. 你所不知道的 set 容器 & 迭代器分类 (BV1m34y157wb) 3. string , string_view , const char * 的爱恨纠葛 (printable character) 外, ASCII 还规定了一 类特殊的控制字符 (control character) : • 0 表示空字符(‘ \0’ ) • 9 表示 Tab 制表符(‘ \t’ ) • 10 表示换行(‘ \n’ ) • 13 表示回车(‘ \r’ ) • 27 表示 ESC 键(‘ \x1b’ ) • 127 表示 DEL 键(‘ \x7f’ )等 • 0~31 和 127 以及按 Ctrl+I 的效果其实和 Tab 键一样,按 Ctrl+J 的效果和 Enter 键一样,按 Ctrl+H 的效果和退格键 一样。 • 这是因为 ASCII 表中规定 ^I 就是 ‘ \t’ , ^J 就是 ‘ \ n’ , ^H 就是 ‘ \b’ ,所以以前原始的计算机键盘上其 实还没有 Enter 键,大家都是按 Ctrl+J 来换行的… … • 不过,如果直接在控制台输入 ‘ ^’0 码力 | 162 页 | 40.20 MB | 1 年前3
Making Libraries Consumable for Non-C++ DevelopersEnabling two or more disparate entities to work together. Don’t touch it! Glue code? Boiler plate? A nightmare?What is interoperability? Don’t touch it! Glue code? Boiler plate? A nightmare? Application */ size_t open_device(char const* dev); size_t open_device(std::wstring_view const dev); The types char and wchar_t do not indicate encoding. The size of wchar_t: • Windows, sizeof(wchar_t) == 2 • • Non-Windows, sizeof(wchar_t) == 4 std::basic_stringhas memory implications. More on that later.What assumptions are being made? void get_size(size_t dev, long* size); Non-C/C++ language. 0 码力 | 29 页 | 1.21 MB | 6 月前3
Lock-Free Atomic Shared Pointers Without a Split Reference Count? It Can Be Done!know roughly what atomic does and what it is used for • You’ve heard of shared_ptr Things we won’t cover • Alias pointers, weak pointers4 Daniel Anderson -- danielanderson.net What we’ll learn today know roughly what atomic does and what it is used for • You’ve heard of shared_ptr Things we won’t cover • Alias pointers, weak pointers5 Daniel Anderson -- danielanderson.net Why is this important basics) T* ptr; shared_ptr<T> s1: control_block: atomicref_count = 1; … control_block* ctrl; T12 Daniel Anderson -- danielanderson.net std::shared_ptr (the basics) T* ptr; shared_ptr<T> s1: 0 码力 | 45 页 | 5.12 MB | 6 月前3
C++23: An Overview of Almost All New and Updated FeaturesMultidimensional Subscript Operator Attributes on Lambda-Expressions Literal Suffix for size_t auto(x): decay-copy in The Language #elifdef, #elifndef, and #warning Marking Unreachable Code Multidimensional Subscript Operator Attributes on Lambda-Expressions Literal Suffix for size_t auto(x): decay-copy in The Language #elifdef, #elifndef, and #warning Marking Unreachable Code Multidimensional Subscript Operator Attributes on Lambda-Expressions Literal Suffix for size_t auto(x): decay-copy in The Language #elifdef, #elifndef, and #warning Marking Unreachable Code0 码力 | 105 页 | 759.96 KB | 6 月前3
Bringing Existing Code to CUDA Using constexpr and std::pmrstd::pmr::memory_resource { void* do_allocate(std::size_t, std::size_t); void do_deallocate( void* p, std::size_t, std::size_t); bool do_is_equal( const std::pmr::memory_resource& std::pmr::memory_resource { void* do_allocate(std::size_t, std::size_t) final; void do_deallocate( void* p, std::size_t, std::size_t) final; bool do_is_equal( const std::pmr::memory_resource& :do_allocate( std::size_t bytes, std::size_t /*alignment*/) { void* x = nullptr; auto const r = cudaMallocManaged(&x, bytes); if (r != cudaError_t::cudaSuccess) { throw0 码力 | 51 页 | 3.68 MB | 6 月前3
C++20 STL Features: 1 Year of Development on GitHubVersion 1.0 - September 15, 2020 1 C++20 STL Features: 1 Year of Development on GitHub Stephan T. Lavavej "Steh-fin Lah-wah-wade" Principal Software Engineer, Visual C++ Libraries stl@microsoft.com @StephanTLavavej2template T, class U> constexpr bool cmp_equal(T t, U u) noexcept; template T, class U> constexpr bool cmp_not_equal(T t, U u) noexcept; template T, class U> constexpr constexpr bool cmp_less(T t, U u) noexcept; template T, class U> constexpr bool cmp_greater(T t, U u) noexcept; template T, class U> constexpr bool cmp_less_equal(T t, U u) noexcept; 0 码力 | 45 页 | 702.09 KB | 6 月前3
C++高性能并行编程与优化 - 课件 - 03 现代 C++ 进阶:模板元编程令 Int, Float, Double 继承 Numeric 接口类并实现 ,其中 multiply(int) 作为虚函数。然后定义: Numeric *twice(Numeric *t) { return t->multiply(2); } 且不说这样的性能问题,你忍得住寂寞去重复定义好 几个,然后每个运算符都要声明一个纯虚函数吗? 而且, Float 的乘法应该是 multiply(float) 不如放弃类和方法的概念,欣然接受全局函数和重载 。 模板函数:定义 • 使用 templateT> • 其中 T 可以变成任意类型。 • 调用时 twice 即可将 T 替换为 int 。 • 注意有的教材上写做: • template T> • 是完全等价的,只是个人喜好不同。 模板函数:自动推导参数类型 • 那这样需要手动写 来还不如重载方便了? • 别担心, C++ 规定: • 当模板类型参数 T 作为函数参数时,则可 以省略该模板参数。自动根据调用者的参 数判断。 模板函数:特化的重载 • 有时候,一个统一的实现(比如 t * 2 )满 足不了某些特殊情况。比如 std::string 就 不能用乘法来重复,这时候我们需要用 t + t 来替代,怎么办呢? • 没关系,只需添加一个 twice(std::string) 0 码力 | 82 页 | 12.15 MB | 1 年前3
C++高性能并行编程与优化 - 课件 - 13 C++ STL 容器全解之 vectorvector 可以在构造时指定初始长度。 • explicit vector(size_t n); • 例如,要创建一个长度为 4 的 int 型数组 : • vectora(4); • 之后可以通过 a.size() 获得数组的长度。 • 比如右边这段代码会得到 4 。 • size_t size() const noexcept; vector 容器: operator[] a[0] 访问第 0 个元素(人类的第一 个) • 例如 a[1] 访问第 1 个元素(人类的第二 个) • int &operator[](size_t i) noexcept; • int const &operator[](size_t i) const noexcept; vector 容器: operator[] • 值得注意的是, [] 运算符在索引超出数组大 小时并不会直接报错,这是为了性能的考虑。 访问了越界的索引,可能 会覆盖掉别的变量导致程序行为异常,或是访 问到操作系统未映射的区域导致奔溃。 • int &operator[](size_t i) noexcept; • int const &operator[](size_t i) const noexcept; vector 容器: at • 为了防止不小心越界,可以用 a.at(i) 替代 a[i] , at 函数会检测索引 0 码力 | 90 页 | 4.93 MB | 1 年前3
共 45 条
- 1
- 2
- 3
- 4
- 5













