- /*《The C Programming Language》SECTION 5.6 中的一个例子*/
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #define MAXLINES 5000 /* max #lines to be sorted */
- char *lineptr[MAXLINES]; /* pointers to text lines */
- int readlines(char *lineptr[], int nlines);
- void writelines(char *lineptr[], int nlines);
- void myqsort(char *lineptr[], int left, int right);
- /* srot input lines */
- int
- main(int argc, int *argv[] )
- {
- printf("Welcome U to have a try!Have a good time!Please press Ctrl+d to finish your input ,starting a new line in Linux environment. And press Ctrl+z in Windows.\n");
- printf("EOF is ???? : %d\nYour input are:\n" , EOF);
- int nlines; /* number of input lines read */
- if ((nlines = readlines(lineptr, MAXLINES)) >= 0){
- myqsort(lineptr, 0, nlines-1);
- printf("\nSo the result of sorting are:\n\n");
- writelines(lineptr, nlines);
- return 0;
- } else {
- printf("error: input too big to sort \n");
- return -1;
- }
- }
- #define MAXLEN 1000
- int getline(char *, int);
- char *alloc(int);
- /* readlines: read input lines */
- int
- readlines(char *lineptr[], int maxlines)
- {
- int len , nlines;
- char *p , line[MAXLEN];
- nlines = 0;
- while ((len = getline(line, MAXLEN)) > 0)
- if (nlines >= maxlines || (p = malloc(len)) == NULL)
- return -1;
- else {
- line[len-1] = '\0'; /* delete newline */
- strcpy(p, line);
- lineptr[nlines++] = p;
- }
- return nlines;
- }
- /* getline: read a line into s, return length */
- int
- getline(char s[], int lim)
- {
- int c, i;
- for (i=0; i<lim-1 && (c=getchar()) != EOF && c != '\n'; ++i)
- s[i] = c;
- if (c == '\n'){
- s[i] = c;
- ++i;
- }
- s[i] = '\n';
- return i;
- }
- /* writelines: write output lines */
- void
- writelines(char *lineptr[], int nlines)
- {
- while (nlines-- > 0)
- printf("%s\n" , *lineptr++);
- }
- /* myqsort: sort v[left],,,v[right] into increasing order */
- void
- myqsort(char *v[], int left, int right)
- {
- int i ,last;
- void swap(char *v[], int i, int j);
- if (left >= right) /* do nothing if array contains */
- return; /* fewer than two elements */
- swap(v, left, (left + right)/2);
- last = left;
- for(i = left + 1; i <= right; i++)
- if (strcmp(v[i], v[left]) < 0)
- swap(v, ++last, i);
- swap(v , left , last);
- myqsort(v, left, last-1);
- myqsort(v, last+1, right);
- }
- /* swap: interchange v[i] and v[j] */
- void
- swap(char *v[], int i, int j)
- {
- char *temp;
- temp = v[i];
- v[i] = v[j];
- v[j] = temp;
- }
#######################################################################################
/*宋宝华《Linux设备驱动程序开发详解》22.2中的关于GDB的使用小结*/
$编译pointer_array_try.c
#gcc -g pointer_array_try.c -o pointer_array_try.
$运行gdb
#gdb pointer_array_try.
$list命令
(gdb)list -/list/l/list /list .
$run命令
set args 设定运行参数;
path 设定程序的运行路径;
cd 改变工作目录;
info terminal显示程序用到的终端信息.
$break命令
break ;
break ;
break +offset/-offset;
break filename:function;
break *address;
break 没有参数时表示下一行指令处停住;
break if eg.break if i=100.
$单步命令
step 进入函数;
next 不进入函数;
set step-mode on/off
finish 直到当前函数完成返回,并打印出返回时的堆栈地址和返回值等信息。是不是非常酷。
until/u 运行程序直到退出循环体。
setpi/si nexti/ni 单步运行一条机器指令。
display/i $pc
$continue命令
continue [ignore-count]
c [ignore-count]
fg [ignore-count] ignore-count表示忽略断点的个数
$print命令
print
print /
p *array@len
$watch命令
watch expr有变化时停止运行程序
rwatch expr被读时停止
awatch expr被读或被写时停止
info watchpoints
$examine命令
examine/x / number, format, units~~
$jump命令
jump
$signal命令
signal <1~15> UNIX系统信号量通常为1~15
$return命令
return
return
$call命令
call
$info命令
info regitsters 除开浮点寄存器
info all-regitsters
info regitsters
$disassemble命令
disassemble