随笔分类 - C/C++
摘要:The#pragma packdirective modifies the current alignment rule for members of structures following the directive.The#pragma packdirective modifies the current alignment rule for only the members of structures whose declarations follow the directive.It does not affect the alignment of the structure dir
阅读全文
摘要:#pragmadirective :Specifies implementation-defined instructions to the compiler.C90 does not permit a#pragmadirective to be produced as the result of a macro expansion. However, the C99_Pragmaoperator enables you to embed a preprocessor macro in a pragma directive, and_Pragmais permitted in C90 if--
阅读全文
摘要:Cloak --https://github.com/pfultz2/CloakA mini-preprocessor library to demostrate the recursive capabilites of the preprocessor#define CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)#define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__#define INC(x) PRIMITIVE_CAT(...
阅读全文
摘要:#include <stdio.h>#include <string.h>#include <stdarg.h>#define NUMARGS(...) (sizeof((int[]){__VA_ARGS__})/sizeof(int))#define SUM(...) (sum(NUMARGS(__VA_ARGS__), __VA_ARGS__))void sum( int numargs, ... );int main( int argc, char *argv[ ] ){ SUM( 1 ); SUM( 1, 2 ); SUM( 1, 2, 3 ); S
阅读全文
摘要:http://c2.com/cgi/wiki?TailRecursionRecursive procedures call themselves to work towards a solution to a problem. In simple implementations this balloons the stack as the nesting gets deeper and deeper, reaches the solution, then returns through all of the stack frames. This waste is a common compla
阅读全文
摘要:http://stackoverflow.com/questions/6707148/foreach-macro-on-macros-arguments/13459454#13459454recursive macros are possible in C using a fancy workaround. The end goal is to create aMAPmacro which works like this:#define PRINT(a) printf(#a": %d", a)MAP(PRINT, a, b, c) /* Apply PRINT to a,
阅读全文
摘要:http://aggregate.org/MAGIC/The Aggregate Magic AlgorithmsThere are lots of people and places that create and collect algorithms of all types (hereare a few WWW sites). Unfortunately, in building systems hardware and software, we inThe Aggregateoften have found it necessary to do relatively obscure l
阅读全文
摘要:http://www.hackersdelight.org/hdcodetxt/nlz.c.txt// This has the programs for computing the number of leading zeros// in a word.// Max line length is 57, to fit in hacker.book.// Compile with g++, not gcc.#include <stdio.h>#include <stdlib.h> // To define "exit", req'd by X
阅读全文
摘要:http://graphics.stanford.edu/~seander/bithacks.htmlBit Twiddling HacksBy Sean Eron Andersonseander@cs.stanford.eduIndividually, thecode snippets here are in the public domain(unless otherwise noted) — feel free to use them however you please. The aggregate collection and descriptions are ©1997-
阅读全文
摘要:http://stackoverflow.com/questions/11632219/c-preprocessor-macro-specialisation-based-on-an-argumentTuns out it is possible. This anwser is based on Pauls macros, but much simpler and does not need definition for each user.#define CAT(a,...) PRIMITIVE_CAT(a, __VA_ARGS__)#define PRIMITIVE_CAT(a,.....
阅读全文
摘要:1. 宏可以像函数一样被定义,例如:#define min(x,y) (x 但是在实际使用时,只有当写上min(),必须加括号,min才会被作为宏展开,否则不做任何处理。2. 如果宏需要参数,你可以不传,编译器会给你警告(宏参数不够),但是这会导致错误。如C++书籍中所描述的,编译器(预处理器)对宏的语法检查不够,所以更多的检查性工作得你自己来做。3. 很多程序员不知道的#和###符号把一个符号直接转换为字符串,例如:#define STRING(x) #xconst char *str = STRING( test_string ); str的内容就是"test_string&qu
阅读全文
摘要:http://pic.dhe.ibm.com/infocenter/iseries/v7r1m0/index.jsp?topic=%2Frzarg%2Fvariable_length_arrays.htmAflexible array memberis permitted as the last element of a structure even though it has incomplete type, provided that the structure has more than one named member. A flexible array member is a C99
阅读全文
摘要:http://pic.dhe.ibm.com/infocenter/iseries/v7r1m0/index.jsp?topic=%2Frzarg%2Fvariable_length_arrays.htmVariable length arrays (C++ only)A variable length array, which is a C99 feature, is an array of automatic storage duration whose length is determined at run time.Variable length array declarator sy
阅读全文
摘要:In astructwith more than one member, the last member of thestructcan have incomplete array type. Such a member is called aflexible array memberof thestruct.NoteWhen astructhas a flexible array member, the entirestructitself has incomplete type.Flexible array members enable you to mimic dynamic type
阅读全文
摘要:Returns NARG, the number of arguments contained in __VA_ARGS__ beforeexpansion as far as NARG is >0 and <64 (cpp limits):#define PP_RSEQ_N() 63,62,61,60,[..],9,8,7,6,5,4,3,2,1,0#define PP_ARG_N(_1,_2,_3,_4,_5,_6,_7,_8,_9,[..],_61,_62,_63,N,...) N#define PP_NARG_(...) PP_ARG_N(__VA_ARGS__)#defi
阅读全文
摘要:http://www.codeguru.com/cpp/g-m/bitmap/specialeffects/article.php/c1739/Invert-mirror-a-bitmap-inplace.htmPosted byRoger OnslowonAugust 5th, 1998It is possible to invert a bitmap (laterally or vertically) IN-PLACE - ie. without creating any intermediate or temporary bitmaps. You can modify the origi
阅读全文
摘要:http://www.codeguru.com/cpp/g-m/bitmap/specialeffects/article.php/c1743/Rotate-a-bitmap-image.htmRotate a bitmap imagehttp://www.codeguru.com/cpp/g-m/bitmap/specialeffects/article.php/c1725/Invert-mirror-a-bitmap.htmInvert (mirror) a bitmapPosted byZafir AnjumonAugust 5th, 1998We can invert an image
阅读全文
摘要:http://www.codeguru.com/cpp/g-m/bitmap/specialeffects/article.php/c1725/Invert-mirror-a-bitmap.htmInvert (mirror) a bitmaphttp://www.efg2.com/Lab/ImageProcessing/RotateScanline.htmDiscussionIf one attempts to rotate a bitmap in the "forward" direction by selection Pixel (i, j) and rotating
阅读全文
摘要:S-Record Format A file in Motorola S-record format is an ASCII file. There are three different formats: S19 for 16-bit address S2 for 24-bit address S3 for 32-bit address The files consist of optional symbol table information, data specifications...
阅读全文
摘要:/*The CRC calculation unit mainly consists of a single 32-bit data register, which:is used as an input register to enter new data in the CRC calculator(when writing into the register) holds the result of the previous CRC calculation(when reading the register)Each write operation into the data regist
阅读全文