-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample_correct.c
More file actions
126 lines (112 loc) · 3.46 KB
/
example_correct.c
File metadata and controls
126 lines (112 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*=============================================================================
# Filename: example.c
# Author: bookug
# Mail: bookug@qq.com
# Last Modified: 2018-10-09 08:13
# Description: union two sorted lists, and apply the sqrt operator on each item
# For debug, gcc -lm -g inc/list.c example.c -o run.exe
# To release, gcc -O2 -lm inc/list.c example.c -o run.exe
use of objdump
https://blog.csdn.net/zoomdy/article/details/50563680
https://www.cnblogs.com/justin-y-lin/p/5599217.html
=============================================================================*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
//Another choice is to use 'inc/list.h' here
#include "list.h"
//A safe way to release memory
#define xfree(p) free(p);p=NULL;
#define list1_num 1000
#define list2_num 1000
//this is placed in .data segment, and it can be assigned larger space than function stack(1M by default). For larger space, use heap memory instead
int list1[list1_num];
int list2[list2_num];
/**
* @author bookug
* @email bookug@qq.com
* @function prepare the data for two lists
* @param no parameters needed
* @return no value returned here
*/
void produceData()
{
for(int i = 0; i < list1_num; ++i)
{
list1[i] = 2 * i + 1;
}
for(int i = 0; i < list2_num; ++i)
{
list2[i] = 2 * (i+1);
}
}
/**
* @author bookug
* @email bookug@qq.com
* @function the main function of C program
* @param number of arguments in the command, including the program itself
* @param all argument strings
* @return no value returned here
*/
int main(int argc, const char* argv[])
{
produceData();
Node *result_head = NULL, *result_tail = NULL;
int pos1 = 0, pos2 = 0;
//NOTICE: this strategy not always fails, for example, the final elements of the two lists are the same
while(pos1 < list1_num && pos2 < list2_num)
//while(pos1 < list1_num && pos2 < list2_num)
{
/*printf("check %d %d\n", pos1, pos2);*/
if(list1[pos1] < list2[pos2])
{
addResult(&result_head, &result_tail, list1[pos1]);
pos1++;
}
else if(list1[pos1] > list2[pos2])
{
addResult(&result_head, &result_tail, list2[pos2]);
pos2++;
}
else //equal case, only add once
{
addResult(&result_head, &result_tail, list1[pos1]);
pos1++;
pos2++;
}
}
while(pos1 < list1_num)
{
addResult(&result_head, &result_tail, list1[pos1]);
pos1++;
}
while(pos2 < list2_num)
{
addResult(&result_head, &result_tail, list2[pos2]);
pos2++;
}
//NOTICE: the declaration Node* p=... in for-loop is only allowed in C99 standard
//output each item in result after sqrt operation
for(Node* p = result_head; p != NULL; p = p->next)
{
//NOTICE: sqrt(8) works but sqrt(n) must be compiled by -lm
double r = sqrt((double)(p->val));
printf("%lf\n", r);
}
//NOTICE: another bug exists here, but error is not reported when running
//printf("%p %p\n", result_head, result_tail);
//release the result list
for(Node* p = result_head; p != NULL; )
{
Node* tmp = p->next;
free(p);
p = tmp;
//NOTICE: this set will cause segment fault immediately
//p = NULL;
//printf("%p\n", p);
}
//clear the list pointers, head and tail
result_head = result_tail = NULL;
return 0;
}