Question

1. Please answer a-d, there was a previous post with additional questions. Thank you in advance....

1. Please answer a-d, there was a previous post with additional questions. Thank you in advance.

Consider the following program and provide code and explanations where asked in the code comment after running the program in your system. Note that there are 4 parts (in bold) that need to be addressed.

#include <iostream>

#include <string.h>

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<payloadsize; 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;

    cout << "Header oject size " << sizeof (h1) << endl;      

    cout << "Header object h2 size "<< sizeof (h2) << endl;

    cout << "Header object pointer size " << sizeof (h3) << endl;

    // a. now allocate memory big enough to hold 10 instances of Header

    // b. Put 10 instances of Header in the allocated memory block, one after another without overwriting

            // The instances should have payload size 10, 20,..., 100 respectively

            // and they should have initial values 1,2,...10 respectively

    // c. now call getsummation() on each instance using a loop, output should be:

            // 10, 40, 90, ...., 1000 respectively

    Header* ptr = h3 + 100;

    cout <<"Printing pointer h3: " << h3 << endl;

    cout <<"Printing pointer ptr: " << ptr << endl;

    

    // d. explain the output you see in the following

    cout << "Difference " << (ptr - h3) << " objects" << endl;

    cout << "Difference " << ((char*) ptr - (char *)h3) << " bytes" << endl;

    

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <string.h>

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<payloadsize; 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;

cout << "Header oject size " << sizeof (h1) << endl;

cout << "Header object h2 size "<< sizeof (h2) << endl;

cout << "Header object pointer size " << sizeof (h3) << endl;

// a. now allocate memory big enough to hold 10 instances of Header
Header* H[10];

// b. Put 10 instances of Header in the allocated memory block, one after another without overwriting
for(int i=0;i<10;i++)
{
   H[i]=new Header(10*(i+1),i+1);
   }

// The instances should have payload size 10, 20,..., 100 respectively

// and they should have initial values 1,2,...10 respectively

// c. now call getsummation() on each instance using a loop, output should be:
cout<<endl;
for(int i=0;i<10;i++)
{
   cout<<H[i]->getsummation()<<" ";
   }
   cout<<endl<<endl;

// 10, 40, 90, ...., 1000 respectively

Header* ptr = h3 + 100;// Here we are moving our pointer till 100th address from address in h3

cout <<"Printing pointer h3: " << h3 << endl;

cout <<"Printing pointer ptr: " << ptr << endl;

  

// d. explain the output you see in the following

cout << "Difference " << (ptr - h3) << " objects" << endl;//Because we have added 100 in the address h3 and stores the new address in ptr and now we are subtracting them, that's why we are getting 100 in response

cout << "Difference " << ((char*) ptr - (char *)h3) << " bytes" << endl;//this is the difference in bytes, Because we are using address of 16 bytes that's why the difference in bytes are 16*100=1600

  

}

Add a comment
Know the answer?
Add Answer to:
1. Please answer a-d, there was a previous post with additional questions. Thank you in advance....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I've posted 3 classes after the instruction that were given at start You will implement and...

    I've posted 3 classes after the instruction that were given at start You will implement and test a PriorityQueue class, where the items of the priority queue are stored on a linked list. The material from Ch1 ~ 8 of the textbook can help you tremendously. You can get a lot of good information about implementing this assignment from chapter 8. There are couple notes about this assignment. 1. Using structure Node with a pointer point to Node structure to...

  • ) Using Linux or Unix command line interpreter, compile and run the programs in Figure 3.8,...

    ) Using Linux or Unix command line interpreter, compile and run the programs in Figure 3.8, Figure 3.30. DO NOT compile and ron these programs on Windows Write the 3.16, Figare 317 and Figure 3 programs in Notepadt+, for example, then compile and run them at the command pr apt. Provide screenshots of your programs compilation, execution, and the results. 144 6 7 8 9 ry-maps a shared-memory object of the ws writing to the object. The flag shared-memory object...

  • Please show all steps for #15 and #16 neatly. Thank you so much in advance! Will...

    Please show all steps for #15 and #16 neatly. Thank you so much in advance! Will rate thumbs up for correct answer ! ? 15. Determine the output of th ollowing Ct+ code? int num346, sum 0; do wA sum-sum + num % 10; num = num / 10; cout < sum endl; while (num> e); rmine the output produced by the following code fragment? int i 11; i 1 i 4)10 9 cout << i << " ". i+t;...

  • Malloc function For the prelab assignment and the lab next week use malloc function to allocate...

    Malloc function For the prelab assignment and the lab next week use malloc function to allocate space (to store the string) instead of creating fixed size character array. malloc function allows user to allocate memory (instead of compiler doing it by default) and this gives more control to the user and efficient allocation of the memory space. Example int *ptr ptr=malloc(sizeof(int)*10); In the example above integer pointer ptr is allocated a space of 10 blocks this is same as creating...

  • C++ What is the output of the following program? Using a pointer diagram show the evolution...

    C++ What is the output of the following program? Using a pointer diagram show the evolution of all data objects in memory. Assume the code is embedded in a correct and complete program. 3.(8 pts) What is the output of the following program? Using a pointer diagram show the evolution of all data objects in memory. Assume the code is embedded in a correct and complete program. int array size 4, *a i a new int [array size] int *p...

  • I have some questions about pointers/pointer arithmetic in C++ 1) Pointers and Pointer Arithmetic a.)       What...

    I have some questions about pointers/pointer arithmetic in C++ 1) Pointers and Pointer Arithmetic a.)       What is the difference between statically allocated arrays and dynamically allocated arrays (be brief) b.)      Which of the following pointers can be used for a dynamically allocated array? (Circle) char ptr; char * ptr; char ptr *; char ptr[]; char [] ptr; c.)       Show now, using that pointer, how to dynamically allocate array of characters of size 10: (don't use malloc) d.)      Which of the...

  • a7q3.cc File #include <iostream> #include <cstring> #include "ArrayList.h" using namespace std; // Algorithm copy(s) // Pre:...

    a7q3.cc File #include <iostream> #include <cstring> #include "ArrayList.h" using namespace std; // Algorithm copy(s) // Pre: s :: refToChar // Post: memory allocated on heap to store a copy // Return: reference to new string char *copy(char *s) { char *temp = new char[strlen(s)+1]; strcpy(temp,s); return temp; } void test_ListOperations(){    cout << "testing createList" << endl;    List *myList = createList(10);    if (myList == NULL){ cout << "createList failed" << endl; return; } else{ cout << "createList succeeded"...

  • The second picture that I attached is the input and output. I already did the code...

    The second picture that I attached is the input and output. I already did the code but I also have to add the partition number assigned to the job (if the job was allocated). Can you please do that for me? I copied and pasted my code below. #include <iostream> #include <string.h> #include <stdio.h> using std::cout; using std::cin; using std::endl; unsigned int memorySize; // Function prototypes unsigned int FirstFit(struct Partition *, int, struct Job *, int); unsigned int BestFit(struct Partition...

  • Please help me finish my code. Before the final pause in the main function, create an...

    Please help me finish my code. Before the final pause in the main function, create an ArrayList of another type such as double, char, short, bool, float, etc. Your choice. Add five data items to your array as we did for the int and string array lists. Print them out with a for loop as we did before. Print out the count and capacity of your new array list. Source.cpp #include #include #include #include "ArrayList.h" using namespace std; /// Entry...

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT