電脳梁山泊烏賊塾 構造体のサイズ. Visual Basic

Similar documents
サーブレットと Android との連携. Generated by Foxit PDF Creator Foxit Software For evaluation only.

Yamaha Steinberg USB Driver V for Mac Release Notes

Unofficial Redmine Cooking - QA #782 yaml_db を使った DB のマイグレーションで失敗する

フラクタル 1 ( ジュリア集合 ) 解説 : ジュリア集合 ( 自己平方フラクタル ) 入力パラメータの例 ( 小さな数値の変化で模様が大きく変化します. Ar や Ai の数値を少しずつ変化させて描画する. ) プログラムコード. 2010, AGU, M.

Cloud Connector 徹底解説. 多様な基盤への展開を可能にするための Citrix Cloud のキーコンポーネント A-5 セールスエンジニアリング本部パートナー SE 部リードシステムズエンジニア. 哲司 (Satoshi Komiyama) Citrix

Computer Programming I (Advanced)

API サーバの URL. <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE COMPLIANCE_SCAN SYSTEM "

Methods to Detect Malicious MS Document File using File Structure Inspection

Androidプログラミング 2 回目 迫紀徳

Certificate of Accreditation

Introduction to Information and Communication Technology (a)

携帯電話の 吸収率 (SAR) について / Specific Absorption Rate (SAR) of Mobile Phones

WD/CD/DIS/FDIS stage

携帯電話の 吸収率 (SAR) について / Specific Absorption Rate (SAR) of Mobile Phones

Yamaha Steinberg USB Driver V for Windows Release Notes

本書について... 7 本文中の表記について... 7 マークについて... 7 MTCE をインストールする前に... 7 ご注意... 7 推奨 PC 仕様... 8 MTCE をインストールする... 9 MTCE をアンインストールする... 11

Relaxed Consistency models and software distributed memory. Computer Architecture Textbook pp.79-83

Agilent. IO Libraries Suite 16.3/16.2 簡易取扱説明書. [ IO Libraries Suite 最新版 ]

J の Lab システムの舞台裏 - パワーポイントはいらない -

MySQL Cluster 7.3 リリース記念!! 5 分で作る MySQL Cluster 環境

UB-U01III/U02III/U03II User s Manual

Yamaha Steinberg USB Driver V for Windows Release Notes

2. 集団の注目位置推定 提案手法では 複数の人物が同一の対象を注視している状況 置 を推定する手法を検討する この状況下では 図 1 のよう. 顔画像からそれぞれの注目位置を推定する ただし f は 1 枚 この仮説に基づいて 複数の人物を同時に撮影した低解像度顔

Private Sub 終了 XToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 終了 XToolStripMenuItem.

Snoop cache. AMANO, Hideharu, Keio University Textbook pp.40-60

Certificate of Accreditation

PSLT Adobe Typekit Service (2016v1.1)

PNRGOV/Ver11.1/ 旅客氏名表予約情報報告 (PNR01)

Quick Install Guide. Adaptec SCSI RAID 2120S Controller

IRS16: 4 byte ASN. Version: 1.0 Date: April 22, 2008 Cisco Systems 2008 Cisco, Inc. All rights reserved. Cisco Systems Japan

サンプル. NI TestStand TM I: Introduction Course Manual

DürrConnect the clever connection. The quick connection with the Click

DSK8AD1DA. 8ch A/D & 1ch D/A for DSK/EVM.

URL IO オブジェクト指向プログラミング特論 2018 年度只木進一 : 工学系研究科

TOOLS for MR V1.7.7 for Mac Release Notes

~ ソフトウエア認証への取り組みと課題 ~

Quick Installation Manual

PRODUCT DESCRIPTIONS AND METRICS

Synchronization with shared memory. AMANO, Hideharu Textbook pp.60-68

PCIe SSD PACC EP P3700 Intel Solid-State Drive Data Center Tool

A. 展開図とそこから折れる凸立体の研究 1. 複数の箱が折れる共通の展開図 2 通りの箱が折れる共通の展開図 3 通りの箱が折れる共通の展開図そして. 残された未解決問題たち 2. 正多面体の共通の展開図 3. 正多面体に近い立体と正 4 面体の共通の展開図 ( 予備 )

和英対訳版. IEC Standard Template のユーザーガイド 備考 : 英語原文掲載 URL ( 一財 ) 日本規格協会

振込依頼書記入要領 Entry Guide for Direct Deposit Request Form

The Secret Life of Components

UML. A Model Trasformation Environment for Embedded Control Software Design with Simulink Models and UML Models

今日の予定 1. 展開図の基礎的な知識 1. 正多面体の共通の展開図. 2. 複数の箱が折れる共通の展開図 :2 時間目 3. Rep-Cube: 最新の話題 4. 正多面体に近い立体と正 4 面体の共通の展開図 5. ペタル型の紙で折るピラミッド型 :2 時間目 ~3 時間目

Rechargeable LED Work Light

NI TB Introduction. Conventions INSTALLATION INSTRUCTIONS Wire Terminal Block for the NI PXI-2529

JASCO-HPLC Operating Manual. (Analytical HPLC)

Centralized (Indirect) switching networks. Computer Architecture AMANO, Hideharu

BMW Head Up Display (HUD) Teardown BMW ヘッドアップディスプレイティアダウン

Operational Precaution

Kb xp download. Мобильный портал WAP версия: wap.altmaster.ru

Vehicle Calibration Techniques Established and Substantiated for Motorcycles

Web Billing User Guide

Structured Report Implement rev2.1. Konica Minolta Medical & Graphic Inc. R&D Center Software Development Division Hiroyuki KUBOTA 2006/7/20

YAS530B MS-3E Magnetic Field Sensor Type 3E

楽天株式会社楽天技術研究所 Autumn The Seasar Foundation and the others all rights reserved.

Lecture 4 Branch & cut algorithm

4. 今回のプログラム 4.2 解決のクラス SearchNumber.java

Unified System Management Technology for Data Centres

此処 夏? プログラムの概要 VB マウストレーニングソフト 此処 夏? で有る 無人島に取り残された女の子を マウスでドラッグして浅瀬を通り 椰子の島に導いて行く 一般的に 実用プログラムに比較するとゲームプログラムは 高度なテクニックを要求される事が多い

Zabbix ログ解析方法. 2018/2/14 サイバートラスト株式会社 Linux/OSS 事業部技術統括部花島タケシ. Copyright Cybertrust Japan Co., Ltd. All rights reserved.

MathWorks Products and Prices Japan September 2016

Video Annotation and Retrieval Using Vague Shot Intervals

Infragistics ASP.NET リリースノート

QuickTransit for Solaris/SPARC-to-Linux/Itanium 動作検証報告書 株式会社ネットワールドシステム基盤技術部プラットフォームソリューショングループ丸山真一

2007/10/17 ソフトウェア基礎課題布広. /** * SOFT * AppletTest01.java * 文字表示 */

HPE Insight Control サーバープロビジョニング 7.6 ビルドプランリファレンスガイド

Infrared Data Association Trademark and Brand Guidelines

マルチビットアップセット耐性及びシングルビットアップセット耐性を備えた

QPHY-SATA Serial ATA Serial Data オペレーターズ マニュアル

Codegate 2014 オンライン予選 Write Up Chrono (Logical) 300

SteelEye Protection Suite for Linux

HA8000V シリーズ. Service Pack for HA8000V (SPH) Version Readme. All Rights Reserved. Copyright (c) 2018 Hitachi, Ltd.

Interdomain Routing Security Workshop 21 BGP, 4 Bytes AS. Brocade Communications Systems, K.K.

暗い Lena トーンマッピング とは? 明るい Lena. 元の Lena. tone mapped. image. original. image. tone mapped. tone mapped image. image. original image. original.


勧告第 16 号 UN/LOCODE- 港および地名コード ECE/TRADE/227

グラフを表すデータ構造 JAVA での実装

Online Meetings with Zoom

TRACE32 Training... シンボルデータベース 内部シンボルデータベースの構造 シンボルデータベースの一般情報 シンボルブラウザ 選択したシンボルの詳細 ソースファイル内の検索...

Studies of Large-Scale Data Visualization: EXTRAWING and Visual Data Mining

製 品 ガ イ ド NetShield for NetWare V E R S I O N 4. 6

Ritsu-Mate Registration Manual (for Undergraduate Programs)

びんぼうでいいの による LCD シールド aitendo UL024TF のオシロスコープモード試験

Module mjlgsys. End Module. Imports MySql.Data.MySqlClient. Public Class frmgsys 'SQL 関連の定数宣言はフォーム間共通にしておく

BraindumpQuiz. Best exam materials provider - BraindumpQuiz! Choosing us, Benefit more!

プログラマ本体 I/F 回路図 -Programmer I/F Circuit Diagram-...2 FLASH2 内部 2 FLASHMATE5V1 内部 2

Preparing Information Design-Oriented. Posters. easy to. easy to. See! Understand! easy to. Convey!

船舶保安システムのセルフチェックリスト. Record No. Name of Ship 船名 flag 国籍 Name of Company 会社名 Date 点検日 Place 場所 Checked by 担当者名. MS-SELF-CHK-SHIP-j (2012.

FUJITSU Software SystemcastWizard Professional V5.1 L30 ユーザーズガイド B7FW Z0(00) 2014 年 8 月

Denso Lexus GS250 TCU Teardown

PGroonga 2. Make PostgreSQL rich full text search system backend!

OPTICAL TALK SET 光トークセット MODEL 415/430/450/450XL INSTRUCTION MANUAL 取扱説明書

4M Change Notice. NTC Lead type for Temperature Sensor : NXR Series

Saki is a Japanese high school student who/ has just started to study/ in the US.//

一覧にない機種は動作確認が未実施のものであり 正常動作しないという意味ではありません ACER 型番 ( 機種名 ) APPLE ASUS BRULE DELL. Windows Vista Home Premium

さまざまなニーズにお応えできるラインナップ!

Vivado Design Suite プロパティリファレンスガイド. UG912 (v2012.3) 2012 年 11 月 16 日

Transcription:

構造体 構造体のサイズ Marshal.SizeOf メソッド 整数型等型のサイズが定義されて居る構造体の場合 Marshal.SizeOf メソッドを使う事に依り型のサイズ ( バイト数 ) を取得する事が出来る 引数に値やオブジェクトを直接指定するか typeof や GetType で取得した型情報を渡す事に依り 其の型のサイズを取得する事が出来る 下記のプログラムを実行する事に依り Marshal.SizeOf メソッドに依るサイズの取得を検証して観る Imports System.Runtime.InteropServices Visual Basic Public Class MarshalSizeOf Private Sub lstvalue_selectedindexchanged(byval sender As System.Object, _ ByVal e As System.EventArgs) Handles lstvalue.selectedindexchanged Dim N As Integer = lstvalue.selectedindex Select Case N Case 0 MessageBox.Show(Marshal.SizeOf(16)) Case 1 MessageBox.Show(Marshal.SizeOf(3L)) Case 2 MessageBox.Show(Marshal.SizeOf(1.414F)) Case 3 MessageBox.Show(Marshal.SizeOf(3.14)) Case 5 MessageBox.Show(Marshal.SizeOf(16%)) -1-

Case 6 MessageBox.Show(Marshal.SizeOf(3&)) Case 7 MessageBox.Show(Marshal.SizeOf(1.414!)) Case 8 MessageBox.Show(Marshal.SizeOf(3.14#)) End Select End Sub Private Sub lsttype_selectedindexchanged(byval sender As System.Object, _ ByVal e As System.EventArgs) Handles lsttype.selectedindexchanged Dim N As Integer = lsttype.selectedindex Try Select Case N Case 0 MessageBox.Show(Marshal.SizeOf(GetType(Integer))) Case 1 MessageBox.Show(Marshal.SizeOf(GetType(Long))) Case 2 MessageBox.Show(Marshal.SizeOf(GetType(Single))) Case 3 MessageBox.Show(Marshal.SizeOf(GetType(Double))) Case 4 MessageBox.Show(Marshal.SizeOf(GetType(IntPtr))) Case 5 MessageBox.Show(Marshal.SizeOf(GetType(DateTime))) Case 6 MessageBox.Show(Marshal.SizeOf(GetType(DateTimeOffset))) Case 7 MessageBox.Show(Marshal.SizeOf(GetType(TimeSpan))) Case 8 MessageBox.Show(Marshal.SizeOf(GetType(String))) Case 9 MessageBox.Show(Marshal.SizeOf(GetType(Object))) End Select Catch ex As Exception MessageBox.Show(ex.Message, " 例外 ", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub End Class using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace MarshalSizeOf -2-

public partial class MarshalSizeOf : Form public MarshalSizeOf() InitializeComponent(); private void lstvalue_selectedindexchanged(object sender, EventArgs e) int n = lstvalue.selectedindex; switch (n) case 0: MessageBox.Show(Marshal.SizeOf(16).ToString()); break; case 1: MessageBox.Show(Marshal.SizeOf(3L).ToString()); break; case 2: MessageBox.Show(Marshal.SizeOf(1.414F).ToString()); break; case 3: MessageBox.Show(Marshal.SizeOf(3.14).ToString()); break; private void lsttype_selectedindexchanged(object sender, EventArgs e) int n = lsttype.selectedindex; try switch (n) case 0: MessageBox.Show(Marshal.SizeOf(typeof(int)).ToString()); break; case 1: MessageBox.Show(Marshal.SizeOf(typeof(long)).ToString()); break; case 2: MessageBox.Show(Marshal.SizeOf(typeof(float)).ToString()); break; case 3: MessageBox.Show(Marshal.SizeOf(typeof(double)).ToString()); break; case 4: MessageBox.Show(Marshal.SizeOf(typeof(IntPtr)).ToString()); break; case 5: MessageBox.Show(Marshal.SizeOf(typeof(DateTime)).ToString()); break; case 6: MessageBox.Show(Marshal.SizeOf(typeof(DateTimeOffset)).ToString()); break; case 7: MessageBox.Show(Marshal.SizeOf(typeof(TimeSpan)).ToString()); break; -3-

case 8: MessageBox.Show(Marshal.SizeOf(typeof(string)).ToString()); break; case 9: MessageBox.Show(Marshal.SizeOf(typeof(object)).ToString()); break; catch (Exception ex) MessageBox.Show(ex.Message, " 例外 ", MessageBoxButtons.OK, MessageBoxIcon.Error); 実行結果を下記に示す Visual Basic リテラル 16 4 16 4 3L 8 3L 8 1.414F 4 1.414F 4 3.14 8 3.14 8 データ型 Integer 4 int 4 Long 8 long 8 Single 4 float 4 Double 8 doublr 8 IntPtr 4 IntPtr 4 DateTime 例外 DateTime 例外 DateTimeOffset 例外 DateTimeOffset 例外 TimeSpan 8 TimeSpan 8 String 例外 string 例外 Object 例外 object 例外 IntPtr は実行環境に依ってサイズが変わる 32 ビット環境では 4 64 ビット環境では 8 が返される 上記の実行結果は 32 ビット環境での物で有る IntrPtr のサイズは IntPtr.Size プロパティに依っても取得する事が出来る IntPtr の値を利用して実行環境が 32 ビットか 64 ビットか判別する事も出来る 猶 何の様な型でも Marshal.SizeOf でサイズを取得する事が出来る訳では無い 例えば DateTime 等 アンマネージコードに渡す際のマーシャリング方法が定義されて居ない型の場合はサイズを取得出来ない 型のサイズが取得出来ない場合 Marshal.SizeOf メソッドは ArgumentException をスローする DateTime の他 DateTimeOffset string や object 等の参照型の物もサイズが取得出来ない 構造体のサイズ 構造体の場合では StructLayout 属性で LayoutKind.Sequential 又は LayoutKind.Explicit が指定されて居れば Marshal.SizeOf メソッドで構造体のサイズを取得する事が出来る -4-

Imports System Imports System.Runtime.InteropServices <StructLayout(LayoutKind.Sequential)> _ Structure Point Dim X As Integer Dim Y As Integer Visual Basic <StructLayout(LayoutKind.Explicit)> _ Structure ARGB <FieldOffset(0)> Dim Value As UInteger <FieldOffset(0)> Dim B As Byte <FieldOffset(1)> Dim G As Byte <FieldOffset(2)> Dim R As Byte <FieldOffset(3)> Dim A As Byte Class Sample Shared Sub Main() Console.WriteLine("SizeOf(0) = 1", GetType(Point), Marshal.SizeOf(GetType(Point))) Console.WriteLine("SizeOf(0) = 1", GetType(ARGB), Marshal.SizeOf(GetType(ARGB))) End Sub End Class [StructLayout(LayoutKind.Sequential)] struct Point int X; int Y; [StructLayout(LayoutKind.Explicit)] struct ARGB [FieldOffset(0)] uint Value; [FieldOffset(0)] byte B; [FieldOffset(1)] byte G; [FieldOffset(2)] byte R; [FieldOffset(3)] byte A; class Sample static void Main() -5-

Console.WriteLine("SizeOf(0) = 1", typeof(point), Marshal.SizeOf(typeof(Point))); Console.WriteLine("SizeOf(0) = 1", typeof(argb), Marshal.SizeOf(typeof(ARGB))); 実行結果を下記に示す SizeOf(Point) = 8 SizeOf(ARGB) = 4 亦.NET Framework 4.5.1 以降では Marshal.SizeOf<T> メソッドが用意されて居る typeof や GetType で型情報を指定する代わりに型パラメータ T を指定する丈で型のサイズが取得出来る様に成る Imports System Imports System.Runtime.InteropServices <StructLayout(LayoutKind.Sequential)> _ Structure Point Dim X As Integer Dim Y As Integer Visual Basic Class Sample Shared Sub Main() Console.WriteLine("SizeOf(Of Integer) = 0", Marshal.SizeOf(Of Integer)()) Console.WriteLine("SizeOf(Of Point) = 0", Marshal.SizeOf(Of Point)()) End Sub End Class [StructLayout(LayoutKind.Sequential)] struct Point int X; int Y; class Sample static void Main() Console.WriteLine("SizeOf<int> = 0", Marshal.SizeOf<int>()); Console.WriteLine("SizeOf<Point> = 0", Marshal.SizeOf<Point>()); -6-

実行結果を下記に示す SizeOf<int> = 8 SizeOf<Point> = 4 sizeof では sizeof に依っても型のサイズを取得する事が出来る sizeof は int や bool 等の組み込み型に対して使用する事が出来 定義済みのサイズを返す 亦 参照型のメンバを含まない構造体に対して使用する事も出来るが 其の場合は unsafe コンテキスト内で使用する必要が有る struct Point int X; int Y; class Sample static void Main() Console.WriteLine("sizeof(int) = 0", sizeof(int)); Console.WriteLine("sizeof(long) = 0", sizeof(long)); unsafe Console.WriteLine("sizeof(Point) = 0", sizeof(point)); 実行結果を下記に示す sizeof(int) = 4 sizeof(long) = 8 sizeof(point) = 8 Marshal.SizeOf と sizeof の違い 型に依っては Marshal.SizeOf と sizeof で異なる結果と成る場合が有る 例えば bool 型では次の様に異なる結果を返す Marshal.SizeOf と sizeof -7-

class Sample static void Main() Console.WriteLine("Marshal.SizeOf(typeof(bool)) = 0", Marshal.SizeOf(typeof(bool))); Console.WriteLine("sizeof(bool) = 0", sizeof(bool)); 実行結果を下記に示す Marshal.SizeOf(typeof(bool)) = 4 sizeof(bool) = 1 此れは Marshal.SizeOf がアンマネージ API 呼び出し時にマーシャリングされる際のサイズを返すのに対し sizeof はマネージドコード上でのサイズを返すと謂う違いが有る為で有る マネージドコードでは bool 型は 1 バイトとされている為 sizeof(bool) は 1 を返す 一方 アンマネージ API 呼び出しに際して bool 型は Win32 BOOL 型にマーシャリングされる 此の BOOL 型は typedef int BOOL;(=4 バイト符号付き整数 ) とされて居る為 Marshal.SizeOf(typeof(bool)) は 4 を返す アラインメント (Pack) サイズ (Size) Marshal.SizeOf 及び sizeof は構造体のアラインメントに応じた結果を返す 次の例では StructLayout 属性の Pack パラメータを指定して構造体のアラインメントを変更して居る Imports System.Runtime.InteropServices Visual Basic ' デフォルトのアラインメント (4 バイト ) Structure S1 Dim F1 As Byte Dim F2 As Integer ' 1 バイトアラインメント <StructLayout(LayoutKind.Sequential, Pack:=1)> _ Structure S2 Dim F1 As Byte ' 1 バイト目に配置される Dim F2 As Integer ' 2~5 バイト目に配置される ' 2 バイトアラインメント <StructLayout(LayoutKind.Sequential, Pack:=2)> _ Structure S3 Dim F1 As Byte ' 1 バイト目に配置される Dim F2 As Integer ' 3~6 バイト目に配置される ' 2 バイトアラインメント <StructLayout(LayoutKind.Sequential, Pack:=4)> _ -8-

Structure S4 Dim F1 As Byte ' 1 バイト目に配置される Dim F2 As Integer ' 5~8 バイト目に配置される Module alignment Sub Main() Console.WriteLine("Marshal.SizeOf(typeof(S1)) = 0", Marshal.SizeOf(GetType(S1))) Console.WriteLine("Marshal.SizeOf(typeof(S2)) = 0", Marshal.SizeOf(GetType(S2))) Console.WriteLine("Marshal.SizeOf(typeof(S3)) = 0", Marshal.SizeOf(GetType(S3))) Console.WriteLine("Marshal.SizeOf(typeof(S4)) = 0", Marshal.SizeOf(GetType(S4))) Console.ReadLine() End Sub End Module using System.Collections.Generic; using System.Text; namespace alignment // デフォルトのアラインメント (4 バイト ) struct S1 byte F1; int F2; // 1 バイトアラインメント [StructLayout(LayoutKind.Sequential, Pack = 1)] struct S2 byte F1; // 1 バイト目に配置される int F2; // 2~5 バイト目に配置される // 2 バイトアラインメント [StructLayout(LayoutKind.Sequential, Pack = 2)] struct S3 byte F1; // 1 バイト目に配置される int F2; // 3~6 バイト目に配置される // 4 バイトアラインメント [StructLayout(LayoutKind.Sequential, Pack = 4)] struct S4-9-

byte F1; // 1 バイト目に配置される int F2; // 5~8 バイト目に配置される class alignment static void Main(string[] args) Console.WriteLine("Marshal.SizeOf(typeof(S1)) = 0", Marshal.SizeOf(typeof(S1))); Console.WriteLine("Marshal.SizeOf(typeof(S2)) = 0", Marshal.SizeOf(typeof(S2))); Console.WriteLine("Marshal.SizeOf(typeof(S3)) = 0", Marshal.SizeOf(typeof(S3))); Console.WriteLine("Marshal.SizeOf(typeof(S4)) = 0", Marshal.SizeOf(typeof(S4))); Console.WriteLine(); unsafe Console.WriteLine("sizeof(typeof(S1)) = 0", sizeof(s1)); Console.WriteLine("sizeof(typeof(S2)) = 0", sizeof(s2)); Console.WriteLine("sizeof(typeof(S3)) = 0", sizeof(s3)); Console.WriteLine("sizeof(typeof(S4)) = 0", sizeof(s4)); Console.ReadLine(); 実行結果を下記に示す (Visual Basic は上半分丈 ) 同様に Size パラメータを指定して構造体のサイズを指定した場合も其れに応じた結果と成る Visual Basic Imports System.Runtime.InteropServices -10-

' 1 バイトアラインメント <StructLayout(LayoutKind.Sequential, Pack:=1)> _ Structure S1 Dim F1 As Byte Dim F2 As Integer ' 1 バイトアラインメントで 8 バイト分確保する <StructLayout(LayoutKind.Sequential, Pack:=2, Size:=8)> _ Structure S2 Dim F1 As Byte Dim F2 As Integer ' 1 バイトアラインメントで 16 バイト分確保する <StructLayout(LayoutKind.Sequential, Pack:=4, Size:=16)> _ Structure S3 Dim F1 As Byte Dim F2 As Integer Module alignment Sub Main() Console.WriteLine("Marshal.SizeOf(typeof(S1)) = 0", Marshal.SizeOf(GetType(S1))) Console.WriteLine("Marshal.SizeOf(typeof(S2)) = 0", Marshal.SizeOf(GetType(S2))) Console.WriteLine("Marshal.SizeOf(typeof(S3)) = 0", Marshal.SizeOf(GetType(S3))) Console.WriteLine() Console.ReadLine() End Sub End Module using System.Collections.Generic; using System.Text; namespace alignment // 1 バイトアラインメント [StructLayout(LayoutKind.Sequential, Pack = 1)] struct S1 byte F1; int F2; // 1 バイトアラインメントで 8 バイト分確保する [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 8)] struct S2-11-

byte F1; int F2; // 1 バイトアラインメントで 16 バイト分確保する [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 16)] struct S3 byte F1; int F2; class alignment static void Main(string[] args) Console.WriteLine("Marshal.SizeOf(typeof(S1)) = 0", Marshal.SizeOf(typeof(S1))); Console.WriteLine("Marshal.SizeOf(typeof(S2)) = 0", Marshal.SizeOf(typeof(S2))); Console.WriteLine("Marshal.SizeOf(typeof(S3)) = 0", Marshal.SizeOf(typeof(S3))); Console.WriteLine(); unsafe Console.WriteLine("sizeof(typeof(S1)) = 0", sizeof(s1)); Console.WriteLine("sizeof(typeof(S2)) = 0", sizeof(s2)); Console.WriteLine("sizeof(typeof(S3)) = 0", sizeof(s3)); Console.ReadLine(); 実行結果を下記に示す (Visual Basic は上半分丈 ) -12-