
C/C++
C/C++
吃着火锅x唱着歌
这个作者很懒,什么都没留下…
展开
-
C/C++ new A与new A()的区别
【代码】C/C++ new A与new A()的区别。原创 2023-07-18 21:45:43 · 729 阅读 · 1 评论 -
内存对齐原因和规则
结构体中的元素在内存中并不是紧密排列的,如以下结构体:struct s { char c1; int i1; char c2; int i2;};在内存中的大小是16字节而非简单的元素大小之和(10字节),原因是原创 2021-11-12 00:31:04 · 1948 阅读 · 0 评论 -
C/C++ __thread
__thread关键字是gcc内置的线程局部存储设施,它的存取效率可与全局变量相当,被该关键字修饰的对象每个线程都有一份独立实体,它只能修饰POD类型,不能修饰class类型。它只能修饰全局变量和函数内的静态变量,不能修饰函数内的普通变量和类的普通成员变量。它修饰的值只能被初始化为编译期常量。...原创 2020-08-16 16:38:50 · 753 阅读 · 0 评论 -
C++ std::bind用于成员函数
使用bind绑定到成员函数时,即使成员函数不需参数,也要将this绑定在第一个参数:#include <iostream>#include <memory>#include <functional>using namespace std;class test {public: test() : function(std::bind(&test::func, this, 1)) { } // 需将this绑定在func的第一个参数处 func原创 2020-07-26 00:10:13 · 5064 阅读 · 1 评论 -
C++ shared_ptr和std::move
以shared_ptr为参数调用std::move后,会将共享指针的引用计数减1,#include <iostream>#include <memory>using namespace std;int main() { shared_ptr<int> iptr(new int); shared_ptr<int> iptr_new = std::move(iptr); if (!iptr) { cout << "after s原创 2020-07-25 23:31:13 · 6335 阅读 · 2 评论 -
C/C++ 换行符、回车符与退格符
\r是回车符,作用为将光标移动到本行的开头。\n是换行符,作用为换行并将光标移到下一行开头。\b是退格符,作用为删除最后的字符。#include <stdio.h>int main() { printf("abc"); printf("def"); putchar('\r'); printf("gh"); putchar('\n'); printf("ijk\b"); printf("lmn"); }执行它:...原创 2020-07-10 21:18:14 · 14713 阅读 · 0 评论