-
Notifications
You must be signed in to change notification settings - Fork 0
/
20190512-5.html
695 lines (413 loc) · 41.2 KB
/
20190512-5.html
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<!-- 元数据 -->
<meta charset="utf-8">
<link rel="icon" href="/images/favicon.ico">
<title>C++基础之字节对齐 | 云海鹰影</title>
<meta name="author" content="云海鹰影" />
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="robots" content="index,follow" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<meta name="format-detection" content="telphone=no, email=no" />
<meta name="keywords" content="C++基础, 字节对齐, 编程基础" />
<!-- 站点验证相关 -->
<meta name="baidu-site-verification" content="code-EjIoHOEm6V" />
<link rel="alternate" href="atom.xml" type="application/atom+xml">
<!-- 样式表文件 -->
<link rel="stylesheet" id="kratos-css" href="/css/kratosr.min.css" type="text/css" media="all">
<link rel="stylesheet" id="highlight-css" href="/css/highlight.min.css" type="text/css" media="all">
<link rel="stylesheet" id="fontawe-css" href="https://cdn.jsdelivr.net/npm/[email protected]/css/font-awesome.min.css" type="text/css" media="all">
<link rel="stylesheet" id="nprogress-css" href="https://cdn.jsdelivr.net/npm/[email protected]/nprogress.min.css" type="text/css" media="all">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fancyapps/[email protected]/dist/jquery.fancybox.min.css">
<link rel="stylesheet" id="darkmode-css" href="/css/kr-dark.min.css" type="text/css" media="all">
<!-- 不得不预先加载的一些JS文件 -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/qrcode.min.js"></script>
<meta name="generator" content="Hexo 5.4.2"></head>
<body class="custom-background">
<div id="kratos-wrapper">
<div id="kratos-page">
<div id="kratos-header">
<div class="nav-toggle"><a class="kratos-nav-toggle js-kratos-nav-toggle"><i></i></a></div>
<header id="kratos-header-section">
<div class="container">
<div class="nav-header">
<div class="color-logo"><a href="/">云海鹰影</a></div>
<nav id="kratos-menu-wrap">
<ul id="kratos-primary-menu" class="sf-menu">
<li><a href="/" ><i class="fa fa-home"></i>首页</a></li>
<li><a href="/tags/" ><i class="fa fa-file"></i>标签云</a></li>
<li><a href="/archives/" ><i class="fa fa-file"></i>档案馆</a></li>
<li><a href="/friends/" ><i class="fa fa-paw"></i>友链</a></li>
<li>
<a ><i class="fa fa-umbrella"></i>秘密小窝</a>
<ul class="sub-menu">
<li><a target="_blank" rel="noopener" href="https://smp.chaoswork.cn" >鹰の巢</a></li>
</ul>
</li>
<li>
<a ><i class="fa fa-random"></i>资源库</a>
<ul class="sub-menu">
<li><a href="/resources/frontend" >前端</a></li>
<li><a href="/resources/backend" >后端</a></li>
<li><a href="/resources/tools" >工具</a></li>
</ul>
</li>
<li>
<a ><i class="fa fa-gift"></i>看动漫</a>
<ul class="sub-menu">
<li><a target="_blank" rel="noopener" href="https://www.5dm.tv/" >五弹幕</a></li>
</ul>
</li>
</ul>
</nav>
</div>
</div>
</header>
</div>
<div class="kratos-start kratos-hero-2">
<!-- <div class="kratos-overlay"></div> -->
<div class="kratos-cover kratos-cover-2 text-center">
<div class="desc desc2 animate-box">
<a href="/">
<h2>云海鹰影</h2> <br />
<span>我独自度过的无数个晨昏,用来交换你身边一瞬</span>
</a>
</div>
</div>
</div>
<div id="kratos-blog-post">
<div class="container">
<div class="row">
<div id="main">
<section class="col-md-8">
<article>
<div class="kratos-hentry kratos-post-inner clearfix">
<header class="kratos-entry-header">
<h1 class="kratos-entry-title text-center">C++基础之字节对齐</h1>
<div class="kratos-post-meta text-center">
<span>
<i class="fa fa-calendar"></i> 2019-05-12
<i class="fa fa-folder"></i> 分类 <a class="label-link" href="/categories/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/">编程基础</a>, <a class="label-link" href="/categories/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/C-%E5%9F%BA%E7%A1%80/">C++基础</a>
<i class="fa fa-user"></i> 作者 云海鹰影
<i class="fa fa-edit"></i>
~4.12K
字
</span>
</div>
</header>
<div class="kratos-post-content">
<div id="expire-alert" class="alert alert-warning hidden" role="alert">
本文最后编辑于 <time datetime="1615457057387"></time> 前,其中的内容可能需要更新。
</div>
<hr />
<p>本文转自<a target="_blank" rel="noopener" href="https://www.cnblogs.com/clover-toeic/p/3853132.html">C语言字节对齐问题详解</a>, 做了一定的删减和修改。</p>
<h2 id="1-什么是字节对齐"><a href="#1-什么是字节对齐" class="headerlink" title="1. 什么是字节对齐"></a>1. 什么是字节对齐</h2><p> 现代计算机中内存空间都是按照字节(byte)划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定变量的时候经常在特定的内存地址访问,这就需要各类型数据按照一定的规则在空间上排列,而不是顺序地一个接一个地排放,这就是对齐。</p>
<h2 id="2-对齐的原因和作用"><a href="#2-对齐的原因和作用" class="headerlink" title="2. 对齐的原因和作用"></a>2. 对齐的原因和作用</h2><p>不同硬件平台对存储空间的处理上存在很大的不同。某些平台对特定类型的数据只能从特定地址开始存取,而不允许其在内存中任意存放。例如Motorola 68000 处理器不允许16位的字存放在奇地址,否则会触发异常,因此在这种架构下编程必须保证字节对齐。</p>
<p>但最常见的情况是,如果不按照平台要求对数据存放进行对齐,会带来存取效率上的损失。比如32位的Intel处理器通过总线访问(包括读和写)内存数据。每个总线周期从偶地址开始访问32位内存数据,内存数据以字节为单位存放。如果一个32位的数据没有存放在4字节整除的内存地址处,那么处理器就需要2个总线周期对其进行访问,显然访问效率下降很多。</p>
<p>因此,通过合理的内存对齐可以提高访问效率。为使CPU能够对数据进行快速访问,数据的起始地址应具有“对齐”特性。比如4字节数据的起始地址应位于4字节边界上,即起始地址能够被4整除。</p>
<p>此外,合理利用字节对齐还可以有效地节省存储空间。但要注意,在32位机中使用1字节或2字节对齐,反而会降低变量访问速度。因此需要考虑处理器类型。还应考虑编译器的类型。在VC/C++和GNU GCC中都是默认是4字节对齐。</p>
<h2 id="3-对齐的分类和准则"><a href="#3-对齐的分类和准则" class="headerlink" title="3. 对齐的分类和准则"></a>3. 对齐的分类和准则</h2><p>本文主要基于Intel X86架构介绍<font color=blue><strong>结构体对齐</strong></font>,<font color=blue><strong>栈内存对齐</strong></font>和<font color=blue><strong>位域对齐</strong></font>,位域本质上为结构体类型。</p>
<p>对于Intel X86平台,每次分配内存应该是从4的整数倍地址开始分配,无论是对结构体变量还是简单类型的变量。</p>
<h3 id="3-1-结构体对齐"><a href="#3-1-结构体对齐" class="headerlink" title="3.1 结构体对齐"></a>3.1 结构体对齐</h3><p>在C语言中,结构体是种复合数据类型,其构成元素既可以是基本数据类型(如int、long、float等)的变量,也可以是一些复合数据类型(如数组、结构体、联合等)的数据单元。编译器为结构体的每个成员按照其自然边界(alignment)分配空间。各成员按照它们被声明的顺序在内存中顺序存储,第一个成员的地址和整个结构的地址相同。</p>
<p>字节对齐的问题主要就是针对结构体。</p>
<h4 id="3-1-1-简单示例"><a href="#3-1-1-简单示例" class="headerlink" title="3.1.1 简单示例"></a>3.1.1 简单示例</h4><p>先看个简单的例子(32位,X86处理器,GCC编译器):<br>【例1】设结构体如下定义:</p>
<figure class="highlight c" data-lang="c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">A</span>{</span></span><br><span class="line"> <span class="type">int</span> a;</span><br><span class="line"> <span class="type">char</span> b;</span><br><span class="line"> <span class="type">short</span> c;</span><br><span class="line">};</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">B</span>{</span></span><br><span class="line"> <span class="type">char</span> b;</span><br><span class="line"> <span class="type">int</span> a;</span><br><span class="line"> <span class="type">short</span> c;</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<p>已知32位机器上各数据类型的长度为:char为1字节、short为2字节、int为4字节、long为4字节、float为4字节、double为8字节。那么上面两个结构体大小如何呢?</p>
<p>结果是:sizeof(strcut A)值为8;sizeof(struct B)的值却是12。</p>
<p>结构体A中包含一个4字节的int数据,一个1字节char数据和一个2字节short数据;B也一样。按理说A和B大小应该都是7字节。之所以出现上述结果,就是因为编译器要对数据成员在空间上进行对齐。</p>
<h4 id="3-1-2-对齐准则"><a href="#3-1-2-对齐准则" class="headerlink" title="3.1.2 对齐准则"></a>3.1.2 对齐准则</h4><p>先来看四个重要的基本概念:</p>
<blockquote>
<ol>
<li><font color=blue><strong>数据类型自身的对齐值</strong></font>:就是基本数据类型的自身对齐值,比如char类型的自身对齐值为1字节,int类型的自身对齐值为4字节。</li>
<li><font color=blue><strong>结构体或者类的自身对齐值</strong></font>:其成员中自身对齐值最大的那个值,比如以上的struct A的对齐值为4。</li>
<li><font color=blue><strong>指定对齐值</strong></font>:预编译命令#pragma pack (value)指定的对齐值value。</li>
<li><font color=blue><strong>数据成员、结构体和类的有效对齐值</strong></font>:自身对齐值和指定对齐值中较小者,即<font color=brwong>有效对齐值=min{自身对齐值,当前指定的pack值}</font>。</li>
</ol>
</blockquote>
<p>其中,有效对齐值N是最终用来决定数据存放地址方式的值。有效对齐N表示“对齐在N上”,即该数据的“存放起始地址%N=0”。而数据结构中的数据变量都是按定义的先后顺序存放。第一个数据变量的起始地址就是数据结构的起始地址。结构体的成员变量要对齐存放,结构体本身也要根据自身的有效对齐值圆整(即结构体成员变量占用总长度为结构体有效对齐值的整数倍)。</p>
<p>基于上面定义和说明,我们可以方便的得出结构体字节对齐满足的三个一般性准则:</p>
<blockquote>
<ol>
<li>结构体变量的首地址能够被其最宽基本类型成员的大小所整除;</li>
<li>结构体每个成员相对结构体首地址的偏移量(offset)都是成员大小的整数倍,如有需要编译器会在成员之间加上填充字节(internal adding);</li>
<li>结构体的总大小为结构体最宽基本类型成员大小的整数倍,如有需要编译器会在最末一个成员之后加上填充字节{trailing padding}。</li>
</ol>
</blockquote>
<p>以此分析3.1.1节中的结构体B:<br>假定对齐值默认为4,则char的有效对齐值为1,int的有效对齐值为4,short的有效对齐值为2;则结构体的有效对齐值为4;假设结构体B内存起始地址为0x0000, 按照结构体B的变量定义顺序,可以简单地知道变量char b内存地址为0x0000,占1个字节;变量int a 内存地址为0x0004,占4个字节;变量short c内存地址为0x0008,占2个字节。至此结构体的大小为(0x0008-0x0000) + 2 = 10; 根据结构体圆整的要求(准则3),编译器会在后面补充2个字节以满足4的整数倍要求,所以sizeof(struct B)=12。</p>
<p>再来看一个例子:<br>【例2】假设4字节对齐,以下程序的输出结果是多少?</p>
<figure class="highlight c" data-lang="c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* OFFSET宏定义可取得指定结构体某成员在结构体内部的偏移 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> OFFSET(st, field) (size_t)&(((st*)0)->field)</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span>{</span></span><br><span class="line"> <span class="type">char</span> a;</span><br><span class="line"> <span class="type">short</span> b;</span><br><span class="line"> <span class="type">char</span> c;</span><br><span class="line"> <span class="type">int</span> d;</span><br><span class="line"> <span class="type">char</span> e[<span class="number">3</span>];</span><br><span class="line">}T_Test;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span>{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"Size = %d\n a-%d, b-%d, c-%d, d-%d\n e[0]-%d, e[1]-%d, e[2]-%d\n"</span>,</span><br><span class="line"> <span class="keyword">sizeof</span>(T_Test), OFFSET(T_Test, a), OFFSET(T_Test, b),</span><br><span class="line"> OFFSET(T_Test, c), OFFSET(T_Test, d), OFFSET(T_Test, e[<span class="number">0</span>]),</span><br><span class="line"> OFFSET(T_Test, e[<span class="number">1</span>]),OFFSET(T_Test, e[<span class="number">2</span>]));</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>执行后输出如下:</p>
<figure class="highlight plaintext" data-lang="plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Size = 16</span><br><span class="line"> a-0, b-2, c-4, d-8</span><br><span class="line"> e[0]-12, e[1]-13, e[2]-14</span><br></pre></td></tr></table></figure>
<p>下面来具体分析:<br>首先char a占用1个字节,没问题。<br>short b本身占用2个字节,根据上面准则2,需要在b和a之间填充1个字节。<br>char c占用1个字节,没问题。<br>int d本身占用4个字节,根据准则2,需要在d和c之间填充3个字节。<br>char e[3];本身占用3个字节,根据原则3,需要在其后补充1个字节。<br>因此,sizeof(T_Test) = 1 + 1 + 2 + 1 + 3 + 4 + 3 + 1 = 16字节。</p>
<h4 id="3-1-3-对齐的隐患"><a href="#3-1-3-对齐的隐患" class="headerlink" title="3.1.3 对齐的隐患"></a>3.1.3 对齐的隐患</h4><h5 id="3-1-3-1-数据类型转换"><a href="#3-1-3-1-数据类型转换" class="headerlink" title="3.1.3.1 数据类型转换"></a>3.1.3.1 数据类型转换</h5><p>代码中关于对齐的隐患,很多是隐式的。例如,在强制类型转换的时候:</p>
<figure class="highlight c" data-lang="c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span>{</span><br><span class="line"> <span class="type">unsigned</span> <span class="type">int</span> i = <span class="number">0x12345678</span>;</span><br><span class="line"> <span class="type">unsigned</span> <span class="type">char</span> *p = (<span class="type">unsigned</span> <span class="type">char</span> *)&i;</span><br><span class="line"> *p = <span class="number">0x00</span>;</span><br><span class="line"> <span class="type">unsigned</span> <span class="type">short</span> *p1 = (<span class="type">unsigned</span> <span class="type">short</span> *)(p+<span class="number">1</span>);</span><br><span class="line"> *p1 = <span class="number">0x0000</span>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>最后两句代码,从奇数边界去访问unsigned short型变量,显然不符合对齐的规定。在X86上,类似的操作只会影响效率;但在MIPS或者SPARC上可能导致error,因为它们要求必须字节对齐。</p>
<h5 id="3-1-3-2-排查对齐问题"><a href="#3-1-3-2-排查对齐问题" class="headerlink" title="3.1.3.2 排查对齐问题"></a>3.1.3.2 排查对齐问题</h5><p>如果出现对齐或者赋值问题可查看:</p>
<ol>
<li>编译器的字节序大小端设置;</li>
<li>处理器架构本身是否支持非对齐访问;</li>
<li>如果支持看设置对齐与否,如果没有则看访问时需要加某些特殊的修饰来标志其特殊访问操作。</li>
</ol>
<h4 id="3-1-4-更改对齐方式"><a href="#3-1-4-更改对齐方式" class="headerlink" title="3.1.4 更改对齐方式"></a>3.1.4 更改对齐方式</h4><p>主要是更改C编译器的缺省字节对齐方式。</p>
<p>在缺省情况下,C编译器为每一个变量或是数据单元按其自然对界条件分配空间。一般地,可以通过下面的方法来改变缺省的对界条件:</p>
<ul>
<li>使用伪指令<code>#pragma pack(n)</code>:C编译器将按照n个字节对齐;</li>
<li>使用伪指令<code>#pragma pack()</code>: 取消自定义字节对齐方式。</li>
</ul>
<p>另外,还有如下的一种方式(GCC特有语法):</p>
<ul>
<li><code>__attribute((aligned (n)))</code>: 让所作用的结构成员对齐在n字节自然边界上。如果结构体中有成员的长度大于n,则按照最大成员的长度来对齐。</li>
<li><code>__attribute__ ((packed))</code>: 取消结构在编译过程中的优化对齐,按照实际占用字节数进行对齐。</li>
</ul>
<p>【注】<code>__attribute__</code>机制是GCC的一大特色,可以设置函数属性(Function Attribute)、变量属性(Variable Attribute)和类型属性(Type Attribute)。</p>
<h3 id="3-2-栈内存对齐"><a href="#3-2-栈内存对齐" class="headerlink" title="3.2 栈内存对齐"></a>3.2 栈内存对齐</h3><p>待整理。</p>
<h3 id="3-3-位域对齐"><a href="#3-3-位域对齐" class="headerlink" title="3.3 位域对齐"></a>3.3 位域对齐</h3><p>待整理。</p>
</div>
<div class="kratos-copyright text-center clearfix">
<h5>本作品采用 <a rel="license nofollow" target="_blank" href="http://creativecommons.org/licenses/by-sa/4.0/">知识共享署名-相同方式共享 4.0 国际许可协议</a> 进行许可</h5>
</div>
<footer class="kratos-entry-footer clearfix">
<div class="post-like-donate text-center clearfix" id="post-like-donate">
<a class="share" href="javascript:;"><i class="fa fa-share-alt"></i> 分享</a>
<div class="share-wrap" style="display: none;">
<div class="share-group">
<a href="javascript:;" class="share-plain qq" onclick="share('qq');" rel="nofollow">
<div class="icon-wrap">
<i class="fa fa-qq"></i>
</div>
</a>
<a href="javascript:;" class="share-plain qzone" onclick="share('qzone');" rel="nofollow">
<div class="icon-wrap">
<i class="fa fa-star"></i>
</div>
</a>
<a href="javascript:;" class="share-plain weixin pop style-plain" rel="nofollow">
<div class="icon-wrap">
<i class="fa fa-weixin"></i>
</div>
<div class="share-int">
<div class="qrcode" id="wechat-qr"></div>
<p>打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮</p>
</div>
</a>
<a href="javascript:;" class="share-plain weibo" onclick="share('weibo');" rel="nofollow">
<div class="icon-wrap">
<i class="fa fa-weibo"></i>
</div>
</a>
<a href="javascript:;" class="share-plain facebook style-plain" onclick="share('facebook');" rel="nofollow">
<div class="icon-wrap">
<i class="fa fa-facebook"></i>
</div>
</a>
<a href="javascript:;" class="share-plain twitter style-plain" onclick="share('twitter');" rel="nofollow">
<div class="icon-wrap">
<i class="fa fa-twitter"></i>
</div>
</a>
</div>
<script type="text/javascript">
$(()=>{
new QRCode("wechat-qr", {
text: "https://www.chaoswork.cn/20190512-5.html",
width: 150,
height: 150,
correctLevel : QRCode.CorrectLevel.H
});
});
function share(dest) {
const qqBase = "https://connect.qq.com/widget/shareqq/index.html?";
const weiboBase = "https://service.weibo.com/share/share.php?";
const qzoneBase = "https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?";
const facebookBase = "https://www.facebook.com/sharer/sharer.php?";
const twitterBase = "https://twitter.com/intent/tweet?";
const hostUrl = "https://www.chaoswork.cn/20190512-5.html";
const title = "「C++基础之字节对齐」";
const excerpt = `本文转自C语言字节对齐问题详解, 做了一定的删减和修改。
1. 什么是字节对齐 现代计算机中内存空间都是按照字节(byte)划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定变量的时候经常在特定的...`;
let _URL;
switch (dest) {
case "qq" : _URL = qqBase+"url="+hostUrl+"&title="+title+"&desc=&summary="+excerpt+"&site=cxpy"; break;
case "weibo" : _URL = weiboBase+"url="+hostUrl+"&title="+title+excerpt; break;
case "qzone" : _URL = qzoneBase+"url="+hostUrl+"&title="+title+"&desc=&summary="+excerpt+"&site=cxpy"; break;
case "facebook" : _URL = facebookBase+"u="+hostUrl; break;
case "twitter" : _URL = twitterBase+"text="+title+excerpt+"&url="+hostUrl; break;
}
window.open(_URL);
};
</script>
</div>
</div>
<div class="footer-tag clearfix">
<div class="pull-left">
<i class="fa fa-tags"></i>
<a class="tag-none-link" href="/tags/C-%E5%9F%BA%E7%A1%80/" rel="tag">C++基础</a>, <a class="tag-none-link" href="/tags/%E5%AD%97%E8%8A%82%E5%AF%B9%E9%BD%90/" rel="tag">字节对齐</a>, <a class="tag-none-link" href="/tags/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/" rel="tag">编程基础</a>
</div>
<div class="pull-date">
<span>最后编辑:2021-03-11</span>
</div>
</div>
</footer>
</div>
<nav class="navigation post-navigation clearfix" role="navigation">
<div class="nav-previous clearfix">
<a title=" 面向对象设计之建造者模式" href="/20190512-4.html">< 上一篇</a>
</div>
<div class="nav-next clearfix">
<a title=" 前缀、中缀、后缀表达式" href="/20190512-6.html">下一篇 ></a>
</div>
</nav>
<link rel="stylesheet" href="https://cdn.bootcss.com/gitalk/1.4.1/gitalk.min.css">
<div id="gitalk-container"></div>
<script type="text/javascript">
var load_comm = () => {
const init = () => {
const gitalk = new Gitalk({
// Gitalk配置
language: "zh-CN",
clientID: "7dff06239ac01f6006c8",
clientSecret: "1cf779fc33e9e0d353f196eefa4bb43ecd560889",
repo: "comment_data",
owner: "stayknight",
admin: ["stayknight"],
id: location.pathname,
distractionFreeMode: false
});
gitalk.render('gitalk-container');
};
if (typeof Gitalk == 'undefined') {
const src = 'https://cdn.bootcss.com/gitalk/1.4.1/gitalk.min.js';
$.getScript(src, init);
} else {
init();
}
};
</script>
</article>
</section>
</div>
<section id="kratos-widget-area" class="col-md-4 hidden-xs hidden-sm">
<aside id="krw-about" class="widget widget-kratos-about clearfix">
<div class="photo-background"></div>
<div class="photo-wrapper clearfix">
<div class="photo-wrapper-tip text-center">
<img class="about-photo" src="https://cdn.jsdelivr.net/npm/[email protected]/avatars/icon41.jpg" />
</div>
</div>
<div class="textwidget">
<p class="text-center">路漫漫其修远兮,吾将上下而求索~<br/><span>(右下角搜索)</span></p>
</div>
</aside>
<!-- Moved to about.ejs -->
<aside id="krw-categories" class="widget widget-categories clearfix">
<h4 class="widget-title"><i class="fa fa-folder"></i>分类目录</h4>
<ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/ACGN/">ACGN</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91/">前端开发</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91/">游戏开发</a><span class="category-list-count">1</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91/%E6%9C%8D%E5%8A%A1%E5%99%A8/">服务器</a><span class="category-list-count">1</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%94%9F%E6%B4%BB%E5%AD%A6%E4%B9%A0/">生活学习</a><span class="category-list-count">8</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%94%9F%E6%B4%BB%E5%AD%A6%E4%B9%A0/%E6%9D%82%E8%B0%88/">杂谈</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%94%9F%E6%B4%BB%E5%AD%A6%E4%B9%A0/%E8%AF%BB%E4%B9%A6/">读书</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%94%9F%E6%B4%BB%E5%AD%A6%E4%B9%A0/%E9%9F%B3%E4%B9%90/">音乐</a><span class="category-list-count">2</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BB%8F%E9%AA%8C%E6%95%99%E7%A8%8B/">经验教程</a><span class="category-list-count">16</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BB%8F%E9%AA%8C%E6%95%99%E7%A8%8B/%E5%8D%9A%E5%AE%A2/">博客</a><span class="category-list-count">5</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/">编程基础</a><span class="category-list-count">39</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/C-%E5%9F%BA%E7%A1%80/">C++基础</a><span class="category-list-count">7</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/linux%E5%9F%BA%E7%A1%80/">linux基础</a><span class="category-list-count">11</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/">数据结构与算法</a><span class="category-list-count">9</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/">网络编程</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/">设计模式</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/%E8%BD%AF%E4%BB%B6%E5%B7%A5%E7%A8%8B/">软件工程</a><span class="category-list-count">2</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%B5%84%E6%BA%90%E5%88%86%E4%BA%AB/">资源分享</a><span class="category-list-count">2</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%B5%84%E6%BA%90%E5%88%86%E4%BA%AB/%E5%BC%80%E5%8F%91%E8%B5%84%E6%BA%90/">开发资源</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%B5%84%E6%BA%90%E5%88%86%E4%BA%AB/%E6%BA%90%E7%A0%81/">源码</a><span class="category-list-count">1</span></li></ul></li></ul>
</aside>
<aside id="krw-tags" class="widget widget-kratos-tags clearfix">
<h4 class="widget-title"><i class="fa fa-tags"></i>标签云</h4>
<div class="tag-clouds">
<a href="/tags/ACGN/" style="font-size: 0.75em;">ACGN</a> <a href="/tags/ACM/" style="font-size: 0.6em;">ACM</a> <a href="/tags/AVL%E6%A0%91/" style="font-size: 0.6em;">AVL树</a> <a href="/tags/C/" style="font-size: 0.6em;">C++</a> <a href="/tags/C-%E5%9F%BA%E7%A1%80/" style="font-size: 0.8em;">C++基础</a> <a href="/tags/C-%E7%B1%BB%E5%9E%8B/" style="font-size: 0.65em;">C++类型</a> <a href="/tags/IO%E5%A4%9A%E8%B7%AF%E5%A4%8D%E7%94%A8/" style="font-size: 0.65em;">IO多路复用</a> <a href="/tags/IO%E6%A8%A1%E5%9E%8B/" style="font-size: 0.6em;">IO模型</a> <a href="/tags/MYSQL/" style="font-size: 0.65em;">MYSQL</a> <a href="/tags/SAO/" style="font-size: 0.6em;">SAO</a> <a href="/tags/adb/" style="font-size: 0.6em;">adb</a> <a href="/tags/anacron/" style="font-size: 0.6em;">anacron</a> <a href="/tags/centos/" style="font-size: 0.7em;">centos</a> <a href="/tags/cms/" style="font-size: 0.6em;">cms</a> <a href="/tags/cron/" style="font-size: 0.6em;">cron</a> <a href="/tags/crontab/" style="font-size: 0.6em;">crontab</a> <a href="/tags/epoll/" style="font-size: 0.6em;">epoll</a> <a href="/tags/gcc/" style="font-size: 0.6em;">gcc</a>
</div>
</aside>
<aside id="krw-posts" class="widget widget-kratos-poststab">
<h4 class="widget-title"><i class="fa fa-file"></i>最新文章</h4>
<div class="tab-content">
<ul class="list-group">
<a class="list-group-item" href="/20230825-1.html"><i class="fa fa-book"></i> 工作中遇到的一些数据库问题记录(随时更新)</a>
<a class="list-group-item" href="/20230508-1.html"><i class="fa fa-book"></i> C++开源游戏服务器或脚手架记录</a>
<a class="list-group-item" href="/20230302-1.html"><i class="fa fa-book"></i> ubuntu scp命令的一个问题</a>
<a class="list-group-item" href="/20230227-1.html"><i class="fa fa-book"></i> linux僵尸进程和孤儿进程一点记录</a>
<a class="list-group-item" href="/20230224-1.html"><i class="fa fa-book"></i> 关于带返回值的函数未返回引起的Undefined Behavior问题</a>
</ul>
</div>
</aside>
</section>
</div>
</div>
</div>
<footer>
<div id="footer">
<div class="kr-tool text-center">
<div class="tool">
<div class="box search-box">
<a href="/search/">
<span class="fa fa-search"></span>
</a>
</div>
<div class="box theme-box" id="darkmode-switch">
<span class="fa fa-adjust"></span>
</div>
</div>
<div class="box gotop-box">
<span class="fa fa-chevron-up"></span>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 footer-list text-center">
<ul class="kratos-social-icons text-center">
<li><a href="mailto:[email protected]"><i class="fa fa-envelope"></i></a></li>
<li><a target="_blank" rel="me" href="https://smp.chaoswork.cn/@stayknight"><i class="fa fa fa-share-alt-square"></i></a></li>
<li><a target="_blank" rel="nofollow" href="https://github.com/stayknight"><i class="fa fa-github"></i></a></li>
<li><a target="_blank" rel="nofollow" href="/atom.xml"><i class="fa fa-rss"></i></a></li>
</ul>
<ul class="kratos-copyright">
<div>
<li>© 2023 云海鹰影 版权所有.</li>
<li>本站已运行<span id="span_dt">Loading...</span></li>
</div>
<div>
<li>Theme <a href="https://github.com/Candinya/Kratos-Rebirth" target="_blank">Kratos:Rebirth</a></li>
<li>Made with <i class="fa fa-heart throb" style="color:#d43f57"></i> by <a href="https://candinya.com" target="_blank" rel="nofollow">Candinya</a>.</li>
</div>
<div>
<li>Powered by <a href="https://hexo.io" target="_blank" rel="nofollow">Hexo</a></li>
<li>Hosted on <a href="https://github.io" target="_blank">Github Pages</a></li>
</div>
<div>
<li><a href="https://beian.miit.gov.cn" rel="external nofollow" target="_blank">鄂ICP备17025858号-5</a></li>
<li><a href="http://www.beian.gov.cn" rel="external nofollow" target="_blank"><img src="/images/psr.webp" width="12" height="12"> xxxxx</a></li>
<li><a href="sitemap.xml" target="_blank">站点地图</a></li>
<li><a href="baidusitemap.xml" target="_blank">百度地图</a></li>
</div>
</ul>
</div>
</div>
</div>
</div>
</footer>
</div>
</div>
<script type="text/javascript" charset="utf-8">
$("img.headicon").one("error", function(e) {
$(this).attr("src", "/images/avatar.webp");
});
</script>
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/nprogress.min.js"></script>
<script>const notMobile = (!(navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)));</script>
<div>
<canvas id="snow"></canvas>
<script async type="text/javascript" src="/js/snow.min.js"></script>
</div>
<script async src="/js/candy.min.js"></script>
<script defer src=""></script>
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/clipboard.min.js"></script>
<script defer src="/js/kratosr.min.js"></script>
<script defer src="/js/pjax.min.js"></script>
<script defer src="/js/kr-dark.min.js"></script>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "//hm.baidu.com/hm.js?bfb917536df0b5a503a5690e6de9ef9c";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
</body>
</html>