随笔分类 - C/C++
摘要:lvalue required as unary ‘&’ operand #include <iostream> struct A { int data_; }; void CreateA(A** a_addr) { *a_addr = new A; } void DestoryA(A* a) {
阅读全文
摘要:C++struct支持数据类型为简单类型的隐式转换,包括stl中一些容器。 #include <iostream> #include <string> struct Widget { int x; std::string str; }; int main() { Widget w1; w1.x =
阅读全文
摘要:最近写代码,内存分配不管二级还是一级全放在一起申请,导致各种地址偏移,转换。这里记录几个常见的数据的大小 #include <iostream> #include <stdint.h> int main() { std::cout << "void** :" << sizeof(void**) <<
阅读全文
摘要:error: deleting ‘void*’ is undefined [-Werror=delete-incomplete] 今天做了一件这样的事 params_size = 2 * sizeof(void *) + sizeof(int *) + 2 * sizeof(int) + sizeo
阅读全文
摘要:安装工具 pip install cpplint # 检查是否符合规范 pip install clang-format # 一键调整 调整 cpplint *.cpp # 检查代码 clang-format -i *.cpp # 一键调整 调整目录下所有的文件 find . -name \( -n
阅读全文
摘要:头文件 #include <mcheck.h> 头文件内容 /* Copyright (C) 1996-2016 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is f
阅读全文
摘要:stod double stod (const string& str, size_t* idx = 0); double stod (const wstring& str, size_t* idx = 0); Convert string to double Parses str interpre
阅读全文
摘要:引子 思考以下几个小问题: 1.在保存一系列对象的时候,是保存对象,还是保存对象的指针呢? 2.假设在一个函数,能不能直接使用外部自己实现的函数? 3.假设要保存的数据对象是不同的,如何放在一起? 4.可不可以通过配置一些文档之类的来写一个程序? 还有许多类似的问题。当然生活中经常听到的反射机制,回
阅读全文
摘要:static关键字常用功能 1.局部静态变量 这个很简单的说就是某个作用内部的变量加上static,比如函数内部的一个static变量。 特点:局部作用域,全局生命周期。不可重入。 用处:记录下某些函数调用的次数之类的。 这样的用法最好是不要使用,因为不可重入性,容易在多线程程序中出错。 2.外部静
阅读全文
摘要:原文转载处 从语法上来说,构造函数和析构函数都可以抛出异常。但从逻辑上和风险控制上,构造函数和析构函数中尽量不要抛出异常,万不得已,一定要注意防止资源泄露。在析构函数中抛出异常还要注意栈展开带来的程序崩溃。 1.构造函数中抛出异常 在C++构造函数中,既需要分配内存,又需要抛出异常时要特别注意防止内
阅读全文
摘要:header #include <cstdarg> // or #include <stdarg.h> 这个头文件定义了一些宏来处理那些参数数量和数据类型不明确的函数调用的情况。 通过在其常规命名参数后包含逗号和三个点(,…),函数可以接受不同数量的附加参数,而无需相应的参数声明: 就像下面的形式
阅读全文
摘要:这是一个老生常谈的问题。每次看的时候都记得到,后面又忘了。记清楚这个三个词,只需要记住三句话。 1. 当一个函数foo,在同一个作用域内存在两个或两个以上的函数签名,就是“重载” 关键词:同一个作用域、 函数签名不同 2. 当派生类中存在一个有基类带virtual关键字的函数,而且函数参数返回值都相
阅读全文
摘要:1.理解const*与*const 假设有一个ptr指针,它保存变量vbl的地址。 Type* ptr = &vbl; 当使用指针的时候就涉及到两个对象:指针本身以及本身所指的对象。这就意味着const有三个层级的保护。 1.1. 确保ptr指向唯一的内存 有两种写法 Type* const ptr
阅读全文
摘要:## 1 memcmp C 库函数 int memcmp(const void *str1, const void *str2, size_t n)) 把存储区 str1 和存储区 str2 的前 n 个字节进行比较。 #include <string.h> int memcmp(const voi
阅读全文
摘要:#include <iostream> #include <fstream> #include <string> #include <vector> #include <set> using namespace std; void dealOneLine(set<string>& iSet, str
阅读全文
摘要:题外话 今天遇到这样一个注释 // BGRA little endian (argb in memory) to ARGB. 在opencv中使用cvtColor, 比如BGR2BGRA, 实际在是用libyuv的时候,(ubuntu)数据存放是argb, 所以数据还是要用libyuv::ARGBT
阅读全文
摘要:#include <dirent.h> #include <stdio.h> int main(int argc, char* argv[]) { DIR *dp; struct dirent *dirp; if ( argc != 2) { //err_quit("usage: ls direct
阅读全文
摘要:今天写代码是这个错误“munmap_chunk(): invalid pointer” 这个错误其实是使用new 申请空间后,再次对申请的空间进行分配,最后释放的时候的多次释放。 比如这样一段代码 size_t len = ????; uint8_t *data[2]; uint8_t* temp
阅读全文
摘要:#include <stdlib.h> #include <stdio.h> #include <time.h> static unsigned long GetTickCount(){ struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts);
阅读全文
摘要:#include <iostream> #include <string> #include <ifaddrs.h> #include <arpa/inet.h> #include <netdb.h> std::string GetIp() { void *tmpAddrPtr = NULL; st
阅读全文

浙公网安备 33010602011771号