
C++/C
文章平均质量分 55
guilanl
这个作者很懒,什么都没留下…
展开
-
C++11 的新特性: thread
这篇博客说的比较全面:http://blog.youkuaiyun.com/tujiaw/article/details/8245130我这里只贴一下我用到的部分内容:windows系统中,需要vs2012才支持。1.线程的创建C++11线程类std::thread,头文件include 首先,看一个最简单的例子:[cpp] vi转载 2015-02-06 16:35:51 · 554 阅读 · 0 评论 -
LeetCode 刷题 -- Reverse String
Write a function that takes a string as input and returns the string reversed.Example:Given s = "hello", return "olleh".我的解答:class Solution {public: string reverseString(string s)原创 2016-05-04 11:39:00 · 288 阅读 · 0 评论 -
LeetCode刷题:Linked List Cycle 及其进阶Linked List Cycle II
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?1. 首先解决的问题是 判断单链表 有没有环?解题思路:两个指针slow 和 fast,开始都指向 head, slow指针每次走一步,fast 指原创 2016-05-05 16:54:04 · 570 阅读 · 0 评论 -
Linux线程同步读写锁 rwlock
读写锁比mutex有更高的适用性,可以多个线程同时占用读模式的读写锁,但是只能一个线程占用写模式的读写锁。1. 当读写锁是写加锁状态时,在这个锁被解锁之前,所有试图对这个锁加锁的线程都会被阻塞;2. 当读写锁在读加锁状态时,所有试图以读模式对它进行加锁的线程都可以得到访问权,但是以写模式对它进行枷锁的线程将阻塞;API接口说明:1) 初始化和销毁#inclu转载 2016-08-26 15:44:08 · 450 阅读 · 0 评论 -
leetcode 刷题之: reverse string
Example:Given s = "hello", return "olleh".Subscribe to see which companies asked this questionclass Solution {public: string reverseString(string s) { int len = s.lengt原创 2016-10-06 00:17:30 · 281 阅读 · 0 评论 -
用python 读取和写入CSV格式的文件
This Python 3 tutorial covers how to read CSV data in from a file and then use it in Python. For this, we use the csv module. CSV literally stands for comma separated variable, where the comma is what原创 2016-11-03 12:03:08 · 9028 阅读 · 0 评论 -
分享一个 变量没有初始化 可能带来的问题
先看 code:static void do_ctors_aux(void){ /* SGX RTS does not support .ctors currently */ fp_t *p = NULL; uintptr_t init_array_addr; size_t init_array_size; const void *en原创 2017-02-22 11:54:05 · 1613 阅读 · 0 评论 -
Integer Promotion
Almost every programmer has learned about C, and a lot of them use it for their career. Yet, C can be really tricky and behave unexpectedly sometimes. One of those dodgy side of C is integer promot原创 2017-03-29 10:57:24 · 899 阅读 · 0 评论 -
UAF (use after free) 漏洞
Use After Free 如上代码所示,指针p1申请内存,打印其地址,值然后释放p1指针p2申请同样大小的内存,打印p2的地址,p1指针指向的值Gcc编译,运行结果如下: p1与p2地址相同,p1指针释放后,p2申请相同的大小的内存,操作系统会将之前给p1的地址分配给p2,修改p2的值,p1也被修改了。由此我们可以知道:1.在f原创 2017-03-22 11:25:53 · 4557 阅读 · 0 评论 -
2sum/3sum/ksum 问题
这个是面试中碰到的一个问题,还没有时间仔细实现,在网上一篇文章,转载一下,代码稍后补上。网址:http://www.sigmainfy.com/blog/summary-of-ksum-problems.htmlSummary for LeetCode 2Sum, 3Sum, 4Sum, K SumOverviewI summarize转载 2016-04-25 16:53:50 · 2057 阅读 · 0 评论 -
LeetCode刷题: power of two (判断一个数是不是2的幂次方)
Given an integer, write a function to determine if it is a power of two.下面是我在网上看到的一种方面,利用了2幂次方的特点如果是power of two, 则2进制表达中,有且仅有一个1. 可以通过移位来数1的个数, 这里用了一个巧妙的办法, 即判断 N & (N-1) 是否为0. class原创 2016-01-14 14:17:15 · 825 阅读 · 0 评论 -
LeetCode 刷题: 合并两个有序链表 (merge two sorted list)
题目:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.合并两个有序链表,新链表的元素就是之前的两个链表里面的元素。程序:/** * De原创 2016-01-13 14:05:57 · 393 阅读 · 0 评论 -
C++ -- 如何取得一个给定的地址所存放的值
终于搞清楚了这个问题。比如是地址0x2222, 想得到0x2222所对应的地址的值,C里面是这样的:int data = *(uint64_t *) 0x2222; 如果是32位地址,则应该是这样写:int data = *(uint32_t *)0x2222;原创 2015-03-16 14:07:51 · 6388 阅读 · 1 评论 -
最好不要在头文件里面包含定义变量
写了一个程序,在头文件里面定义了一个常量数组。在另外的两个CPP 文件里面都include 了这个头文件,最好编译的时候链接出错,说multiple definition.原因是两个CPP 里面都包含了这个头文件,那么这个常量数组相当于定义了两遍,所以出错了。教训: 最好不要在头文件里面定义变量!!原创 2015-06-04 11:19:49 · 741 阅读 · 0 评论 -
Visual Studio 中使用try except 遇到的问题
在Visual Studio 中使用try except , compile的时候遇到如下error:__try in functions containing objects with destructors原因是在使用try/except 的时候,不可以在函数中有object unwinding and destruction.解决办法, 参考微软: Compiler Error C2712原创 2015-07-09 16:45:15 · 693 阅读 · 0 评论 -
C++的global data的位置 及PE 文件中的section的内容
1. 情况一:int a = 5;a 在.data section 里面2. 情况二:const int a = 5;a 在 .rdata section里面扩展:一般C语言的编译后执行语句都编译成机器代码,保存在.text段。已初始化的全局变量和局部静态变量都保存在. data段未初始化的全局变量一般放在一个叫.“bss”的段里转载 2015-07-14 09:58:56 · 1265 阅读 · 0 评论 -
Windows API函数 WaitForMultiObjects 的使用注意事项
今天在使用CreateThread 和WaitForMultiObjects 来创建和回收线程的时候,发生了错误。当线程数是小于64的时候,没有问题,当线程数大于64的时候,出现问题。原因是 WaitForMultiObjects(...) 最多只能等待MAXIMUM_WAIT_OBJECTS个kernal objects。MAXIMUM_WAIT_OBJEC原创 2015-07-30 13:06:48 · 1916 阅读 · 0 评论 -
标准错误和标准输出的重定向
1: 标准输出2: 标准错误一般情况下:./run > 1.txt以上命令只能讲标准输出重定向到1.txt.如果想把标准错误也重定向到1.txt, 命令如下:./run > 1.txt 2>&12>&1表示: 标准错误也重定向到标准输出。原创 2015-12-10 14:56:10 · 988 阅读 · 0 评论 -
Linux- 线程函数如何将返回值传给主线程
网上找到几种方法,一一记录。1. 定义一个 包含 线程函数的 参数和返回值的 数据结构。例子如下:#include #include typedef struct thread_data { int a; int b; int result;} thread_data;void *myThread(void *arg){ thread_d原创 2015-12-02 16:56:07 · 4915 阅读 · 0 评论 -
Linux- 如何算出函数的运行时间
背景知识:1. time() 函数 - get time in secondsSynopsis#include time.h>time_t time(time_t *t);Descriptiontime() returns the time as the number of seconds since the Epoch, 1970-01-01 00:00:00原创 2015-12-03 10:53:26 · 464 阅读 · 0 评论 -
memcpy_s 的安全提示
errno_t memcpy_s( void *restrict dest, rsize_t destsz, const void *restrict src, rsize_t count );(2)(since C11) 1) Copies count characters from the原创 2018-04-25 16:31:39 · 5140 阅读 · 0 评论