構造体などのアラインメント (16bit・32bit・64bit)をソースコードの途中で変更
pushとpop機能でネスト可能
#pragma pack(push,1)によって、アラインメントを1バイトに変更
cat pragma_pack_test.cpp
// pragma_pack_test.cpp
// console application
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
#include "pragma_pack_test.h"
typedef struct s1_tag {
char ch1;
int i1;
long l1;
} s1;
#pragma pack(push,1)
typedef struct s2_tag {
char ch1;
int i1;
long l1;
} s2;
#pragma pack(pop)
int main(array<System::String ^> ^args){
s1 t1;
s2 t2;
Console::WriteLine("size of t1={0}",sizeof(s1));
Console::WriteLine("size of t2={0}",sizeof(s2));
return EXIT_SUCCESS;
}
cat makefile
# makefile for pragma_pack_test
# console application
.PHONY: all test mmm clean
.SUFFIXES: .exe .obj .txt .resources .dll
TARGET=pragma_pack_test
CULTUREDIRS=
DLLS=
all: $(TARGET).exe $(DLLS)
LOPT=
OBJS=$(TARGET).obj AssemblyInfo.obj
$(TARGET).obj: $(TARGET).cpp
AssemblyInfo.obj: AssemblyInfo.cpp
meta: $(TARGET).exe
ILDasm /out:$@ /metadata $<
$(TARGET).exe: $(OBJS)
link $(LOPT) $^
.cpp.obj:
#cl -c -Wall -Od -clr:safe $<
cl -c -Wall -Od -clr $<
test: $(TARGET).exe
./$(TARGET).exe
clean:
@-rm $(TARGET).exe *.obj *~ $(TARGET).exe.manifest 1>/dev/null 2>&1
@-rm *.resources 1>/dev/null 2>&1
@-rm -fr $(CULTUREDIRS)
mmm:
cat $(TARGET).cpp
cat AssemblyInfo.cpp
@echo
cat makefile
html:
cat /dev/clipboard | code2html.exe | unix2dos
install: $(TARGET).exe
cp $(TARGET).exe ~/bin
cp $(TARGET).exe.manifest ~/bin
$ make -B test
#cl -c -Wall -Od -clr:safe pragma_pack_test.cpp
cl -c -Wall -Od -clr pragma_pack_test.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.42
for Microsoft (R) .NET Framework version 2.00.50727.312
Copyright (C) Microsoft Corporation. All rights reserved.
pragma_pack_test.cpp
pragma_pack_test.cpp(12) : warning C4820: 's1_tag' : '3' bytes padding added aft
er data member 's1_tag::ch1'
pragma_pack_test.cpp(23) : warning C4100: 'args' : unreferenced formal parameter
pragma_pack_test.cpp(25) : warning C4101: 't2' : unreferenced local variable
pragma_pack_test.cpp(24) : warning C4101: 't1' : unreferenced local variable
#cl -c -Wall -Od -clr:safe AssemblyInfo.cpp
cl -c -Wall -Od -clr AssemblyInfo.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.42
for Microsoft (R) .NET Framework version 2.00.50727.312
Copyright (C) Microsoft Corporation. All rights reserved.
AssemblyInfo.cpp
link pragma_pack_test.obj AssemblyInfo.obj
Microsoft (R) Incremental Linker Version 8.00.50727.42
Copyright (C) Microsoft Corporation. All rights reserved.
./pragma_pack_test.exe
size of t1=12
size of t2=9