zoukankan      html  css  js  c++  java
  • 基础

      1 // 结构体大小.cpp : 定义控制台应用程序的入口点。
      2 //
      3 
      4 #include "stdafx.h"
      5 #include <windows.h>
      6 #include <iostream>
      7 
      8 using namespace std;
      9 
     10 
     11 /*
     12 结构体计算要遵循字节对齐原则
     13 
     14 结构体默认的字节对齐一般满足三个准则:
     15 
     16 1) 结构体变量的首地址能够被其最宽基本类型成员的大小所整除;
     17 
     18 2) 结构体每个成员相对于结构体首地址的偏移量(offset)都是成员大小的整数倍,如有需要编译器会在成员之间加上填充字节(internal adding);
     19 
     20 3) 结构体的总大小为结构体最宽基本类型成员大小的整数倍,如有需要编译器会在最末一个成员之后加上填充字节(trailing padding)
     21 
     22 */
     23 
     24 
     25 typedef struct _S1_
     26 { 
     27     char  a;   
     28     short b;   
     29     char  c; 
     30 }S1;
     31 /*
     32 S1中,最大是short,2字节
     33 char->short->char
     34 char是一个字节,要对齐,附近又没有另外的char和他凑
     35 所以
     36 2
     37 2
     38 2
     39 
     40 
     41 第一个char占一个字节        多的这一个补0,只是占位作用
     42 short                   刚好占2个字节
     43 第二个char也占1个            多的这一个补0,只是占位作用
     44 
     45 
     46 一共6字节
     47 
     48 */
     49 
     50 
     51 
     52 typedef struct _S2_
     53 { 
     54     char  a;  
     55     char  b;  
     56     short c; 
     57 }S2;
     58 /*
     59 S1=2中,最大是short,2字节
     60 char->char->short
     61 
     62 char是一个字节,要对齐,
     63 第一个char和第二个char正好凑成两个字节
     64 
     65 第一个char占一个字节        第二个char也占1个
     66 short                   刚好占2个字节
     67 
     68 
     69 所以
     70 1 1
     71 2
     72 
     73  74 2
     75 2
     76 
     77 一共4字节
     78 
     79 */
     80 
     81 
     82 
     83 
     84 //同理S1、S2
     85 typedef struct _S3_
     86 {
     87     int  i;
     88     char c;
     89     int  j;
     90 }S3;
     91 
     92 
     93 
     94 //同理S1、S2
     95 typedef struct _S4_
     96 {
     97     int  i;
     98     int  j;
     99     char c;
    100 }S4;
    101 
    102 
    103 
    104 
    105 typedef struct _A_
    106 {
    107     char      a1;
    108     short int a2;
    109     int       a3;
    110     double     d;
    111 
    112 }A;
    113 
    114 
    115 
    116 typedef struct _B_
    117 {
    118     long int  b2;
    119     short int b1;
    120     A          a;
    121 
    122 }B;
    123 /*
    124 当结构体成员变量是另外一个结构体时,只要把结构体中成员为另一结构体作为整体相加就行
    125 
    126 对于B,先去掉A a 
    127 体B 算得 其为8,
    128 所以最后结果为8+16=24;
    129 
    130 */
    131 
    132 
    133 
    134 
    135 int main()
    136 {
    137     cout << sizeof(S1) << endl;
    138     cout << sizeof(S2) << endl;
    139     cout << sizeof(S3) << endl;
    140     cout << sizeof(S4) << endl;
    141     cout << sizeof(A)  << endl;
    142     cout << sizeof(B)  << endl;
    143 
    144     return 0;
    145 }
  • 相关阅读:
    恢复spark挂掉的节点
    启动spark集群
    记录一下SparkStreaming中因为使用redis做数据验证而导致数据结果不对的问题
    ps -aux与ps -ef
    Operation category READ is not supported in state standby
    spark web ui中的skipped的含义
    关于spark ui中executor显示的内存量与设置的内存量不符的问题
    flume修改配置文件
    maven中的各种问题
    java 的集合框架
  • 原文地址:https://www.cnblogs.com/1228073191Blog/p/7448694.html
Copyright © 2011-2022 走看看