我写的东西像Java,和我有指针的问题( - - ) 我有一个结构:C++不可能的指针行为
struct _lnHeader32
{ unsigned char signature[2]; //LN
unsigned char architecture;
unsigned int length; //Without _lnHeader
unsigned int lnHeaderLength;
unsigned char permissions;
unsigned char typeOfExecutable;
unsigned int flowSegment;
unsigned int dataSegment;
unsigned int loaderSegment;
unsigned int cleanerSegment;
unsigned int errorSegment;
unsigned int exportTable;
unsigned int importTable;
unsigned int authenticationTable; //Encrypt it with GPG.
unsigned int loaderTable;
};
我打开可执行文件,它是在小尾数,由使用std :: fstream的:
lnFile.open(argv[1], std::fstream::in | std::fstream::binary);
if (false == lnFile.is_open())
throw (unableToOpen);
lnSize = getFileSize(lnFile);
lnImage = new (std::nothrow) unsigned char [lnSize];
if (0 == lnImage)
throw (noMem);
lnFile.read(reinterpret_cast<char*>(lnImage), lnSize); //#1 Possible mistake?
if (!lnFile)
throw (unableToRead);
lnFile.close();
然后我点_lnHeader32 *来分配lnImage:
lnHeader32 = reinterpret_cast<_lnHeader32*>(lnImage);
在最后我通过两种方法打印整个结构:
//Method 1
std::cout << reinterpret_cast<unsigned int*>(lnImage) << "\n";
std::cout << reinterpret_cast<unsigned int*>(lnImage+2) << "\n";
std::cout << reinterpret_cast<unsigned int*>(lnImage+3) << "\n";
std::cout << reinterpret_cast<unsigned int*>(lnImage+7) << "\n";
std::cout << reinterpret_cast<unsigned int*>(lnImage+11) << "\n";
std::cout << reinterpret_cast<unsigned int*>(lnImage+12) << "\n\n";
//Method 2
std::cout << reinterpret_cast<unsigned int*>(&lnHeader32->signature) << "\n";
std::cout << reinterpret_cast<unsigned int*>(&lnHeader32->architecture) << "\n";
std::cout << reinterpret_cast<unsigned int*>(&lnHeader32->length) << "\n";
std::cout << reinterpret_cast<unsigned int*>(&lnHeader32->lnHeaderLength) << "\n";
std::cout << reinterpret_cast<unsigned int*>(&lnHeader32->permissions) << "\n";
std::cout << reinterpret_cast<unsigned int*>(&lnHeader32->typeOfExecutable)
<< "\n\n";
它给了我像输出:
0xe8b260
0xe8b262
0xe8b263 <---
0xe8b267
0xe8b26b
0xe8b26c
0xe8b260
0xe8b262
0xe8b264 <--- Should be 0xe8b263 | Here starts problem
0xe8b268
0xe8b26c
0xe8b26d
它通过使用第一种方法打印以及lnHeader32的领域,但我更喜欢使用第二种方法。我计算了一切几次。为什么会发生? 可执行文件是由perl编译器生成的,对它有什么影响?
https://en.wikipedia.org/wiki/Data_padding –
使用nothrow new,只检查结果为null,然后抛出是一个真正的WTF? –
现在一切都很好。谢谢你,Oli Charlesworth。 – SysOp