随笔分类 -  C

摘要:1 《泛型编程与STL》侯捷译or英文版 ---------这本书里从STL的设计哲学切入,讲述了STL为什么要那样设计,五种迭代器如何把容器与算法完美地解耦合。它详细介绍了每一个STL容器与算法,包括接口原型、参数要求、执行时间复杂度等等。
看这本书以前,我感觉自己是在STL的树林里跑,偶尔碰到一棵树就爬上去摘点果子尝尝;看完这本书以后,感觉自己是在俯瞰这片小树林,哪儿有什么树什么果子都一目了然,想要什么都可以顺手拈来了。这感觉是真够棒的。 以后编程的时候,总是下意识先想想STL里有没有现成的算法,如果有就直接用,简洁清晰还高效率。2 《STL源码剖析》-----------这个是侯捷自己写 阅读全文
posted @ 2013-01-22 00:01 Jack204 阅读(1965) 评论(0) 推荐(0)
摘要:此题要求深刻理解:1指针的概念,2指针的数组操作和静态的数组操作之间的区别。下面的代码是动态申请三维数组Arr[a][b][c],*data ------- 指向存储数据的一维数组,总共有a*b*c个元素。**fp ------- 第一层指针,总共有a*b个元素,每个元素为指向data某一段首的指针。每一段长为c,因为三维数组相当于a*b个c组成。***sp -------- 第二层指针,总共有a个元素,每个元素为指向fp某一段首的指针。每一段厂为b。然后再分别把指针赋值上就行了。使用的时候,p[i][j] = *((*(p+i))+j).方便很多。//以下代码co... 阅读全文
posted @ 2012-08-01 23:16 Jack204 阅读(462) 评论(0) 推荐(0)
摘要:1 #include "stdio.h" 2 3 struct { 4 unsigned char a:3; 5 unsigned char b:3; unsigned char c:4; 6 }i;7 8 int main() 9 { 10 i.a ... 阅读全文
posted @ 2012-07-22 12:27 Jack204 阅读(1620) 评论(0) 推荐(1)
摘要:Compile Objective-C Programs Using gcchttp://blog.lyxite.com/2008/01/compile-objective-c-programs-using-gcc.htmlProbably 99% of all Objective-C programmers out there are compiling their programs with XCode very happily. However that doesn't stop the other 1%, who are brave enough or simply don&# 阅读全文
posted @ 2012-03-21 16:15 Jack204 阅读(699) 评论(0) 推荐(0)
摘要:Linux Kernel : likely/unlikely macrosEver wondered what the likely and unlikely macros in the linux kernel are ?The macros are defined as :#define likely(x) __builtin_expect((x),1)#define unlikely(x) __builtin_expect((x),0)The __builtin_expect is a method that gcc (versions >= 2.96) offer ... 阅读全文
posted @ 2012-02-20 13:57 Jack204 阅读(353) 评论(0) 推荐(0)
摘要:宽字符wchar_t.那就要和char来比较:The char specifier is an integral type.A char has enough storage to represent a character from the basic character set. The amount of storage allocated for a char is implementation-dependent.You initialize a variable of type char with a character literal (consisting of one cha 阅读全文
posted @ 2011-12-11 11:48 Jack204 阅读(3669) 评论(0) 推荐(0)
摘要:1 windows下按照标准格式编写dll,然后gcc编译加-shared,生成dll文件。2 windows linux下均可以将.o文件,使用ar.exe,链接成lib[name].a文件,.a文件用来静态链接,生成.exe文件。 编译时候 gcc -o run.exe main.c libname.a.提示:dll的c源文件也可以用来当做.a文件的源,但是倒着回去不行。3 同一个dll文件,可以有两种不同的用法:Loading Library 和 Runtime Library.Loading Library --- 使用函数时候,像正常时候一样,不过头文件中的声明变为:__decls. 阅读全文
posted @ 2011-12-06 21:27 Jack204 阅读(421) 评论(0) 推荐(0)
摘要:1 先看C语言中的可以使用的__declspec(...)的参数。下面是拷贝的微软msdn文档中的解释:Extended attribute syntax simplifies and standardizes the Microsoft-specific extensions to the C language. The storage-class attributes that use extended attribute syntax include thread, naked, dllimport, and dllexport.The extended attribute syntax 阅读全文
posted @ 2011-12-05 13:10 Jack204 阅读(308) 评论(0) 推荐(0)
摘要:1 首先是Windows的inline asm形式:void function_name{ .... __asm{ MASM inline syntax; } ....}使用Windows的编译器,cl.exe来编译。默认使用的MASM汇编语法,使用ml.exe进行编译。具体语法查看MSDN。2 如果使用Dev-C++编译器进行编译,查看安装目录,里面全是gcc.exe, as.exe默认使用gnu的标准,即inline asm的形式为:void c_func();int main(int argc, char* argv[]){ printf("%d : %s\n",.. 阅读全文
posted @ 2011-12-05 11:46 Jack204 阅读(729) 评论(0) 推荐(0)
摘要:查看反汇编主要的思路在于将 流程,处理,算法 区分开来。1 函数调用:原C代码:int sum(int, int);int main(){ int c = sum(1, 2); printf("c=%d", c); return 0;}int sum(int a, int b){ int c = a + b; return c;}反汇编的函数调用主要使用bp-frame式(不懂自己去查去)的调用方式:mmm然后是被调用的sum:ss2 for循环:基本的for循环是这样的:int sum(int b){ int c, i=0; c = 0; fo... 阅读全文
posted @ 2011-12-02 13:44 Jack204 阅读(59865) 评论(0) 推荐(8)
摘要:Reason to use per-CPU variable1 Remember that the 2.6 kernel is preemptible; it would not do for a processor to be preempted inthe middle of a critical section that modifies a per-CPU variable.2 It also would not be good if your process were to be moved to another processor in the middle of a per-CP 阅读全文
posted @ 2011-11-21 11:24 Jack204 阅读(856) 评论(0) 推荐(0)
摘要:对于c的可重定位文件中的.symtab..symtab存放的是 全局变量/函数名字. --- 给连接器来使用symtab中每个条目都有自己的属性.所以static与非static的全局变量和函数名字都在.symtab中,在重定位之后才会被去除,不过static类型的定义,连接器不会把它往外部.链接但是虽然不往外部链接,用到函数名或者全局变量的地方,无论是否为static,都需要在链接时候进行重定位.!!!!!!!!!!!!!! 阅读全文
posted @ 2011-10-31 23:56 Jack204 阅读(291) 评论(0) 推荐(0)
摘要:主要的Makefile内容如下,非常的绕。 1 ifneq ($(KERNELRELEASE),) 2 # call from kernel build system 3 4 scull-objs := main.o pipe.o access.o 6 obj-m := scull.o 7 8 else 9 10 KERNELDIR ?= /lib/modules/$(shell uname -r)/build11 PWD := $(shell pwd)12 13 modules:14 $(MAKE) -C $(KERNELDIR) M=$(PWD) LDD... 阅读全文
posted @ 2011-10-31 19:42 Jack204 阅读(3455) 评论(0) 推荐(0)
摘要:poll的作用:同时探测n个drivers,找到可以直接使用的driver,从而尽量block进程。以下kernel源代码来自于:<linux/poll.h> 与 fs/select.cstatic unsigned int scull_p_poll(struct file *filp, poll_table *wait){ struct scull_pipe *dev = filp->private_data; unsigned int mask = 0; /* * The buffer is circular; it is considered full ... 阅读全文
posted @ 2011-10-30 15:54 Jack204 阅读(17944) 评论(1) 推荐(0)
摘要:Surpring point: 1 _IOC_TYPECHECK的写法。2 对32位cmd定义的macro类型。介绍之前先说下32位的cmd被分为4块,不罗嗦,直接看宏便知道了。#define _IOC_NRBITS 8#define _IOC_TYPEBITS 8#define _IOC_SIZEBITS 14#define _IOC_DIRBITS 2#define _IOC_NRMASK ((1 << _IOC_NRBITS)-1)#define _IOC_TYPEMASK ((1 << _IOC_TYPEBITS)-1)#define _IOC_SIZEMASK 阅读全文
posted @ 2011-10-27 19:42 Jack204 阅读(827) 评论(0) 推荐(0)
摘要:为了在acm中使用stl标准库,果断在写acm的时候,投靠了c++。这里总结一下cin cout scanf printf:scanf("%c", &temp); ---- 回车换行也会被当做一个字符来输入。cin >> temp ---- 回车换行不会被当做一个字符读入。printf(); cout 倒是没有那么大的区别。不过需要特殊格式输出的时候,我还是倾向于使用printf。在写程序的时候,除特殊需要,要么就将代码风格写的像c++,要么就像c。不要混。!!! 阅读全文
posted @ 2011-10-22 15:33 Jack204 阅读(221) 评论(0) 推荐(0)
摘要:1 typedef struct { volatile int counter; } atomic_t; ----- volatile意思是不要对其进行优化,这里如果不优化的话就会被汇编成直接访问内存地址,而不是操作寄存器。2 #define automic_set(v, i) (((v)->counter) = (i)) ----- 会被汇编成一条访问内存的指令。3 Atomic_add(int i, atomic_t *v)static __inline__ void atomic_add(int i, atomic_t *v){__asm__ __volatile__(LOCK & 阅读全文
posted @ 2011-10-19 22:53 Jack204 阅读(3389) 评论(0) 推荐(0)
摘要:#的使用方法(因为老是忘,所以记录于此):Quoting macro arguments(当字符串引用)#define QUOTEME(x) #xthe codeprintf("%s\n", QUOTEME(1+2));will expand toprintf("%s\n", "1+2");Token concatenation(Token当做字符串连接)#define MYCASE(item,id) \case id: \ item##_##id = id;\break switch(x) { MYCASE(widget,23);} 阅读全文
posted @ 2011-10-16 17:49 Jack204 阅读(444) 评论(0) 推荐(0)
摘要:Kernel. EXPORT_SYMBOL解析Code Segment:include/module.h:struct kernel_symbol { unsigned long value; const char *name;};/* For every exported symbol, place a struct in the __ksymtab section */#define __EXPORT_SYMBOL(sym, sec) \ __CRC_SYMBOL(sym, sec) \ static const char __kstrtab_##sym[] \ __attribute__ 阅读全文
posted @ 2011-10-12 15:26 Jack204 阅读(854) 评论(0) 推荐(0)
摘要:因为elf格式中不同的section,不同的segment有不同的作用。这里只是注重整体ELF的作用,具体详细的用到了再看。1 Elf files have a dual nature:Compilers, assemblers, and linkers treat the file as a set of logical sections described by a section header table.The system loader treats the file as a set of segments described by a program header table.2 阅读全文
posted @ 2011-10-09 23:45 Jack204 阅读(220) 评论(0) 推荐(0)