探索記憶體大小段儲存

來源:互聯網
上載者:User

標籤:大小端儲存

1.650) this.width=650;" src="http://s1.51cto.com/wyfs02/M00/7F/6C/wKiom1cd5XywQXGiAABIHlL_Jyk476.png" style="float:none;" title="QQ圖片20160425173251.png" alt="wKiom1cd5XywQXGiAABIHlL_Jyk476.png" />

A的ASCII碼16進位儲存為41,對應B,C,D,E為42,43,44,45;

VS下為小段模式

2.650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/7F/6C/wKiom1cd5X2xQI6tAABbzfupj8s910.png" style="float:none;" title="31LK]E~~]%3HK[8CSQJMM_G.png" alt="wKiom1cd5X2xQI6tAABbzfupj8s910.png" />

將高精度存入低精度時,截取掉高位,高位補ff,記憶體中預設用cc初始化

650) this.width=650;" src="http://s2.51cto.com/wyfs02/M02/7F/6C/wKiom1cd5_vCgAhyAAGo9SKOnSU952.png" title="QQ圖片20160425174414.png" alt="wKiom1cd5_vCgAhyAAGo9SKOnSU952.png" />

3.

#include<stddef.h>#pragma pack(4)struct A{int a;char b;short c;char d[11];};aaaab0ccddddddddddd//應補齊最大對齊數的整數倍int main(){cout << sizeof(A) << endl;cout << offsetof(A,a) << offsetof(A,b) << offsetof(A,c) << offsetof(A,d) << endl;system("pause");return 0;}

運行結果為:20   0,4,6,8

4.

struct B{int a;char c;double d;};//aaaac000//ddddddddcout << sizeof(B) << endl;cout << offsetof(B,a) << offsetof(B,c) << offsetof(B,d) << endl;

運行結果:16  0,4,8

5.

struct A{int a;char b;short c;char d[11];};struct B{int a;char c;double d;A e;};cout << sizeof(B) << endl;cout << offsetof(B, a) << offsetof(B, c) << offsetof(B, d) << offsetof(B, e) << endl;

運行結果:36  0,4,8,16

e不是對齊到d後面,按A本身最大整數倍對齊數。

6.

#include<stddef.h>#pragma pack(4)struct A{int a;char b;short c;char d[11];};struct B{int a;double d;char c;A e;};cout << sizeof(B) << endl;cout << offsetof(B, a) << offsetof(B, c) << offsetof(B, d) << offsetof(B, e) << endl;

運行結果:36   0,4,12,16

7.

#include<stddef.h>#pragma pack(8)struct A{int a;char b;short c;char d[11];};struct B{int a;double d;char c;A e;};cout << sizeof(B) << endl;cout << offsetof(B, a) << offsetof(B, c) << offsetof(B, d) << offsetof(B, e) << endl;

運行結果:40   0,8,16,20

總結:

  1. 結構體中第一個變數地址等於結構體起始地址

  2. 第一個變數永遠對齊到0位移(相對於結構體的起始位置),永遠對齊(結構體中最大對齊數整數倍)-----結構體大小。

  3. 對齊數:結構體中最大的和系統中的較小者。windows:8,Linux:4

本文出自 “小止” 部落格,請務必保留此出處http://10541556.blog.51cto.com/10531556/1767609

探索記憶體大小段儲存

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.