1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/* main.c */
#include <stdio.h>
char
G_TestValue;
int
G_TestValue2;
void
PrintTestValue(
void
);
int
main(
int
argc,
char
*argv[])
{
PrintTestValue();
printf
(
"main:\t\t(&G_TestValue)=0x%08x\n\t\t"
"(G_TestValue)=%d\n\t\tsizeof((G_TestValue))=%d\n"
"\t\tG_TestValue2=%d\n"
"\t\t&G_TestValue2=0x%08x\n"
,&(G_TestValue),G_TestValue,
sizeof
(G_TestValue),G_TestValue2,&G_TestValue2);
return
0;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/* global_test.c*/
#include <stdio.h>
typedef
struct
test_struct_t{
char
a;
int
b;
} TestStruct_t;
TestStruct_t G_TestValue = {2,8};
int
G_TestValue2;
void
PrintTestValue(
void
)
{
printf
(
"PrintTestValue:\t(&G_TestValue)=0x%08x\n\t\t"
"(G_TestValue.a)=%d\n\t\tsizeof(G_TestValue)=%d\n"
"\t\tG_TestValue2=%d\n"
"\t\t&G_TestValue2=0x%08x\n"
,&(G_TestValue), G_TestValue.a,
sizeof
(G_TestValue),G_TestValue2,&G_TestValue2);
printf
(
"-----------------------------------------\n"
);
}
|
1
2
3
4
5
6
7
8
9
10
|
test: global_test.o main.o
gcc -o test global_test.o main.o -std=gnu99
global_test.o: global_test.c
gcc -c global_test.c -std=gnu99
main.o: main.c
gcc -c main.c -std=gnu99
clean:
rm *.o test
|
-
不允许出现多个相同强符号。
-
如果有一个强符号和多个弱符号,则选择强符号。
-
如果有多个弱符号,那么先决议到size最大的那个,如果同样大小,则按照链接顺序选择第一个。
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
|
/* main.c */
#include <stdio.h>
#include <stdlib.h>
typedef
struct
malloc_test_tag
{
char
*str;
int
n;
}MallocTest_t ,*P_MallocTest_t;
int
main(
int
argc,
char
*argv[])
{
P_MallocTest_t test,saved_test;
printf
(
"malloc for test\n"
);
test = (P_MallocTest_t)
malloc
(
sizeof
(MallocTest_t));
if
(NULL == test )
{
exit
(1);
}
printf
(
"the address of (test) = 0x%x\n"
,test);
saved_test = test;
printf
(
"malloc for test->str\n"
);
test->str = (
char
*)
malloc
(6);
if
(NULL == test->str)
{
exit
(1);
}
printf
(
"the address of (test->str[0]) = 0x%x\n"
,test->str);
printf
(
"-----------------------------------------\n"
);
printf
(
"free for test->str\n"
);
free
(test->str);
printf
(
"free for test\n"
);
free
(test);
test = NULL;
printf
(
"malloc for test again!!!\n"
);
test = (P_MallocTest_t)
malloc
(
sizeof
(MallocTest_t));
if
(NULL == test )
{
exit
(1);
}
printf
(
"the address of (test) = 0x%x\n"
,test);
printf
(
"malloc for test->str again!!!!\n"
);
test->str = (
char
*)
malloc
(6);
if
(NULL == test->str)
{
exit
(1);
}
printf
(
"the address of (test->str[0]) = 0x%x\n"
,test->str);
printf
(
"free for test->str 2\n"
);
free
(saved_test->str);
saved_test->str = NULL;
printf
(
"free for test 2\n"
);
free
(test);
test = NULL;
return
0;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/* main.c */
#include <stdio.h>
#include <stdlib.h>
int
n = 10;
int
test_array[n] = {1,2,3,4,5,};
int
main(
int
argc,
char
*argv[])
{
printf
(
"the value test_array[0] is %d\n"
,test_array[0]);
return
0;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
/* main.c */
#include <stdio.h>
#include <stdlib.h>
void
test(
int
n)
{
char
test_array[n];
printf
(
"the size of test_array is %d\n"
,
sizeof
(test_array));
}
int
main(
int
argc,
char
*argv[])
{
int
n = 10;
int
test_array[n];
test_array[0] = 10;
printf
(
"the value test_array[0] is %d\n"
,test_array[0]);
test(4);
test(5);
return
0;
}
|
1
2
3
4
5
|
printf
(
"PrintTestValue:\t(&G_TestValue)=0x%08x\n\t\t"
"(G_TestValue.a)=%d\n\t\tsizeof(G_TestValue)=%d\n"
"\t\tG_TestValue2=%d\n"
"\t\t&G_TestValue2=0x%08x\n"
,&(G_TestValue), G_TestValue.a,
sizeof
(G_TestValue),G_TestValue2,&G_TestValue2);
|
1
2
3
4
5
6
7
|
#define SAFE_DELETE(p) \
do
\
{ \
delete
p; \
p = NULL; \
} \
while
(0)
|