
c/c++
adream307
这个作者很懒,什么都没留下…
展开
-
[c++] c++17 在编译期判断是否存在某个函数
#include <bits/stdc++.h>namespace detail {template <class Default, class AlwaysVoid, template <class...> class Op, class... Args>struct detector { using value_t = std::false_type; using type = Default;};template &原创 2021-11-01 13:09:41 · 1089 阅读 · 0 评论 -
2021-10-25
示例程序#include <stdio.h>int main(int ac, char **av) { int localfn(int a) { return a+ac; } int (*fptr)(int) = localfn; printf("%d\n", fptr(-1)); return 0;}编译 1gcc trampoline.c -z execstack -原创 2021-10-25 11:14:33 · 330 阅读 · 0 评论 -
使用 sanitize工具检查内存为题
// g++ str_view_test.cpp -fsanitize=address -std=c++17 -o s#include <bits/stdc++.h>std::string f1() { std::string val; val += "123"; val += "456"; return val;}void f2(std::string_view val) { std::cout << val << std:原创 2021-10-25 09:41:06 · 277 阅读 · 0 评论 -
[linux] 手动设置进程的 rlimit 信息
实例代码如下,设置进程只能打开 8 个文件描述符号#include <unistd.h>#include <sys/resource.h>#include <stdlib.h>#include <stdio.h>int main(){ struct rlimit old; if(getrlimit(RLIMIT_NOFILE,&old)<0) return EXIT_FAILURE; printf("rlimi原创 2021-04-12 17:15:45 · 411 阅读 · 0 评论 -
[linux] 阻塞进程的所有信号
#include <unistd.h>#include <signal.h>#include <stdio.h>int main(){ sigset_t mask; sigfillset(&mask); int ret = sigprocmask(SIG_BLOCK,&mask,NULL); if(ret<0) return -1; for(int i=0;i<3;i++){ printf("block all signa原创 2021-04-01 11:51:20 · 432 阅读 · 0 评论 -
[linxu]SUBREAPER 设置祖父进程接管孙子进程
测试代码#include<unistd.h>#include<stdlib.h>#include<stdio.h>#include<sys/prctl.h>int main(){ pid_t pid = fork(); if(pid<0) return -2; if(pid==0){ //child printf("child, pid = %d, ppid = %d\n",getpid(),getpp原创 2021-03-31 19:43:32 · 598 阅读 · 0 评论 -
[linux] setsid测试
测试程序如下#include<unistd.h>#include<stdio.h>#include<stdlib.h>int main(){ printf("start...\n"); int err = setenv("ENV_TEST","VALUE_TEST",0); if(err<0) return err; int pid = fork(); if(pid<0) return -1; if(pid==0){ // if(原创 2021-03-31 16:45:12 · 209 阅读 · 0 评论 -
c++ 构造函数抛异常
如果构造函数抛异常,则析构函数不会被调用,已经初始化了成员变量则被系统依次释放,此处可能引起内存泄露#include <iostream>class T2{public: T2(){std::cout << "T2()" << std::endl;} ~T2(){std::cout << "~T2()" << std::endl;}};class T1{public: T1(){ a = ne原创 2021-03-02 15:44:50 · 437 阅读 · 0 评论 -
[c++]数组全排列
#include <vector>#include <functional>#include <iostream>using namespace std;constexpr int empty_val = -1;void Permutation_List(vector<int> &array, int pos, function<void(const vector<int> &x)> &foo)原创 2020-12-20 21:53:28 · 557 阅读 · 0 评论 -
[cpp] 使用 shared_ptr的自定义删除函数模拟 go 语言的 defer 功能
go 语言的 defer 定义函数退出时的行为,c++ 的 shared_ptr 可以自定义deleter 函数明确变量在析构时的行为。因此可以使用 share_ptr 的自定义 deleter 函数模拟 go 语言中的 defer 行为#include <memory>#include <iostream>int main(){ int x = 5; std::shared_ptr<int> tx(nullptr,[&](int *){原创 2020-10-10 13:59:19 · 447 阅读 · 0 评论 -
[cpp]无符号数据测试
#include<stdio.h>int main(){ unsigned int x1 = -1; unsigned int x2 = 0; unsigned int x3 = x2-x1; printf("x1 = %u\n",x1); printf("x2 = %u\n",x2); printf("x3 = %u\n",x3); printf("x2-x1 = %u\n",x2-x1); if(x2>x1) printf("x2 > x1\n"); els原创 2020-08-10 19:13:22 · 196 阅读 · 0 评论 -
[cuda]异步内存拷贝中的默认同步
MemcpyIn the reference documentation, each memcpy function is categorized as synchronous or asynchronous, corresponding to the definitions below.SynchronousAll transfers involving Unified Memory regions are fully synchronous with respect to the host.F原创 2020-08-03 16:23:55 · 1238 阅读 · 0 评论 -
[c++]蛋疼的面试题目
#include<stdio.h>int main(){ int a[]={10,11,12,13,14,15}; int b=3; printf("%d, %d\n",a[b],b[a]); return 0;}输出结果13, 13原创 2020-05-09 18:35:54 · 269 阅读 · 0 评论 -
[cpp]double最大值、最小值及正无穷
#include <iostream>#include <limits>int main(){ double inf = std::numeric_limits<double>::infinity(); double max = std::numeric_limits<double>::max(); double min...原创 2020-03-01 19:51:40 · 4820 阅读 · 0 评论 -
[cuda]cudaMemcpy会同步所有的默认stream
测试代码如下#include <cuda_runtime.h>#include <cassert>#include <iostream>typedef unsigned long long int ullint;__global__ void sum_test(ullint *ptr){ for(int i=0;i<100000; +...原创 2019-12-11 16:21:33 · 949 阅读 · 0 评论 -
[cuda]unified memory测试
unified memory 在被 kernel 函数访问的时候,如果同事被 host 函数访问,那么host 得到的结果可能是错误的,测试程序如下#include <cuda_runtime.h>#include <cassert>__global__ void umem_test(int* cnt){ atomicAdd(cnt,1);}int m...原创 2019-12-07 11:49:38 · 362 阅读 · 0 评论 -
[cuda]磁盘的文件直接进显存
使用mmap将文件内容映射到内存的一段地址使用cudaMemcpy将映射地址复制到显存#include <fcntl.h>#include <sys/mman.h>#include <sys/stat.h>#include <sys/types.h>#include <unistd.h>#include <cuda...原创 2019-12-05 17:12:12 · 972 阅读 · 0 评论 -
[cuda]cuBLAS矩阵乘法示例
#include <cuda_runtime.h>#include <cublas_v2.h>#include <chrono>#include <iostream>#define IDX2C(i,j,ld) (((j)*(ld))+( i ))#define ROW 13#define COL 17int main(){ ...原创 2019-12-03 15:00:32 · 721 阅读 · 1 评论 -
智能指针(smart pointer)
源于优快云上的一道题目,大意是这样的: class A(){public: A(){ m_ptr = new int[10]; } ~A(){ delete [] m_ptr; }};题目上说,上面这段代码是不安全的,如果发送异常,A的析构函数有可能不被执行,发送内存泄露,题目并给出了安全的代码:原创 2009-11-19 18:24:00 · 750 阅读 · 0 评论 -
用自己的比较函数构造STL的set
http://www.cppreference.com/wiki/stl/set/start set The C++ Set is an associative STL container that contains a sorted set of unique objects of type Key. Sorting is done using the key comparison原创 2009-12-01 13:36:00 · 919 阅读 · 0 评论 -
字符串分割
#include#include//本函数把一个长字符串分割成几个小字符串,//这些小字符串由分割符 char c 隔离//返回子字符串的数量int SubString(std::vector &Str,std::string src,char c){ int begin,end; Str.clear(); end=0; do{ begin = src原创 2009-11-30 12:32:00 · 735 阅读 · 0 评论 -
关于构造函数异常
//关于类构造函数的异常//构造函数内变量的初始化顺序只与变量在类内部的声明顺序有关,与构造函数上的初始化列表顺序无关//2009-12-15#includeusing namespace std;class base{public: base()try{ throw 1; cout }catch(int i){ cout }原创 2009-12-15 23:24:00 · 753 阅读 · 0 评论 -
modify temporaries of builtin type -- error
//这个是从《Exceptional C++》的 Item1:Iterator上学来的//无法对内建类型的临时变量做修改//内建类型指 int char 等,//注意:指针也是内建的数据类型//builtin type//2009-12-15#includeusing namespace std;class A{ int n;public: A(int i):n(i){}原创 2009-12-15 23:32:00 · 730 阅读 · 0 评论 -
C语言中的参数可变函数
这是从IBM的DeveloperWork上看到的,原网址如下:http://www.ibm.com/developerworks/cn/linux/l-va/index.html下面是从cppreference上复制下来的:http://www.cppreference.com/wiki/c/other/va_arg?s[]=va&s[]=startSyntax: #inc转载 2009-12-23 00:30:00 · 753 阅读 · 0 评论 -
递归的方法求平均数
这是QQ上看到的问题,最初是这么想的:double Average(double x[],int n){ if(n != 0){ return ((Average(x,n-1))*n+x[n])/(n+1); } return x[0];}后来要求使用分治的思想解决问题,最直接的想法是这样的:double Average(double x[],int sta原创 2009-12-21 23:16:00 · 4912 阅读 · 0 评论 -
虚函数的默认参数
这是从《Exceptional C++》Item 21里学来的#includeusing namespace std;class Base{public: virtual void f(int i=10){cout };class Derived: public Base{public: virtual void f(int i=20){cout };int mai原创 2009-12-27 18:49:00 · 807 阅读 · 0 评论 -
virtual function
这个是从《Exceptional C++》上看到的,84页,有这么一句话:Avoid public virtual funtions;prefer using the Template Method pattern instead这是自己写的几个例子例1:下面这段代码是没办法编译通过的#include#includeusing namespace std;class Bas原创 2009-12-27 18:26:00 · 759 阅读 · 0 评论 -
C语言调用Matlab
开发环境 : Windows XP SP2IDE : CodeblocksMatlab : 7.0程序实现一个简单的FFT运算,最后把结果返回C程序把 D:/MATLAB7/extern/include 添加到Search directories把 D:/MATLAB7/extern/lib/win32/microsoft/msvc71 添加到 Link se原创 2010-05-01 15:34:00 · 6600 阅读 · 1 评论 -
用一个循环,先输出奇数在输出偶数
<br />先帖出处:<br />http://topic.youkuaiyun.com/u/20081114/18/ad521288-2901-4868-8430-77a0e01ae746.html?94480<br /> <br />只允许使用一个循环<br />先输出1至10内的奇数<br />然后输出其中的偶数<br />结果应该是这样的<br />1<br />3<br />5<br />7<br />9<br />2<br />4<br />6<br />8<br />见到一个贼*(不知道怎么形容了)的解法转载 2010-10-21 18:04:00 · 2324 阅读 · 0 评论 -
全局变量与局部变量的地址
全局变量以及全局的函数名在编译时就已经分配地址了,而且这个地址是固定的线性地址,不管程序运行多少次,运行多少个实例,它的地址始终是确定的,而且是唯一的线性地址(线性地址通过分页机制转化成物理地址) //hello.c #include const char gstr[]="Hello world"; int main() { const char str[]="Hello world"; printf("str=%X ",str);原创 2010-12-18 13:26:00 · 3767 阅读 · 0 评论 -
[转]Plain Binary (Flat-Form Binary)
出处:http://wzw19191.blog.163.com/blog/static/1311354702009926105347968/ So what does plain binary mean? Quoted from Nasm Documentation 6.1 bin: Flat-Form Binary Output The bin format does not produce object files: it generates nothing in the原创 2010-12-22 20:52:00 · 1255 阅读 · 0 评论 -
readelf--读elf文件信息
$ readelf -h foobar ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version:原创 2010-12-24 18:58:00 · 831 阅读 · 0 评论 -
代码格式化命令—indent: indent –h.c –o h2.c -gnu -br
//h.c #include int main(void){printf("Hello World!/n");if(1>2){printf("1>2/n");}while(1 int main (void) { printf ("Hello World!/n"); if (1 > 2) { printf ("1>2/n"); } while (1原创 2011-01-02 14:40:00 · 694 阅读 · 0 评论 -
奇怪的switch语句
在这个个上面看到的: http://www.matrix67.com/blog/archives/429 #include int main() { int a=10; switch(a) { case '1': printf("ONE/n"); break;原创 2011-01-01 13:58:00 · 617 阅读 · 0 评论 -
struct中的地址对齐
下文在gcc编译器中测试通过,32位的RedHat9.0 struct结构体中每个数均按照地址对齐的方式排列 #include typedef struct { unsigned short a; unsigned long b; }S1; typedef struct { unsigned short a; unsigned long b; unsigned char c[2]; }S原创 2011-01-02 23:08:00 · 903 阅读 · 0 评论 -
linux查看网卡信息
lspci | grep Ethernet 查看硬件信息: dmesg原创 2011-01-08 16:28:00 · 1021 阅读 · 0 评论 -
gcc 预编译控制
//h.c #include int main() { #ifdef DEBUG printf("In debug.../n"); #else printf("In release.../n"); #endif return 0; } gcc h.c -o h ./h In release... gcc –DDEBUG h.c -o h ./h In debug...原创 2011-01-18 22:00:00 · 771 阅读 · 0 评论 -
[转]CentOS5.5编译arm-elf-gcc
参考文献: [1]http://www.unix-center.net/bbs/viewthread.php?tid=2234&extra=page%3D1### [2]http://blog.ednchina.com/walnutcy/162547/message.aspx [3]http://ubuntuforums.org/showthread.php?t=708309 [4]http://cygwin.com/ml/crossgcc/2007-02/msg00017.html原创 2011-01-18 23:43:00 · 1288 阅读 · 0 评论 -
[转]用温柔的方法对付Troll
出处:http://developers.solidot.org/developers/11/03/07/0125251.shtml 在互联网论坛上我们经常能看到有人发表挑逗性的评论,这些人被称为Troll,如在PC游戏论坛大赞主机的好处,在PS论坛鼓吹Xbox,在Android论坛拜苹果,或者Vi vs Emacs,等等。这些留言经常会引发一连串的回应,其他人会反击,留言者可能会被Ban。常识认为对付Troll的最佳方法是不要理睬他们,但这种策略实际上是无效而错误的。受《Feeling Good》这原创 2011-03-07 12:13:00 · 613 阅读 · 0 评论 -
Altium使用技巧--持续更新
1、寻找未与任何网络相连的过孔 (Not(InNet(‘*’))) and IsVia原创 2011-03-08 14:55:00 · 800 阅读 · 0 评论