1. 建立32位可执行程序编译环境
$ sudo apt-get update
$ sudo apt-get install gcc-multilib
2. 示例源码1: bugging 程序( bugging.c )
这是一个简单的求和程序,计算 1+2+3+...+100 的值。程序存在bug,导致预期结果并不为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:链表测试程序( linked_list.h、linked_list.c、test_linked_list.c )
本例的程序来源于 github, 程序定义了数据结构链表,并使用了三个测试函数进行测试
/* 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");
}