Question: The code below is an example question for C + + Data Align. Good Output : data data A: 0 x 0 0 : 0

The code below is an example question for C++ Data Align.
Good Output :
data data A:
0x00: 00000000
0x04: 00 aa aa aa
--------------------------
size : 8 padding : 3
My Output:
data data A:
0x00: 00000000
0x04: 00 aa aa aa
--------------------------
size : 8 padding : 0
The example of the output is as above, but the code below fails to check padding (it only outputs 0).
Align.h (DO NOT MODIFY) :
#ifndef ALIGN_H
#define ALIGN_H
class Align
{
public:
struct Info
{
int NumTotalBytes;
int NumBytesOfPadding;
};
static Info PrintME(void *p, int StructSize, char *s);
};
#endif
//--- End of File ---
Align.cpp:
#include "Align.h"
//-------------------------------------------------------------
// PrintME()
//
// Write your alignment printing function here
//-------------------------------------------------------------
Align::Info Align::PrintME(void *pData, int StructSize, char *pString)
{
// Use Trace::out2(...) to display the data layout and padding
// Mimic the KeenanSampleOutput_Debug.txt
char* pDataBytes = reinterpret_cast(pData);
Trace::out2("data %s: ", pString);
int paddingBytes =0;
int totalBytes =0;
char* pMember = pDataBytes;
while (pMember < pDataBytes + StructSize)
{
if (totalBytes %4==0)
{
Trace::out2("
0x%02x: ", totalBytes);
}
for (int i =0; i < sizeof(*pMember); ++i)
{
Trace::out2("%02x ", static_cast(*(pMember + i)));
++totalBytes;
}
int memberSize = sizeof(*pMember);
int memberAlignment = alignof(decltype(*pMember));
if (memberSize == sizeof(double)|| memberSize == sizeof(long long)){
memberAlignment =8;
}
int padding =(totalBytes % memberAlignment ==0)?0 : memberAlignment -(totalBytes % memberAlignment);
paddingBytes += padding;
for (int i =0; i < paddingBytes; ++i)
{
Trace::out2("00"); // Output padding bytes as zeros
++totalBytes;
}
pMember += memberSize;
}
Info info;
info.NumBytesOfPadding = paddingBytes;
info.NumTotalBytes = totalBytes;
Trace::out2("
--------------------------
");
Trace::out2("size : %d padding : %d
", totalBytes, paddingBytes);
return info;
}
//--- End of File ---
AlignData.h:
#ifndef ALIGN_DATA_H
#define ALIGN_DATA_H
//-----------------------------------------------------------
// Add default constructor to each structure
// Initialize all variables to 0
// Do NOT rearrange any data layout...
// These are the classes used with Align::PrintMe();
//-----------------------------------------------------------
struct A
{
// Hint add the default constructor and set variables to zero
A() : a0(0), a1(0){}
int a0;
char a1;
};
struct B
{
B() : b0(0), b1(0), b2(false), b3(0){}
float b0;
float b1;
bool b2;
float b3;
};
struct C
{
C() : c0(0), c1(0), c2(0){}
char c0;
double c1;
char c2;
};
struct D
{
D() : d0(), d1(0), d2(), d3(0), d4(){}
A d0;
double d1;
B d2;
char d3;
C d4;
};
struct E
{
E() : a0(), c0(), aa(0), b0(){}
A a0;
C c0;
char aa;
B b0;
};
#endif
//--- End of File ---
AlignData.cpp:
#include "AlignData.h"
// Insert code
//--- End of File ---
Please modify 'Align.cpp' to calculate padding.
Must be able to pass the test below.
#include "_UnitTestConfiguration.h"
#include "AlignData.h"
#include "Align.h"
//----------------------------------
//--- DO NOT MODIFY FILE ---
//----------------------------------
static char *pBuff = nullptr;
TEST_WITH_TEARDOWN(Print_Alignment, TestConfig::ALL)
{
Align::Info Info;
// Create a tmp buffer
const unsigned int BUFF_SIZE =256;
pBuff = new char[BUFF_SIZE];
assert(pBuff != nullptr);
memset(pBuff,0xAA, BUFF_SIZE);
AZUL_PLACEMENT_NEW_BEGIN
#undef new
E* pE = new(pBuff) E();
AZUL_PLACEMENT_NEW_END
Info = Align::PrintME(pE, sizeof(E), "data E");
CHECK(Info.NumTotalBytes ==56);
CHECK(Info.NumBytesOfPadding ==27);
delete[] pBuff;
pBuff = nullptr;
} TEST_END
TEST_TEARDOWN(Print_Alignment)
{
delete[] pBuff;
}
//--- End of File --

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!