Visual Studio 2008 で C++ を使用しています。次のような構造があるとします。
struct StructOfInts
{
int a;
int b;
int c;
};
これは、次のように読み書きすることを意図しています。
void Read( std::istream& is, StructOfInts& myStruct )
{
is.read( (char*)&myStruct.a, sizeof myStruct.a );
is.read( (char*)&myStruct.b, sizeof myStruct.b );
is.read( (char*)&myStruct.c, sizeof myStruct.c );
}
void Write( std::ostream& os, StructOfInts& myStuct )
{
os.write( (char*)&myStruct, sizeof myStruct );
}
上記のコードは、ファイルの読み取りまたは書き込み時に何らかのメモリ破損を引き起こす可能性がありますか? メモリの破損とは、誤った値が読み込まれていることを意味します。読み込まれている -1.#QNB 値のソースを特定しようとしていますが、これが原因ではないかと考えています。また、pragma pack を使って構造体を pack すると違いはありますか?