C/C++
yangjingxu
这个作者很懒,什么都没留下…
展开
-
Code demostrating "Distinguishing C++ Declarations from Expressions"
Code demostrating "Distinguishing C++ Declarations from Expressions" in The Definitive ANTLR Reference. #include <stdio.h>typedef int I;char x = 'a';void foo() { I(x); x = 1...2011-01-01 21:19:44 · 116 阅读 · 0 评论 -
C++ inline
http://www.parashift.com/c++-faq-lite/inline-member-fns.html原创 2012-07-10 13:41:10 · 117 阅读 · 0 评论 -
C switch statement without braces
A example is static_assert. #include <stdio.h>#define static_assert(x) switch (x) case 0: case (x):int main(int argc, const char *argv[]) { static_assert(1 == 1); static_ass...2012-03-13 11:21:42 · 127 阅读 · 0 评论 -
Array Initialization in C
#include <stdio.h>int array[10] = { [7] = 700,};int main(int argc, const char *argv[]) { printf("%d\n", array[7]); return 0;}2012-03-14 09:55:33 · 112 阅读 · 0 评论 -
& operator optional in function pointer assignment
Code:#include <stdio.h>int info(int no) { printf("no: %d\n", no);}int main(int argc, const char *argv[]) { int (*fp)(int); fp = info; printf("function pointer: %p\...原创 2012-03-29 18:34:58 · 116 阅读 · 0 评论 -
Compute the difference between a value and 2's power
#include <stdio.h>int main(int argc, const char *argv[]) { int base = 0xf0000000; // difference between base and 0x100000000 int diff = -base; printf("%08x, %08x\n", base, dif...2012-03-31 10:14:08 · 95 阅读 · 0 评论 -
Access elements in array of struct
#include <stdio.h>struct packet_buf { char buf[16];} __attribute__((aligned(16)));struct packet_buf buf_ring[4];int main(int argc, const char *argv[]) { int i; printf("~...2012-04-05 10:32:14 · 105 阅读 · 0 评论 -
Optional comma in C initialization
#include <stdio.h>struct person { int sex; int no;};int main(int argc, const char *argv[]) { int array_1[3] = {1, 2, 3}; int array_2[3] = {1, 2, 3,}; struct person xiao_y...2012-04-06 14:09:51 · 103 阅读 · 0 评论 -
#if and #ifdef in C
#include <stdio.h>#define debug 0int main(int argc, const char *argv[]) {#if debug printf("#if\n");#endif#ifdef debug printf("#ifdef\n");#endif}2012-04-06 22:19:12 · 280 阅读 · 0 评论 -
An Application of Bubble Sort
This problem is inspired by the discussion about ordering specification, BSort in the paper "Aurora: a new model and architecture for data stream management" Problem: There is an array A who...2013-04-11 21:03:51 · 96 阅读 · 0 评论 -
Relationship between union and its members
Code:#include <stdio.h>struct Nsreq_accept { int req_s;};struct Nsreq_shutdown { int req_s; int req_how;};union Nsipc { struct Nsreq_accept accept; struct Nsreq_sh...2012-04-08 09:42:42 · 137 阅读 · 0 评论 -
Struct array member
C code:#include <stdio.h>// This struct does not store jp_data: // +-+-+-+-+ // 0+-+-+-+-+ jp_len // 3 2 1 0struct zero_size_array_struct { int jp_len; ch...2012-04-08 10:59:49 · 128 阅读 · 0 评论 -
Array and its first element
#include <stdio.h>struct rx_desc { int len; int status;};struct rx_desc rx_ring[2];int main(int argc, const char *argv[]) { struct rx_desc* rx; // Intialization rx_r...2012-04-09 17:46:01 · 109 阅读 · 0 评论 -
Example of snprintf
#include <stdio.h>int main(int argc, const char *argv[]) { int count; int i; char buf[10]; for (i = 0; i < 10; i++) buf[i] = 1; count = snprintf(buf, 10, "%s", "abc");...2012-04-09 18:18:33 · 106 阅读 · 0 评论 -
C array initialization
For fewer initializers, refer to 4.9 Initialization in K&R and 6.7.8 Initialization in C99 standard. #include <stdio.h>// in bss section. static storage. initialized to 0int ...2012-04-14 10:18:35 · 151 阅读 · 0 评论 -
Assembly code for stack array initialization
I develop the code with x86 gcc. The C code: #include <stdio.h>void test() { int i; int array[3] = {0}; for (i = 0; i < 3; i++) printf("array[%d] = %d\n", i, arr...2012-04-14 10:28:02 · 125 阅读 · 0 评论 -
The New C: X Macros (memo)
The code:#include <stdio.h>#define COLOR_TABLE \X(red, "red") \X(green, "green") \X(blue, "blue")#define X(a, b) a,enum COLOR { COLOR_TABLE};#undef X#defi...原创 2012-04-20 10:42:23 · 135 阅读 · 0 评论 -
Hacking Assembly Code Generated by G++
For the following C++ code: class person { public: person() {} person(const person& rhs) {}};void func(person p) {}int main(int argc, const char *argv[]) { person y...原创 2013-06-09 15:31:59 · 290 阅读 · 0 评论 -
Check the final definition of a typedef
Refer to http://stackoverflow.com/questions/471248/what-is-ultimately-a-time-t-typedef-to. Read Quassnoi's answer.2012-07-04 10:43:59 · 95 阅读 · 0 评论 -
Solution to an Interview Question
https://gist.github.com/19217012012-02-27 13:41:39 · 86 阅读 · 0 评论 -
PC Assembly Tutorial
A great tutorial for learning assembly programming and internals of C and C++.http://www.drpaulcarter.com/pcasm/原创 2011-03-03 16:49:01 · 109 阅读 · 0 评论 -
Sample Code of the Usage of /dev/zero
#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <errno.h>int main(int argc, const char *...原创 2011-03-09 22:29:56 · 89 阅读 · 0 评论 -
Stack and heap allocation in C
#include <stdio.h>#include <stdlib.h>/* * str is a literal. So it is allocated in readonly segment. It is OK to return * it. But the data pointed by the pointer can't be modifi...2010-11-26 23:16:38 · 114 阅读 · 0 评论 -
timer in libevent
#include <event.h>#include <stdio.h>#include <time.h>static voidhello(int fd, short event, void *arg){ printf("hello man\n");}int main(int argc, const char ...2010-11-27 11:18:24 · 84 阅读 · 0 评论 -
C++ bitset
#include <bitset>#include <iostream>using namespace std;int main() { const bitset<12> mask(2730ul); cout << "mask = " << mask << endl;原创 2011-07-20 15:09:01 · 89 阅读 · 0 评论 -
a simple bit vector
#include <stdio.h>#define BYTE_LEN 8unsigned int direct_address_search(int T[], int k) { int byte_offset = k / BYTE_LEN; int bit_offset = k % BYTE_LEN; return (T[byte_offset]...2011-07-20 15:57:52 · 103 阅读 · 0 评论 -
x86 64 assembly tutorial (MEMO)
Refer to http://os-blog.com/x86-64-assembly-programming-hello-world/. Check /usr/include/unistd.h for system call numbers. Note that syscall numbers may be different for the same system call on 32...2012-04-24 15:50:01 · 150 阅读 · 0 评论 -
KMP Algorithm
#include <stdio.h>#define MAX_LEN 100int f[MAX_LEN];// Compute failure functionvoid compute_failure_function(char* b, int n) { int t, s; t = 0; f[0] = 0; for (s = 0; s ...2012-01-12 14:56:38 · 101 阅读 · 0 评论 -
How to embed binary data in an ELF file (memo)
http://www.techpulp.com/blog/2009/01/how-to-embed-binary-data-in-an-elf-file/原创 2012-01-19 18:31:19 · 95 阅读 · 0 评论 -
gcov tutorial
tmp.c#include <stdio.h>int main(int argc, const char *argv[]) { int i, j; for (i = 0; i < 10; i++) if (i > 100) j += 3; else j += 2; return 0;} S...2012-05-03 21:01:43 · 121 阅读 · 0 评论 -
Source code change effect on object code
C code: #include <stdio.h>// change no's type from int to long longlong long func(long long no) { return no + 1; }int main(int argc, const char *argv[]) { int i; i = 100...2012-05-05 11:15:01 · 116 阅读 · 0 评论 -
GCC options: -ffunction-sections and -fdata-sections
C code: struct person { int age; int no;};int plus_one(int no){ return no + 1;}int minus_one(int no){ return no - 1;} Run 'gcc -S -ffunction-sections -fdata-sect...2012-05-05 11:21:16 · 147 阅读 · 0 评论 -
C symbol table
C source code: #include <stdio.h>void sub(int x) {}void xiao() { static int x = 10; sub(x);}void yu(){ static int x = 20; sub(x);}int main(int argc, co...2012-05-06 10:38:55 · 459 阅读 · 0 评论 -
String Literals in C
Refer to 6.4.5 String literals in C99 standard. Sample code:#include <stdio.h>int main(int argc, const char *argv[]) { char val[] = "one" "two"; printf("part1\n" "part2\...2012-02-01 23:18:28 · 123 阅读 · 0 评论 -
Comments In GAS Assembly
Anything from `/*' through the next `*/' is a comment. For x86, # starts a line comment. / also starts a line comment. / can also start a line comment. But / must be the first non-whitespace cha...2012-02-06 16:50:02 · 94 阅读 · 0 评论 -
Sections in ELF
The following little code shows the effects of .rodata, .data and .bss sections. #include <stdio.h>// y_readonly is in data seciton. "123" is in rodata section.// y_readonly is a po...2012-02-06 16:57:29 · 90 阅读 · 0 评论 -
What's the use of do while(0) when we define a macro?
Sample code: #include <stdio.h>#define INIT do { \ printf("line1\n"); \} while (0)#define INIT_ { \ printf("line1\n"); \}void correct() { if (1) INIT; else ...2012-02-15 14:22:53 · 123 阅读 · 0 评论 -
Division and modulo in C/C++, Java and Python
[quote]a is an integer, n is a positive integer.C/C++=====quotient is rounded towards zero. a mod n = a - n (a/n)Python======Same as defined by CRLS.[/quote]原创 2013-12-19 12:58:08 · 223 阅读 · 0 评论