有没有办法让两个不同大小的变量共享一个内存地址而不用将它们声明为指针?
Is there a way to have two variables of different sizes share a memory address without declaring them as pointers?
我正在开发一个个人项目,一个模拟器。假设有两个寄存器 H 和 L,每个一个字节长。所以我们可以用一个字节写入 H 或 L。但是,某些指令要求您将两个字节写入 H 和 L,或 HL。第一个字节转到 H,第二个字节转到 L。根据我的实现方式,有些东西很难实现。
所以我的想法是让 HL 成为一个两字节的单词。但是,仍然存在H和L变量,它们分别与HL的第一个字节和HL的第二个字节共享相同的地址。
我可以做指针,但是我真的不想将我所有的寄存器都声明为指针。
我在想的一件事是联合,就像这样:
union {
BYTE H;
WORD HL;
}
但是我不知道如何将 L 作为第二个字节放在那里。
有什么想法吗?
像这样的工会怎么样?
union {
BYTE asBytes[2];
WORD asWord;
}
然后您可以通过 asBytes[0]
访问 H,通过 asBytes[1]
访问 L,或通过 asWord
.
访问 HL
你可以这样做:
union
{
struct
{
BYTE L, H;
} b;
WORD HL;
} u;
一些编译器允许您这样做,尽管它是非标准的:
union
{
struct
{
BYTE L, H;
};
WORD HL;
} u;
Is there a way to have two variables of different sizes share a memory
address without declaring them as pointers?
我相信您可以实现您想要的 'behavior',无需指针、共享内存和类型双关。
考虑以下 class。请注意,用户定义类型的行为是在函数中定义的。
class ML // msByte and lsByte
{
private:
BYTE m_bytes[2]; // this data will be 2 * sizeof(BYTE)
public:
ML() = default; // does nothing. ?or do you need to initialize these?
~ML() = default; // does nothing unless you want it to
// extract one or the other byte - consider
BYTE lsByte() { return m_bytes[0]; } // index 0/1
BYTE msByte() { return m_bytes[1]; } // fix based on endianess
// extract the two bytes by summing,
// I prefer bit shifting and or'ing
WORD ML() { return ( add m_bytes[0] and (256*m_bytes[1]) into WORD }
// or maybe ( m_bytes[0] | (m_bytes[1] << 8)); )// fix based on endianess
void ML( BYTE ls, BYTE ms)
{ // fix index based on endianess
m_bytes[0] = ls;
m_bytes[1] = ms;
}
void ML( WORD w)
{ // fix shifting based on endianess
ML ( (w & 0xff), ((w >> 8) & 0xff) ) // invoke ML (BYTE, BYTE)
// lsbyte msbyte
}
};
我正在开发一个个人项目,一个模拟器。假设有两个寄存器 H 和 L,每个一个字节长。所以我们可以用一个字节写入 H 或 L。但是,某些指令要求您将两个字节写入 H 和 L,或 HL。第一个字节转到 H,第二个字节转到 L。根据我的实现方式,有些东西很难实现。
所以我的想法是让 HL 成为一个两字节的单词。但是,仍然存在H和L变量,它们分别与HL的第一个字节和HL的第二个字节共享相同的地址。
我可以做指针,但是我真的不想将我所有的寄存器都声明为指针。
我在想的一件事是联合,就像这样:
union {
BYTE H;
WORD HL;
}
但是我不知道如何将 L 作为第二个字节放在那里。
有什么想法吗?
像这样的工会怎么样?
union {
BYTE asBytes[2];
WORD asWord;
}
然后您可以通过 asBytes[0]
访问 H,通过 asBytes[1]
访问 L,或通过 asWord
.
你可以这样做:
union
{
struct
{
BYTE L, H;
} b;
WORD HL;
} u;
一些编译器允许您这样做,尽管它是非标准的:
union
{
struct
{
BYTE L, H;
};
WORD HL;
} u;
Is there a way to have two variables of different sizes share a memory address without declaring them as pointers?
我相信您可以实现您想要的 'behavior',无需指针、共享内存和类型双关。
考虑以下 class。请注意,用户定义类型的行为是在函数中定义的。
class ML // msByte and lsByte
{
private:
BYTE m_bytes[2]; // this data will be 2 * sizeof(BYTE)
public:
ML() = default; // does nothing. ?or do you need to initialize these?
~ML() = default; // does nothing unless you want it to
// extract one or the other byte - consider
BYTE lsByte() { return m_bytes[0]; } // index 0/1
BYTE msByte() { return m_bytes[1]; } // fix based on endianess
// extract the two bytes by summing,
// I prefer bit shifting and or'ing
WORD ML() { return ( add m_bytes[0] and (256*m_bytes[1]) into WORD }
// or maybe ( m_bytes[0] | (m_bytes[1] << 8)); )// fix based on endianess
void ML( BYTE ls, BYTE ms)
{ // fix index based on endianess
m_bytes[0] = ls;
m_bytes[1] = ms;
}
void ML( WORD w)
{ // fix shifting based on endianess
ML ( (w & 0xff), ((w >> 8) & 0xff) ) // invoke ML (BYTE, BYTE)
// lsbyte msbyte
}
};