chap1 實(shí)驗(yàn)環(huán)境和示例源碼

1. 建立32位可執(zhí)行程序編譯環(huán)境

$ sudo apt-get update

$ sudo apt-get install gcc-multilib

2. 示例源碼1: bugging 程序( bugging.c )

這是一個(gè)簡(jiǎn)單的求和程序,計(jì)算 1+2+3+...+100 的值。程序存在bug,導(dǎo)致預(yù)期結(jié)果并不為5050

/* bugging.c */

#include <stdio.h>

int foo(int n)
{

    int sum;
    int i;

    for (i=0; i<=n; i++)
    {
        sum = sum+i;
    }

    return sum;
}

int main(int argc, char** argv)
{
    int result = 0;
    int N = 100;

    result = foo(N);

    printf("1+2+3+...+%d= %d\n", N, result);

    return 0;

}

3. 示例源碼2:鏈表測(cè)試程序( linked_list.h、linked_list.c、test_linked_list.c )

本例的程序來(lái)源于 github, 程序定義了數(shù)據(jù)結(jié)構(gòu)鏈表,并使用了三個(gè)測(cè)試函數(shù)進(jìn)行測(cè)試

/* linked_list.h */

typedef struct node_t {
    char name[10];
    int id;
    char msg[30];
    struct node_t *next;
} node_t, *list_t;

/*
 * Modify h to point at NULL (empty list)
 */
void list_init(list_t *h);

int list_size(const list_t *h);

int list_empty(const list_t *h);

/*
 * prepend the node into the front
 */
void list_insert(list_t *h, node_t *n);

/*
 * delete first element in the lists, return the pointer to it
 */
node_t * list_delete(list_t *h, int id);


/*
 *
 */
void print_list(const list_t *h);

/* linked_list.c */

#include <stdio.h>
#include <string.h>
#include "linked_list.h"

void list_init(list_t *h) {
    *h = NULL;
}

int list_size(const list_t *h) {
    node_t *p = *h;
    int r = 0;
    do {
        r += 1;
        p = p->next;
    } while (p);
    return r;
}

int list_empty(const list_t *h) {
    return (*h == NULL);
}

void list_insert(list_t *h, node_t *n) {
    n->next = *h;
    *h = n;
}

node_t *list_find(const list_t *h, int id) {
    node_t *p = *h;
    while (p) {
        if (p->id == id) return p;
        p = p->next;
    }
}

node_t *list_find_before(const list_t *h, int id) {
    node_t *p = *h;
    while (p && p->next) {
        if (p->next->id == id) return p;
        p = p->next;
    }
    return NULL;
}

node_t *list_delete(list_t *h, int id) {
    node_t *r = NULL;
    if (*h && (*h)->id == id) {
        r = *h;
        *h = NULL;
        return r;
    }

    node_t *p = list_find_before(h, id);
    if (p) {
        r = p->next;
        p->next = p->next->next;
        r->next = NULL;
    }
    return r;
}

void print_list(const list_t *h) {
    node_t *p = *h;
    while (p) {
        printf("%d: %s says %s\n", p->id, p->name, p->msg);
        p = p->next;
    }
}


/* test_linked_list.c  */

#include <stdlib.h>
#include "linked_list.h"
#include <string.h>
#include <assert.h>
#include <stdio.h>


void test_delete_one() {
    list_t h;
    list_init(&h);
    node_t n;
    n.id = 0;
    strcpy(n.name, "hello");
    strcpy(n.msg, "world");
    list_insert(&h, &n);
    node_t *f = list_delete(&h, 0);
    assert(f == &n);
}


void test_delete() {
    list_t h;
    list_init(&h);
    node_t n[3];
    int i;
    for (i = 0; i < 3; i++) {
        n[i].id = i;
        list_insert(&h, &n[i]);
    }
    list_delete(&h, 1);
    assert(list_size(&h) == 2);
}

void core_dump_test() {
    int size = 0;
    list_t h;
    list_init(&h);
    size = list_size(&h);
    printf("list size is: %d\n", size);
}

int main () {
    test_delete();
    test_delete_one();
    core_dump_test();
    printf("Pass\n");
}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Ubuntu的發(fā)音 Ubuntu,源于非洲祖魯人和科薩人的語(yǔ)言,發(fā)作 oo-boon-too 的音。了解發(fā)音是有意...
    螢火蟲(chóng)de夢(mèng)閱讀 100,613評(píng)論 9 468
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,940評(píng)論 25 709
  • 1、交叉編譯 由于嵌入式系統(tǒng)資源匱乏,一般不能像 PC 一樣安裝本地編譯器和調(diào)試器,不能在本地編寫(xiě)、編譯和調(diào)...
    不配野心閱讀 2,236評(píng)論 0 4
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,540評(píng)論 19 139
  • 愿天下的有情人終成眷屬,愿有情人不再受相思之苦。 有多久,沒(méi)走進(jìn)自己的內(nèi)心看看,恐怕暗生了不少的蛛網(wǎng),寂落了一壁的...
    溫度文字閱讀 397評(píng)論 16 6

友情鏈接更多精彩內(nèi)容