Question

5. What’s wrong with the code below? Feel free to compile and run the program to...

5. What’s wrong with the code below? Feel free to compile and run the program to investigate. [6]



#include <iostream>

using namespace std;

class HW1

{

public:

   char *word;

   HW1() { }

   ~HW1()

   {

delete [] word;

   }

};

void foo(HW1 a)

{

//some operation here

}

int main()

{

HW1 a;

a.word = new char[80];

strcpy(a.word, "abc");

foo(a);

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <cstring>
using namespace std;
class HW1
{
    public:
       char *word;
       HW1() { }
       ~HW1()
       {}
};

void foo(HW1 a)
{
    //some operation here
}

int main()
{
    HW1 a;
    // Memory for a.word is allocated in main()
    a.word = new char[80];
    strcpy(a.word, "abc");
    foo(a);
    // So, you should fee memory in main() method only
    delete[] a.word;
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
5. What’s wrong with the code below? Feel free to compile and run the program to...
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
  • 10. Does the following code compile? Does it run? Is there any problem with the code?...

    10. Does the following code compile? Does it run? Is there any problem with the code? If yes, how do you fix it? 1. #include < iostream > 2. using namespace std; 4 class Computer int Id; 7. public: Computer(int id) this -Id- id; ) void process() cout << "Computer::process()"; 9 10. H; 12. class Employee 13. 14. 15. public: 16 Computer* c; Employee )cnew Computer (123); 17.Employee() C 18. 19. 20 21. 22. ^; 23. 24. int main(){ 25....

  • Need help with a multiple inheritance work Question Compile and run the file. If you find...

    Need help with a multiple inheritance work Question Compile and run the file. If you find any compilation errors,explain their reason as comments in the code. Then fix the errors such that the program compiles and runs. Explain the motivation of your modifications in the code and their effects on the execution inheritance.cpp file #include<iostream> using namespace std; class A { int x; public: void setX(int i) {x = i;} void print() { cout << x; } }; class B:...

  • Any help will be appreciated please... Compile and run the file. If you find any compilation...

    Any help will be appreciated please... Compile and run the file. If you find any compilation errors,explain their reason as comments in the code. Then fix the errors such that the program compiles and runs. Explain the motivation of your modifications in the code and their effects on the execution. --- inheritance2 file--- #include<iostream> using namespace std; class A { int x; public: A(int i) { x = i; } void print() { cout << x; } }; class B:...

  • What is printed by the following program? (read the code carefully) #include <iostream> using namespace std;...

    What is printed by the following program? (read the code carefully) #include <iostream> using namespace std; class A { public: int x; }; int main() { A *a = new A; A *b = new A; a->x=1; b->x=1; if (a == b) { cout << "black"; } else { cout << "white"; } delete a; delete b; return 0; }

  • There are many problems with the program below. Describe 5 of these problems in detail. For...

    There are many problems with the program below. Describe 5 of these problems in detail. For each, also explain how you could fix or avoid the issue. #include <iostream> using namespace std; class Duck { public: virtual void quack() { cout << "Quack" << endl; } }; class RubberDuck : public Duck { public: RubberDuck() { array = NULL; } RubberDuck(int size) { array = new int[size]; array[0] = 1; array[1] = 2; array[2] = 3; } ~RubberDuck() { delete...

  • When I run this code use ./main How to fix it when program reminds segmentation fault...

    When I run this code use ./main How to fix it when program reminds segmentation fault (core dumped)? #include <iostream> void set_num(int* i_prt, int num) { *i_prt = num; } int main(int argc, char** argv) { int x; set_num(&x, 13); std::cout << "x: " << x << std::endl; int* y; set_num(y, 4); std::cout << "*y:" << *y << std::endl; }

  • The following code will not compile. Fix it by inserting one line of code. Do not...

    The following code will not compile. Fix it by inserting one line of code. Do not change anything else. Hint: copy and paste the code below into your answer box, then make the required addition. Also include a comment indicating the line of code that you inserted, and why that line of code is required include <iostream> using namespace std int mainO cout <<"The word of the day is: sayword 0 return 0 void sayWordO f cout くく "WORD!";

  • What will be printed by the code below What will be printed by the code below...

    What will be printed by the code below What will be printed by the code below #include <iostream> using namespace std; class A { public: A() {cout << "+1";} ~A() {cout << "-1";} }; class B: public A { public: B() {cout << "+2";} ~B() {cout << "-2";} }; class C: public B { publc: C() {cout << "+3";} ~C() {cout << "-3";} }; int main() { A a1; B b1; A *a2 = new A; B *b2; = new...

  • JAVA CODE This program causes an error and crashes. Compile and give it a test run,...

    JAVA CODE This program causes an error and crashes. Compile and give it a test run, note the exception that is thrown. Then do the following to fix the problem 1. Write code to handle this exception 2. Use try, catch and finally blocks. 3. Display, the name of the exception 4. Display a message from the user about what happened. Here is the starting file BadArray.java: public class BadArray { public static void main(String[] args) { // Create an...

  • KINDLY HELP SOLVE THE C++ SECURE SHELL DRILLS, RUN AND COMPILE THEM TO PERFORM THE TASK PERFECTLY...

    KINDLY HELP SOLVE THE C++ SECURE SHELL DRILLS, RUN AND COMPILE THEM TO PERFORM THE TASK PERFECTLY. THANK YOU. /1 Function: Complete the body of the Read _Kth Num function. #include <iostream> using namespace std; // Read the k'th input value. DO NOT ISSUE A PROMPT // Example: when 3 and inputs are 4 6 8 10 12 the desired value is 8 void Read Kth Num (int k, int & value) tinclude "mainpt60300a.cpp" //Function: Complete the MaxVal function #include...

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