|
(null) 님이 쓰신 글 :
: Constructor를 쓰면 간단하지만, 그것을 사용하지 않는 방법을 물어보신거 같네요.
:
: 그 다음에 char 갯수만큼 memset으로 20으로 초기화 시키고...
:
: 포인터를 char 갯수만큼 이동시켜서 0으로 초기화하면 될거예요.
:
: memset( &m_sDummyData, 20, 26 );
: memset( (&m_sDummyData)+26, 0, sizeof(int)*5 );
:
위와 같은 초기화는 data alignment 문제로 인하여 제대로 초기화가 안될 수 있습니다.
컴파일러에 따라서, 그리고 컴파일러의 옵션에 따라서 구조체의 크기가 달라질 수 있습니다.
그냥, 아래 처럼 초기화 하는게 잘못된 초기화를 방지하겠죠..
struct SDummyData // Dummy Data
{
public:
// ...
SDummyData();
};
SDummyData::SDummyData()
: nCoilThick(0), nCoilWidth(0), nCoilLength(0)
, nCoilWeight(0), nUseCount(0)
{
memset(szStatusCode, 0x20, sizeof szStatusCode);
memset(szCCoilNo, 0x20, sizeof szCCoilNo);
memset(szDecisionKind, 0x20, sizeof szDecisionKind);
}
참고)
[MSDN Library - July 2004, C++ Language Reference align]
Writing applications that use the latest processor instructions introduces some new constraints and issues. In particular, many new instructions require that data must be aligned to 16-byte boundaries. Additionally, by aligning frequently used data to the cache line size of a specific processor, you improve cache performance.
[C++ Builder 6 Help, Data Alignment]
The Data Alignment options let you choose how the compiler aligns data in stored memory.
Word, double-word, and quadword alignment force items to be aligned on memory addresses that are a multiple of the type chosen. Extra bytes are inserted in structures to ensure that members align correctly.
Default = Double Word (32-bit)
- Byte aligns to 8-bit boundaries.
- Word aligns to 16-bit boundaries.
- Double Word aligns to 32-bit boundaries.
Data with type sizes of less than 4 bytes are aligned on their type size.
- Quad Word aligns to 64-bit boundaries.
Data with type sizes of less than 8 bytes are aligned on their type size.
|