Chinaunix首页 | 论坛 | 博客
  • 博客访问: 419973
  • 博文数量: 67
  • 博客积分: 2468
  • 博客等级: 上尉
  • 技术积分: 1050
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-05 01:21
文章分类

全部博文(67)

文章存档

2013年(1)

2012年(65)

2011年(1)

分类: C/C++

2012-02-10 23:22:23


点击(此处)折叠或打开

  1. /*《The C Programming Language》SECTION 5.6 中的一个例子*/
  2.     #include<stdio.h>
  3.     #include<stdlib.h>
  4.     #include<string.h>
  5.     #define MAXLINES 5000 /* max #lines to be sorted */
  6.     char *lineptr[MAXLINES]; /* pointers to text lines */
  7.     int readlines(char *lineptr[], int nlines);
  8.     void writelines(char *lineptr[], int nlines);
  9.     void myqsort(char *lineptr[], int left, int right);
  10.     /* srot input lines */
  11.     int
  12.     main(int argc, int *argv[] )
  13.     {
  14.         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");
  15.         printf("EOF is ???? : %d\nYour input are:\n" , EOF);
  16.         int nlines; /* number of input lines read */
  17.         if ((nlines = readlines(lineptr, MAXLINES)) >= 0){
  18.             myqsort(lineptr, 0, nlines-1);
  19.             printf("\nSo the result of sorting are:\n\n");
  20.             writelines(lineptr, nlines);
  21.             return 0;
  22.         } else {
  23.             printf("error: input too big to sort \n");
  24.             return -1;
  25.         }
  26.     }
  27.     #define MAXLEN 1000
  28.     int getline(char *, int);
  29.     char *alloc(int);
  30.     /* readlines: read input lines */
  31.     int
  32.     readlines(char *lineptr[], int maxlines)
  33.     {
  34.         int len , nlines;
  35.         char *p , line[MAXLEN];
  36.         nlines = 0;
  37.         while ((len = getline(line, MAXLEN)) > 0)
  38.             if (nlines >= maxlines || (p = malloc(len)) == NULL)
  39.                 return -1;
  40.             else {
  41.                 line[len-1] = '\0'; /* delete newline */
  42.                 strcpy(p, line);
  43.                 lineptr[nlines++] = p;
  44.             }
  45.         return nlines;
  46.     }
  47.     /* getline: read a line into s, return length */
  48.     int
  49.     getline(char s[], int lim)
  50.     {
  51.         int c, i;
  52.         for (i=0; i<lim-1 && (c=getchar()) != EOF && c != '\n'; ++i)
  53.             s[i] = c;
  54.         if (c == '\n'){
  55.             s[i] = c;
  56.             ++i;
  57.         }
  58.         s[i] = '\n';
  59.         return i;
  60.     }
  61.     /* writelines: write output lines */
  62.     void
  63.     writelines(char *lineptr[], int nlines)
  64.     {
  65.         while (nlines-- > 0)
  66.             printf("%s\n" , *lineptr++);
  67.     }
  68.     /* myqsort: sort v[left],,,v[right] into increasing order */
  69.     void
  70.     myqsort(char *v[], int left, int right)
  71.     {
  72.         int i ,last;
  73.         void swap(char *v[], int i, int j);
  74.         if (left >= right) /* do nothing if array contains */
  75.             return; /* fewer than two elements */
  76.         swap(v, left, (left + right)/2);
  77.         last = left;
  78.         for(i = left + 1; i <= right; i++)
  79.             if (strcmp(v[i], v[left]) < 0)
  80.                 swap(v, ++last, i);
  81.         swap(v , left , last);
  82.         myqsort(v, left, last-1);
  83.         myqsort(v, last+1, right);
  84.     }
  85.     /* swap: interchange v[i] and v[j] */
  86.     void
  87.     swap(char *v[], int i, int j)
  88.     {
  89.         char *temp;
  90.         temp = v[i];
  91.         v[i] = v[j];
  92.         v[j] = temp;
  93.     }

#######################################################################################

/*宋宝华《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


阅读(717) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~