归档: /home/sjj/work_learning/c/Const
/*这里显示三个东西:
1.如何确定const 限定符 ,修饰的是什么?
1)const type 等价于type const,
这里的type可以是普通的char , int ,unsigned long 等等,也可以是结构体和由typedef定义的新类型;
例如下面的 char const gA = 'a' ; 这句语句也可以写成为 const char gA = 'a';
它们都表示gA 这个变量只读。
2)对于存在指针const 类型的时候,只要记住一个原则, const修饰他后面的类型,
看 char * const gBp1 = &gB ; 这里的const修饰的就是gBp1变量,所以说gBp1这个变量在程序执行过程中只读。
如果有这样的定义:
char * const * gVar = &gp ;
那么const 修饰的就是*gVar,也就是说 gVar变量所指向的内容只读,当然咯,它指向的类型是个指向字符变量的指针。
有兴趣考察下,这个定义吧:char const * const * gVar = &gp ;
2.被限定的对象,它的读写性质是怎么样的,如果是一个变量,那么它存储在那个section?
这个可以借助一个工具来看看我们这些变量到底存储在哪个sect了:
$readelf a.out -s -S
可以看到如下面注释中的分部。
很显然,限定为const的变量是存储在rodata区的,也就是只读区域,注意哦,它还是一个变量,不是一个常量。
而对于 const char * gAp2 = &gA ; gAp2 这样的变量,只是说他所指向的内容只读,但是他本身是可写的,所以是没有资格放在rodata区的。
3.在栈中的const变量,并没有升级到rodata区中去。(...)
*/
/*额外的,但是很重要的东西:
下面有两条打印,按照正常的理解 *gBp2是不会变化的,
但是程序的结果就是,*gBp2变了,而且变的很正确,呵呵。
这里要说明的就是, const char * const gBp2 表明*gBp2的值不能通过 (*gBp2) = 100 这样的形式去改变,
但是gB本身是可以改变的, 而*gBp2取得的值永远是gB变量存的值。
所以不要以为我把它const了,就安全了,其实,只是说不能通过这个变量去改变它罢了。
*/
俗话说,翠花儿~上code :
in Const.c:
#include <stdio.h>
char const gA = 'a' ; /*rodata*/
const char * const gAp1 = &gA ; /*rodata*/
const char * gAp2 = &gA ; /*data*/
char gB = '1' ; /*data*/
char * const gBp1 = &gB ;/*rodata*/
const char * const gBp2= &gB; /*rodata*/
int main (int argc , char * argv[])
{
char const lA = 'A' ;
const char * const lAp = &lA ;
printf("%c %c %c/n",gB,*gBp1,*gBp2); // 1 1 1
gB ++ ;
printf("%c %c %c/n",gB,*gBp1,*gBp2); // 2 2 2
//gBp1++; //erro
//gBp2++ ; //erro
(*gBp1)++ ;
//(*gBp2)++ ; //erro
return 0;
}
$readelf a.out -s -S:
There are 36 section headers, starting at offset 0x1774:
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .interp PROGBITS 08048134 000134 000013 00 A 0 0 1
[ 2] .note.ABI-tag NOTE 08048148 000148 000020 00 A 0 0 4
[ 3] .hash HASH 08048168 000168 000028 04 A 5 0 4
[ 4] .gnu.hash GNU_HASH 08048190 000190 000020 04 A 5 0 4
[ 5] .dynsym DYNSYM 080481b0 0001b0 000050 10 A 6 1 4
[ 6] .dynstr STRTAB 08048200 000200 00004c 00 A 0 0 1
[ 7] .gnu.version VERSYM 0804824c 00024c 00000a 02 A 5 0 2
[ 8] .gnu.version_r VERNEED 08048258 000258 000020 00 A 6 1 4
[ 9] .rel.dyn REL 08048278 000278 000008 08 A 5 0 4
[10] .rel.plt REL 08048280 000280 000018 08 A 5 12 4
[11] .init PROGBITS 08048298 000298 000030 00 AX 0 0 4
[12] .plt PROGBITS 080482c8 0002c8 000040 04 AX 0 0 4
[13] .text PROGBITS 08048310 000310 00020c 00 AX 0 0 16
[14] .fini PROGBITS 0804851c 00051c 00001c 00 AX 0 0 4
[15] .rodata PROGBITS 08048538 000538 000022 00 A 0 0 4
[16] .eh_frame PROGBITS 0804855c 00055c 000004 00 A 0 0 4
[17] .ctors PROGBITS 08049f0c 000f0c 000008 00 WA 0 0 4
[18] .dtors PROGBITS 08049f14 000f14 000008 00 WA 0 0 4
[19] .jcr PROGBITS 08049f1c 000f1c 000004 00 WA 0 0 4
[20] .dynamic DYNAMIC 08049f20 000f20 0000d0 08 WA 6 0 4
[21] .got PROGBITS 08049ff0 000ff0 000004 04 WA 0 0 4
[22] .got.plt PROGBITS 08049ff4 000ff4 000018 04 WA 0 0 4
[23] .data PROGBITS 0804a00c 00100c 000010 00 WA 0 0 4
[24] .bss NOBITS 0804a01c 00101c 000008 00 WA 0 0 4
[25] .comment PROGBITS 00000000 00101c 0000fc 00 0 0 1
[26] .debug_aranges PROGBITS 00000000 001118 000070 00 0 0 8
[27] .debug_pubnames PROGBITS 00000000 001188 000025 00 0 0 1
[28] .debug_info PROGBITS 00000000 0011ad 0001b5 00 0 0 1
[29] .debug_abbrev PROGBITS 00000000 001362 000083 00 0 0 1
[30] .debug_line PROGBITS 00000000 0013e5 000180 00 0 0 1
[31] .debug_str PROGBITS 00000000 001565 00008e 01 MS 0 0 1
[32] .debug_ranges PROGBITS 00000000 0015f8 000040 00 0 0 8
[33] .shstrtab STRTAB 00000000 001638 000139 00 0 0 1
[34] .symtab SYMTAB 00000000 001d14 000500 10 35 54 4
[35] .strtab STRTAB 00000000 002214 000229 00 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings)
I (info), L (link order), G (group), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)
Symbol table '.dynsym' contains 5 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
1: 00000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__
2: 00000000 0 FUNC GLOBAL DEFAULT UND __libc_start_main@GLIBC_2.0 (2)
3: 00000000 0 FUNC GLOBAL DEFAULT UND printf@GLIBC_2.0 (2)
4: 0804853c 4 OBJECT GLOBAL DEFAULT 15 _IO_stdin_used
Symbol table '.symtab' contains 80 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
1: 08048134 0 SECTION LOCAL DEFAULT 1
2: 08048148 0 SECTION LOCAL DEFAULT 2
3: 08048168 0 SECTION LOCAL DEFAULT 3
4: 08048190 0 SECTION LOCAL DEFAULT 4
5: 080481b0 0 SECTION LOCAL DEFAULT 5
6: 08048200 0 SECTION LOCAL DEFAULT 6
7: 0804824c 0 SECTION LOCAL DEFAULT 7
8: 08048258 0 SECTION LOCAL DEFAULT 8
9: 08048278 0 SECTION LOCAL DEFAULT 9
10: 08048280 0 SECTION LOCAL DEFAULT 10
11: 08048298 0 SECTION LOCAL DEFAULT 11
12: 080482c8 0 SECTION LOCAL DEFAULT 12
13: 08048310 0 SECTION LOCAL DEFAULT 13
14: 0804851c 0 SECTION LOCAL DEFAULT 14
15: 08048538 0 SECTION LOCAL DEFAULT 15
16: 0804855c 0 SECTION LOCAL DEFAULT 16
17: 08049f0c 0 SECTION LOCAL DEFAULT 17
18: 08049f14 0 SECTION LOCAL DEFAULT 18
19: 08049f1c 0 SECTION LOCAL DEFAULT 19
20: 08049f20 0 SECTION LOCAL DEFAULT 20
21: 08049ff0 0 SECTION LOCAL DEFAULT 21
22: 08049ff4 0 SECTION LOCAL DEFAULT 22
23: 0804a00c 0 SECTION LOCAL DEFAULT 23
24: 0804a01c 0 SECTION LOCAL DEFAULT 24
25: 00000000 0 SECTION LOCAL DEFAULT 25
26: 00000000 0 SECTION LOCAL DEFAULT 26
27: 00000000 0 SECTION LOCAL DEFAULT 27
28: 00000000 0 SECTION LOCAL DEFAULT 28
29: 00000000 0 SECTION LOCAL DEFAULT 29
30: 00000000 0 SECTION LOCAL DEFAULT 30
31: 00000000 0 SECTION LOCAL DEFAULT 31
32: 00000000 0 SECTION LOCAL DEFAULT 32
33: 00000000 0 FILE LOCAL DEFAULT ABS init.c
34: 00000000 0 FILE LOCAL DEFAULT ABS initfini.c
35: 00000000 0 FILE LOCAL DEFAULT ABS crtstuff.c
36: 08049f0c 0 OBJECT LOCAL DEFAULT 17 __CTOR_LIST__
37: 08049f14 0 OBJECT LOCAL DEFAULT 18 __DTOR_LIST__
38: 08049f1c 0 OBJECT LOCAL DEFAULT 19 __JCR_LIST__
39: 08048340 0 FUNC LOCAL DEFAULT 13 __do_global_dtors_aux
40: 0804a01c 1 OBJECT LOCAL DEFAULT 24 completed.6635
41: 0804a020 4 OBJECT LOCAL DEFAULT 24 dtor_idx.6637
42: 080483a0 0 FUNC LOCAL DEFAULT 13 frame_dummy
43: 00000000 0 FILE LOCAL DEFAULT ABS crtstuff.c
44: 08049f10 0 OBJECT LOCAL DEFAULT 17 __CTOR_END__
45: 0804855c 0 OBJECT LOCAL DEFAULT 16 __FRAME_END__
46: 08049f1c 0 OBJECT LOCAL DEFAULT 19 __JCR_END__
47: 080484f0 0 FUNC LOCAL DEFAULT 13 __do_global_ctors_aux
48: 00000000 0 FILE LOCAL DEFAULT ABS initfini.c
49: 00000000 0 FILE LOCAL DEFAULT ABS Const.c
50: 08049ff4 0 OBJECT LOCAL HIDDEN 22 _GLOBAL_OFFSET_TABLE_
51: 08049f0c 0 NOTYPE LOCAL HIDDEN 17 __init_array_end
52: 08049f0c 0 NOTYPE LOCAL HIDDEN 17 __init_array_start
53: 08049f20 0 OBJECT LOCAL HIDDEN 20 _DYNAMIC
54: 0804a00c 0 NOTYPE WEAK DEFAULT 23 data_start
55: 08048480 5 FUNC GLOBAL DEFAULT 13 __libc_csu_fini
56: 08048310 0 FUNC GLOBAL DEFAULT 13 _start
57: 08048548 4 OBJECT GLOBAL DEFAULT 15 gBp1
58: 00000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__
59: 00000000 0 NOTYPE WEAK DEFAULT UND _Jv_RegisterClasses
60: 08048538 4 OBJECT GLOBAL DEFAULT 15 _fp_hw
61: 0804851c 0 FUNC GLOBAL DEFAULT 14 _fini
62: 00000000 0 FUNC GLOBAL DEFAULT UND __libc_start_main@@GLIBC_
63: 0804853c 4 OBJECT GLOBAL DEFAULT 15 _IO_stdin_used
64: 08048540 1 OBJECT GLOBAL DEFAULT 15 gA
65: 0804a00c 0 NOTYPE GLOBAL DEFAULT 23 __data_start
66: 0804a014 4 OBJECT GLOBAL DEFAULT 23 gAp2
67: 0804854c 4 OBJECT GLOBAL DEFAULT 15 gBp2
68: 0804a010 0 OBJECT GLOBAL HIDDEN 23 __dso_handle
69: 08049f18 0 OBJECT GLOBAL HIDDEN 18 __DTOR_END__
70: 08048490 90 FUNC GLOBAL DEFAULT 13 __libc_csu_init
71: 00000000 0 FUNC GLOBAL DEFAULT UND printf@@GLIBC_2.0
72: 08048544 4 OBJECT GLOBAL DEFAULT 15 gAp1
73: 0804a01c 0 NOTYPE GLOBAL DEFAULT ABS __bss_start
74: 0804a018 1 OBJECT GLOBAL DEFAULT 23 gB
75: 0804a024 0 NOTYPE GLOBAL DEFAULT ABS _end
76: 0804a01c 0 NOTYPE GLOBAL DEFAULT ABS _edata
77: 080484ea 0 FUNC GLOBAL HIDDEN 13 __i686.get_pc_thunk.bx
78: 080483c4 182 FUNC GLOBAL DEFAULT 13 main
79: 08048298 0 FUNC GLOBAL DEFAULT 11 _init