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:
x:
x: aa aa aa
size : padding :
My Output:
data data A:
x:
x: aa aa aa
size : padding :
The example of the output is as above, but the code below fails to check padding it only outputs
Align.h DO NOT MODIFY :
#ifndef ALIGNH
#define ALIGNH
class Align
public:
struct Info
int NumTotalBytes;
int NumBytesOfPadding;
;
static Info PrintMEvoid p int StructSize, char s;
;
#endif
End of File
Align.cpp:
#include "Align.h
PrintME
Write your alignment printing function here
Align::Info Align::PrintMEvoid pData int StructSize, char pString
Use Trace::out to display the data layout and padding
Mimic the KeenanSampleOutputDebug.txt
char pDataBytes reinterpretcastpData;
Trace::outdata s: pString;
int paddingBytes ;
int totalBytes ;
char pMember pDataBytes;
while pMember pDataBytes StructSize
if totalBytes
Trace::out
xx: totalBytes;
for int i ; i sizeofpMember; i
Trace::outx staticcastpMember i;
totalBytes;
int memberSize sizeofpMember;
int memberAlignment alignofdecltypepMember;
if memberSize sizeofdouble memberSize sizeoflong long
memberAlignment ;
int padding totalBytes memberAlignment : memberAlignment totalBytes memberAlignment;
paddingBytes padding;
for int i ; i paddingBytes; i
Trace::out; Output padding bytes as zeros
totalBytes;
pMember memberSize;
Info info;
info.NumBytesOfPadding paddingBytes;
info.NumTotalBytes totalBytes;
Trace::out
;
Trace::outsize : d padding : d
totalBytes, paddingBytes;
return info;
End of File
AlignData.h:
#ifndef ALIGNDATAH
#define ALIGNDATAH
Add default constructor to each structure
Initialize all variables to
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 : a a
int a;
char a;
;
struct B
B : b b bfalse b
float b;
float b;
bool b;
float b;
;
struct C
C : c c c
char c;
double c;
char c;
;
struct D
D : d d d d d
A d;
double d;
B d;
char d;
C d;
;
struct E
E : a c aa b
A a;
C c;
char aa;
B b;
;
#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;
TESTWITHTEARDOWNPrintAlignment, TestConfig::ALL
Align::Info Info;
Create a tmp buffer
const unsigned int BUFFSIZE ;
pBuff new charBUFFSIZE;
assertpBuff nullptr;
memsetpBuffxAA, BUFFSIZE;
AZULPLACEMENTNEWBEGIN
#undef new
E pE newpBuff E;
AZULPLACEMENTNEWEND
Info Align::PrintMEpE sizeofE "data E;
CHECKInfoNumTotalBytes ;
CHECKInfoNumBytesOfPadding ;
delete pBuff;
pBuff nullptr;
TESTEND
TESTTEARDOWNPrintAlignment
delete pBuff;
End of File
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
