这里没有严重错误。一个不必要的演员阵容,但看起来不错。
void Memory::writeLong(const long address, const long value)
{
if (address < start || address + sizeof(long) > start + memorySize) {
cout << "Memory::readLong out of range" << endl;
throw "Memory class range error";
}
uint8_t *valRep = (uint8_t *) &value;
for (int i = 0; i < sizeof(long); i++)
{
contents[address + i] = *((uint8_t *)(valRep + i));// don't need cast
}
}
阅读中有几处令人讨厌的地方:
long Memory::readLong(const long address)
{
long retVal = 0;
if (address < start || address + sizeof(long) > start + memorySize) {
cout << "Memory::readLong out of range" << endl;
throw "Memory class range error";
}
uint8_t in[8]; // should be uint8_t in[sizeof(retVal)];
// why sizeof(retVal) and not sizeof(long)
// someone could cut and paste to make the different typed
// versions and change only retVal. Might as well lock them
for (int i = 0; i < sizeof(long); i++) //nag nag, signed unsigned mismatch
{
try {
in[i] =(uint8_t) contents.at(address + i) << (i * 8);
// in[i] is 8 bits. << (i * 8) ensures all but the first byte are shifted
// out of byte range
}
catch (const out_of_range& err)
{
contents[address] = 0;
}
}
memcpy(&retVal, in, 8); // overruns retVal if retval is 32 bits. Use
// sizeof(retVal) again.
return retVal;
}
所以这不可能工作或生成 OP 报告的结果。我的猜测是 OP 有一些几乎可以工作的东西,然后开始四处寻找修复它并使事情变得更糟。
我会怎么做:
编辑:原来用工会的类型双关语是非法的。糟透了,因为我认为它比指针版本更安全、更容易阅读。指针版本不非法的唯一原因可能是大量的合法案例用于转换为字节。
我还将 try/catch 块移到了重组循环之外,因为一旦您读取外部内存,恢复过程就非常顺利。
#include <iostream>
#include <map>
std::map<long, uint8_t> contents;
void writeLong(const size_t address, const long value)
{
/* not needed for test case
if (address < start || address + sizeof(long) > start + memorySize) {
cout << "Memory::readLong out of range" << endl;
throw "Memory class range error";
}
*/
uint8_t *valRep = (uint8_t *) &value;
for (size_t i = 0; i < sizeof(long); i++)
{
contents[address + i] = *(valRep + i);
}
}
long readLong(const size_t address)
{
// Verbotten!
// union
// {
// long val;
// uint8_t arr[sizeof(val)];
// }retVal; // Ahhhrrrrrr! Here be endian nightmare!
long retVal;
uint8_t *valRep = (uint8_t *) &retVal;
// uglier, but legal. Consider using the no-punning version below
/* not needed for test case
if (address < start || address + sizeof(long) > start + memorySize) {
cout << "Memory::readLong out of range" << endl;
throw "Memory class range error";
}
*/
// uint8_t in[sizeof(long)]; replaced by evil union abuse above
try
{
for (size_t i = 0; i < sizeof(long); i++)
{
// Not legal
// retVal.arr[i] = contents.at(address + i) ;
*(valRep + i) = contents.at(address + i);
}
}
catch (const std::out_of_range& err)
{
retVal = 0;
}
// memcpy(&retVal, in, sizeof(long)); replaced by evil union abuse above
// Not legal
// return retVal.val;
return retVal;
}
void test(long val)
{
writeLong(0, val);
std::cout << "In " << val << " out " << readLong(0) << std::endl;
}
int main()
{
test(-4);
test(-8);
test(-5);
test(-1);
test(-1224);
test(0);
test(1);
test(0x7fffffff);
test(0x80000000);
}
输出:
In -4 out -4
In -8 out -8
In -5 out -5
In -1 out -1
In -1224 out -1224
In 0 out 0
In 1 out 1
In 2147483647 out 2147483647
In -2147483648 out -2147483648
我会想念我的朋友类型双关语,但也有无双关语的版本。
void writeLong(size_t address, long value)
{
// not needed for test case
// if (address < start || address + sizeof(long) > start + memorySize) {
// cout << "Memory::readLong out of range" << endl;
// throw "Memory class range error";
// }
for (size_t i = 0; i < sizeof(long); i++)
{ // note this only preserves the proper memory layout for little endian systems
contents[address + i] = (uint8_t)(value &0xFF);
value >>= 8;
}
}
long readLong(size_t address)
{
long retVal;
// not needed for test case
// if (address < start || address + sizeof(long) > start + memorySize) {
// cout << "Memory::readLong out of range" << endl;
// throw "Memory class range error";
// }
try
{
address += sizeof(long) - 1;
retVal = contents.at(address--);
for (size_t i = 0; i < sizeof(long) - 1; i++)
{ // this only works if the little endian memory layout is preserved
retVal <<= 8;
retVal += contents.at(address--);
}
}
catch (const std::out_of_range& err)
{
retVal = 0;
}
return retVal;
}