随笔分类 - C/C++
摘要:Q: I don't understand why my exploit is not working. I need your help. download : http://pwnable.kr/bin/wtf download : http://pwnable.kr/bin/wtf.py Ru
阅读全文
摘要:在编译中要加 -lpthread或-pthread参数(不同版本的gcc可能不一样,man gcc可以查阅对应参数)。 例如:在加了头文件#include <pthread.h>之后执行 pthread.c文件,需要使用如下命令: gcc -lpthread -o thread thread.c 或
阅读全文
摘要:// <![CDATA[ #include <stdio.h> #include <stdlib.h> typedef union data { unsigned long vi; double vd; } data; void main() { data a; a.vd = 22.0; print
阅读全文
摘要:void die(const char *msg) { perror(msg); exit(errno); }
阅读全文
摘要:Re-execute itself from elf file.
阅读全文
摘要:原理 : 將資料逐一插入以排序好的序列中 Algo : for i <- 2 to n do 將a[i] 插入 a[1] ~ a[i-1] 之間適當的位置,使a[1]~a[i]排好 #include <stdio.h> #define SIZE 8 void sort(int array[], in
阅读全文
摘要:int x = 0;if (x = 0 || x == 0) printf("%dn", x);printf("%dn", x); 參考C的優先表, 其實就是if (x = (0 || x == 0))會printf出兩個1.同一优先级的运算符,运算次序由结合方向所决定。简单记就是:! > 算术运...
阅读全文
摘要:Question : Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are s...
阅读全文
摘要:Question :Search a 2D Matrix IIWrite an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integer...
阅读全文
摘要:Question : Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follow up: Can you solve it without using...
阅读全文
摘要:Question :Given a binary tree, return thepreordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[...
阅读全文
摘要:/*** CVE-2014-4014 Linux Kernel Local Privilege Escalation PoC** Vitaly Nikolenko* http://hashcrack.org** Usage: ./poc [file_path]* * where file_path ...
阅读全文
摘要:在Linux Kernel中有些constant需要被C code 跟 assembler共同使用在用constant的時候,不能單方面給0x1000UL因為assembler無法看這東西。但是C compiler卻可以。所以使用這個gcc feature.讓C compiler看到的constan...
阅读全文
摘要:from apue7.6. Memory Layout of a C ProgramA typical memory representation of C program consists of following sections.1. Text segment2. Initialized da...
阅读全文
摘要:一個自訂struct型態的變數,若想要轉換為unsigned,直接使用cast,gcc(version 4.4.3)編譯會回報錯誤。例如:struct _test { unsigned hour : 5; unsigned minute : 6;};struct _test var = {5, 6}printf("var = %x\n", (unsigned)var);error: aggregate value used where an integer was expected解決方法是,改為printf("var = %x\n", *(unsign
阅读全文
摘要:这两天找工作,做面试题的时候,碰到这样的题:指定了一个地址,比如说0x0012ff7c这样一个地址,要求是给这个指定的地址赋一个确定的值,比如说100,当时就是一个郁闷啊,在汇编里这样用过,在c中还真没用过。今天在网上查了查,自己也在turbo c上运行了一下,正好做个总结,方法如下:char * test;test = (char *)0x0012ff7c;*test = value;//自己赋值就好了这里需要注意的是第二行中的(char *)是一定需要的,而且要和上面的指针定义的类型相对应,比如:int * test;test = (int *)0x0012ff7c;*test = val
阅读全文
摘要:Reverse a singly linked listhttp://angelonotes.blogspot.tw/2011/08/reverse-singly-linked-list.htmlSource123456789101112131415161718192021222324252627282930313233343536373839#include typedef struct Node { char data; struct Node* next;} Node;void print_list(Node* root) { while (root) { printf("%c
阅读全文
摘要:#include const char shell[]="\x0f\x01\xf8\xe8\5\0\0\0\x0f\x01\xf8\x48\xcf";int main(){ }$ gcc -o disassembly disassembly.c$objdump -D disassembly | less$ /shell08048410 : 8048410: 0f 01 f8 swapgs 8048413: e8 05 00 00 00 call 804841d 8048418: 0f 01 f8 ...
阅读全文
摘要:一般来说,一个C的工程中一定要做一些这方面的工作,因为你会涉及到跨平台,不同的平台会有不同的字长,所以利用预编译和typedef可以让你最有效的维护你的代码。为了用户的方便,C99标准的C语言硬件为我们定义了这些类型,我们放心使用就可以了。按照posix标准,一般整形对应的*_t类型为:1字节 uint8_t2字节 uint16_t4字节 uint32_t8字节 uint64_t附:C99标准中inttypes.h的内容00001 /*00002inttypes.h0000300004Contributors:00005Createdby Marek Michalkiewicz 000...
阅读全文
摘要:通常在PC上寫程式時,很少會去管struct會佔掉多少記憶體。當要使用到時,也不會想去用手算到底佔掉多少,大多是直接使用sizeof來做計算。然而sizeof計算出來的值往往不會如我們想的一樣。因為compiler為了效能考量,會自動地為我們做最佳化,也就是資料對齊。為了這個目的,compiler會為struct多準備一些記憶體。我們可以看以下的code:struct ABC {int index;char name[6];int score;};struct DEF{int att;char name[3];};int main(void){printf("sizeof(ABC)
阅读全文