Question
Please answer a-d of the following, there will be a follow up post asking additional questions! Thank you in advance. Consider the following program and
Please answer a-d of the following, there will be a follow up post asking additional questions! Thank you in advance.
Consider the following program and provide explanations where asked in the code comment after running the program in your system. Note that there are 4 places where an explanation is needed. For explanation "a" (i.e., the first commented line), does the Header size equal the sum of individual data types (i.e., a char, an int and a pointer)? Try to explain this with something called packing.
#include
#include
using namespace std;
class Header{
private:
char used;
int payloadsize;
char* data;
public:
Header (){
used = 0, payloadsize = -1, data = NULL;
}
Header (int ps, char initvalue = 0){
used = 0;
payloadsize = ps;
data = new char [payloadsize];
memset (data, initvalue, payloadsize);
}
int getsummation (){
int sum = 0;
for (int i=0; i sum += data [i]; } return sum; } }; int main (){ Header h1; Header h2 (10); Header* h3 = new Header (20); cout << "Header type size " << sizeof (Header) << endl; // a. explain cout << "Header oject size " << sizeof (h1) << endl; // b. explain why cout << "Header object h2 size "<< sizeof (h2) << endl; // c. explain why cout << "Header object pointer size " << sizeof (h3) << endl; // d. why Header* ptr = h3 + 100; cout <<"Printing pointer h3: " << h3 << endl; cout <<"Printing pointer ptr: " << ptr << endl; cout << "Difference " << (ptr - h3) << " objects" << endl; cout << "Difference " << ((char*) ptr - (char *)h3) << " bytes" << endl; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started