• Loading...

Yet another Segmentation Fault (Debugging 2)

Points : 20

Remove error(s)/segmentation fault(s)/warning(s) from the given CPP code by

  1. Deleting exactly three characters (7 marks)
  2. Adding exactly one keyword (5 marks)

In addition to above changes by further deleting exactly three characters (8 marks), change c1 contents such that


	c1.id[0]=c1.id[1]=c1.id[2]=0;
	c1.id[3]=200;

For all the changes you do, you need to provide proper explanation for the same. More than the answer, explanation carries more points in your solution.

Note: Use g++ 3.4 or higher compiler. Avoid using Turbo compilers. Do not use any optional flag for compilation.

Code

  1. #include<iostream>
  2. #include<limits>
  3. using namespace std;
  4.    
  5. class Codefest {
  6.     int a;
  7.     int id[20000];      
  8.  
  9.     public:
  10.     Codefest();
  11.     Codefest(int);
  12.     int& eventsCount();          
  13.     void print();
  14.     int& operator[](int);
  15.      Codefest& operator=(const Codefest&);
  16. };
  17.  
  18. Codefest::Codefest(){
  19.     a=0;
  20.     for(int i=0;i<20000;i++)
  21.       id[i]=0;
  22. }
  23. Codefest::Codefest(int x){
  24.     a=x;
  25.     for(int i=0;i<20000;i++)
  26.       id[i]=0;
  27.    
  28. }
  29. int& Codefest::operator[](int x){
  30.     return id[x];
  31. }
  32.  Codefest& Codefest::operator=(const Codefest& c){
  33.     a=c.a;
  34.     for(int i=0;i<20000;i++){
  35.         id[i]=c.id[i];
  36.     }
  37. }
  38. int& Codefest::eventsCount(){
  39.     int ec=6;
  40.     return ec;
  41. }
  42. void Codefest::print(){
  43.     cout<<"Hello CodeWarriors"<<a<<endl;
  44. }
  45.  
  46. int main(){
  47.     Codefest* p2010 = NULL;
  48.     p2010->print();
  49.     p2010->eventsCount();
  50.     Codefest c1;
  51.     c1[0]=3;
  52.     c1[1]=90;
  53.     c1[2]=100;
  54.     c1[3]=200;
  55.     for(int i=0;i<4;i++){
  56.         cout<<c1[i]<<endl;
  57.     }
  58.     return 0;
  59. }

Solution

  • To remove the warning, add a keyword static to line 39.
    static int ec = 6
    This is because whenever you are returning a reference you must be sure that the associated object exists even after returning from the function. Since locals get destroyed immediately after exit from function the returned reference must point to a static area which exists till the end of the program.
    Reference: Pg 452 Bruce Eckel Thinking in C++ Vol 1.
  • To remove the run time error/segmentation fault, delete 3 characters from line 43.
    cout<<"Hello Warriors "<<endl;
    Though a "this" pointer is passed to every member function, that is not used until a member variable is accessed. Removing variable "a" from the line eliminates the null "this" pointer dereference , which was the reason for segmentation fault.
    Reference: Pg 240 Bruce Eckel Thinking in C++ Vol 1
  • To get the desired output, change line 53 to
    c1 = 100
    This is an example of type conversion where the line has the same effect as
    c1 = Codefest(100);
    Constructors with single arguments also serve as type conversions. After this the "operator=" is invoked for the assignment and hence the new contents.
    Reference: http://www.cplusplus.com/doc/tutorial/typecasting/

    Post your queries regarding the solution at Debugging 2 Forum Thread.

Submit your solution

You need to be logged in to submit a solution.

discussions


Sidregarding ;
Q:; is counted as a character
A:Yes. View http://www.asciitable.com/ for the list of all 256 characters.
Sidregarding variable
Q:can we replace some character along with deleting exactly 3 chracters
A:No, you are allowed to only delete 3 characters.
ReDucTorquery
Q:Does keyword include opperand (e.g. new xxx)
A:Yes it does. Refer http://www.cppreference.com/wiki/keywords/start for list of all CPP keywords.
xolveWhat counts as character?
Q:Do spaces and ,=)(}{[] count as characters to delete?
A:Yes. View http://www.asciitable.com/ for the list of all 256 characters.
codegamblerquery
Q:can i shift the position of lines in code.
A:No. Do only as mentioned in the problem statement
viveknNo of chars
Q:Should i delete 3 chars from existing code and change nothing else or should there be a net decrease of 3 chars in the code?
A:You are allowed to only delete 3 character from existing code and add a keyword.

post a question

Loading...

Round 2 Submissions
Puzzle 1
25 pt
You are not logged in
29
submissions

Puzzle 2
20 pt
You are not logged in
23
submissions

Algorithm 1
30 pt
You are not logged in
26
submissions

Algorithm 2
30 pt
You are not logged in
26
submissions

Debugging 1
30 pt
You are not logged in
50
submissions

Debugging 2
20 pt
You are not logged in
34
submissions

Programming
20 pt
You are not logged in
77
submissions

Puzzle 1
17 pt
You are not logged in
32
submissions

Puzzle 2
8 pt
You are not logged in
47
submissions

Algorithm 1
17 pt
You are not logged in
19
submissions

Algorithm 2
13 pt
You are not logged in
15
submissions

Debugging 1
7 pt
You are not logged in
98
submissions

Debugging 2
8 pt
You are not logged in
57
submissions

Programming
30 pt
You are not logged in
28
submissions